树状数组(Binary Index Tree, BIT)。
最简单的树状数组支持两种操作,时间复杂度均为 :
单点修改:更改数组中一个元素的值
区间查询:查询一个区间内所有元素的和
1.lowbit(x)
lowbit(x) = ((x)&(-x))
此操作为取x在二进制下的最后一位1(数字在计算机中是以二进制补码存储的,因此x,-x相与可以得到最后一位1)
2.两种操作
单点修改:
int tree[MAXN];
inline void update(int i, int x)
{
for (int pos = i; pos < MAXN; pos += lowbit(pos))
tree[pos] += x;
}区间求和(以前n相和为基础):
inline int query(int n)
{
int ans = 0;
for (int pos = n; pos; pos -= lowbit(pos))
ans += tree[pos];
return ans;
}
inline int query(int a, int b)
{
return query(b) - query(a - 1);
}例题
1、HDU P1166 http://acm.hdu.edu.cn/showproblem.php?pid=1166
敌兵布阵
Problem Description
C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.
Input
第一行一个整数T,表示有T组数据。
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令
Output
对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
Sample Input
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End
Sample Output
Case 1:
6
33
59
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#define lowbit(x) ((x)&(-x))
using namespace std;
int a[50005];
int n;
void add(int i,int x) {
for(;i<50005;i+=lowbit(i)) {
a[i]+=x;
}
return;
}
int sum(int i) {
int res=0;
for(;i;i-=lowbit(i)) {
res+=a[i];
}
return res;
}
int main() {
int t,cas=0;
scanf("%d",&t);
for(cas=1;cas<=t;++cas){
memset(a,0,sizeof(a));
printf("Case %d:\n",cas);
scanf("%d",&n);
for(int i=1;i<=n;++i) {
int x;
scanf("%d",&x);
add(i,x);
}
char s[10];
while(scanf("%s",s)) {
if(strcmp(s,"End")==0) break;
else if(strcmp(s,"Query")==0) {
int l,r;
scanf("%d %d",&l,&r);
printf("%d\n",sum(r)-sum(l-1));
}
else if(strcmp(s,"Add")==0) {
int pos,x;
scanf("%d %d",&pos,&x);
add(pos,x);
}
else {
int pos,x;
scanf("%d %d",&pos,&x);
add(pos,-x);
}
}
}
return 0;
}2、洛谷 P1908 https://www.luogu.com.cn/problem/P1908
逆序对个数:
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<utility>
#include<algorithm>
#define lowbit(x) ((x)&(-x))
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
const LL mod=1e9+7;
int read() {
char ch=getchar();int x=0,f=1;
while(ch<'0' || ch>'9') {if(ch=='-')f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int gcd_(int a,int b) {return b==0?a:gcd_(b,a%b);}
LL fpow(LL a,LL b) {
LL res=1;
while(b) {
if(b&1) res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
}
LL n;
LL a[500005];
LL res[500008];
struct Node{
LL x;
LL pos;
}b[500005];
bool cmp(Node x,Node y) {
if(x.x!=y.x) return x.x<y.x;
return x.pos<y.pos;
}
void add(LL i,LL x) {
for(;i<500005;i+=lowbit(i)) {
res[i]+=x;
}
return;
}
LL sum(LL i) {
LL num=0;
for(;i;i-=lowbit(i)) {
num+=res[i];
}
return num;
}
int main() {
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
// cout<<"Accepted!\n";
scanf("%lld",&n);
for(int i=1;i<=n;++i) {
scanf("%lld",&b[i].x);
b[i].pos=1LL*i;
}
sort(b+1,b+1+n,cmp);
for(int i=1;i<=n;++i) {
a[b[i].pos]=1LL*i;
}
LL s=0;
for(int i=1;i<=n;++i) {
add(a[i],1);
s+=sum(a[i]);
}
s=n*(n+1)/2-s;//逆序对+顺序对=总共对数
printf("%lld",s);
return 0;
}

京公网安备 11010502036488号