一、树状数组的基本应用:
其基本用途是维护前缀和。对于给定的序列a,我们建立一个数组c,
c(x)保存序列a的区间 [x−lowbit(x)+1,x]中所有数的前缀和。
事实上,数组c可以看作一个树形结构。叶子节点为a[1–N]。
(1)每个内部节点c(x)保存以它为根的子树中所有叶节点的和。
(2)每个内部节点c(x)的子节点个数等于lowbit(x)的位数。
(3)除树根外,每个内部节点c(x)的父节点是c(x+lowbit(x))。
(4)树的深度为O(logN)。
(5)如果N不是2的整次幂,那么树状数组就是一个具有同样性质的森林结构。
树状数组支持的操作有两个:
----查询前缀和。
int ask(int x)
{
int ans=0;
for( ; x ; x -= (x&-x))
ans+=c[x];
return ans;
}
//区间[L,R]---ask(R)-ask(L-1)
----单点修改。
void add(int x,int y)
{
for(; x<=N ; x += (x&-x) )
c[x]+=y;
}
初始化:
针对原始序列a,构造一个树状数组。
为了简便起见,比较一般的初始化方法是:直接建立一个全为0的数组c,然后对每个位置的x执行add(x,a(x))操作,就完成了对原始序列a构造树状数组的过程,时间复杂度为O(nlogn)。通常采用这种初始化方法已经够了。
更高效的初始化方法是:从小到大依次考虑每个节点x,借助lowbit运算扫描它的子节点并求和。若采用这种方法,树形结构中的每条边只会被遍历一次,时间复杂度为O(n)。
二、树状数组与逆序对:
在序列a的数值范围上建立树状数组,初始化全为0。
倒序扫描给定的序列a,对于每个数a(i):
在树状数组中查询前缀和(1,a(i)-1),累加到答案ans中。
执行单点增加操作,即把位置a(i)上的数加1,同时正确维护t的前缀和。这表示数值a(i)又出现了一次。
ans即为所求。
for(int i=n; i ;i--)
{
ans+=ask(a[i]-1);
add(a[i],1);
}
在这个算法中,因为倒叙扫描,已经出现过的数就是在a(i)后面的数,所以我们通过树状数组查询的内容就是每个a(i)后面有多少个数比它小。每次查询结果之和当然是逆序对个数。时间复杂度为O((N+M)logM),M为数值范围大小。
当数值范围较大时,可以先进行离散化,再用树状数组进行计算。不过因为离散化本身就要通过排序来实现,所以在这种情况下就不如直接用归并排序来计算逆序对数了。
三、单点修改、区间查询:
int ask(int x)
{
int ans=0;
for( ; x ; x -= (x&-x))
ans+=c[x];
return ans;
}
//区间[L,R]---ask(R)-ask(L-1)
void add(int x,int y)
{
for(; x<=N ; x += (x&-x) )
c[x]+=y;
}
P3374 【模板】树状数组 1:
题目描述
如题,已知一个数列,你需要进行下面两种操作:
1.将某一个数加上x
2.求出某区间每一个数的和
输入输出格式
输入格式:
第一行包含两个整数N、M,分别表示该数列数字的个数和操作的总个数。
第二行包含N个用空格分隔的整数,其中第i个数字表示数列第i项的初始值。
接下来M行每行包含3个整数,表示一个操作,具体如下:
操作1: 格式:1 x k 含义:将第x个数加上k
操作2: 格式:2 x y 含义:输出区间[x,y]内每个数的和
输出格式:
输出包含若干行整数,即为所有操作2的结果。
输入输出样例
输入样例#1:
5 5
1 5 4 2 3
1 1 3
2 2 5
1 3 -1
1 4 2
2 1 4
输出样例#1:
14
16
说明
时空限制:1000ms,128M
数据规模:
对于30%的数据:N<=8,M<=10
对于70%的数据:N<=10000,M<=10000
对于100%的数据:N<=500000,M<=500000
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=500005;
ll c[maxn]={0};
ll ask(int x)
{
ll ans=0;
for( ; x ; x -= (x&-x))
ans+=c[x];
return ans;
}
//区间[L,R]---ask(R)-ask(L-1)
void add(int x,int y)
{
for(; x<maxn ; x += (x&-x) )
c[x]+=y;
}
int main(void)
{
int n,m;
scanf("%d%d",&n,&m);
int k;
for(int i=1;i<=n;i++)
{
scanf("%d",&k);
add(i,k);
}
int l,r;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&k,&l,&r);
if(k==1) add(l,r);
else
{
printf("%lld\n",ask(r)-ask(l-1));
}
}
return 0;
}
四、区间修改、单点查询:
(一)、正向树状数组加差分,维护指令的累积影响。
P3368 【模板】树状数组 2
题目描述
如题,已知一个数列,你需要进行下面两种操作:
1.将某区间每一个数数加上x
2.求出某一个数的值
输入输出格式
输入格式:
第一行包含两个整数N、M,分别表示该数列数字的个数和操作的总个数。
第二行包含N个用空格分隔的整数,其中第i个数字表示数列第i项的初始值。
接下来M行每行包含2或4个整数,表示一个操作,具体如下:
操作1: 格式:1 x y k 含义:将区间[x,y]内每个数加上k
操作2: 格式:2 x 含义:输出第x个数的值
输出格式:
输出包含若干行整数,即为所有操作2的结果。
输入输出样例
输入样例#1:
5 5
1 5 4 2 3
1 2 4 2
2 3
1 1 5 -1
1 3 5 7
2 4
输出样例#1:
6
10
说明
时空限制:1000ms,128M
数据规模:
对于30%的数据:N<=8,M<=10
对于70%的数据:N<=10000,M<=10000
对于100%的数据:N<=500000,M<=500000
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=500005;
ll c[maxn]={0};
ll a[maxn];
ll ask(int x)
{
ll ans=0;
for( ; x ; x -= (x&-x))
ans+=c[x];
return ans;
}
void add(int x,int y)
{
for(; x<maxn ; x += (x&-x) )
c[x]+=y;
}
int main(void)
{
int n,m;
scanf("%d%d",&n,&m);
int k;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
int l,r,p;
for(int i=1;i<=m;i++)
{
scanf("%d",&k);
if(k==1)
{
scanf("%d%d%d",&l,&r,&p);
add(l,p);
add(r+1,-p);
}
else
{
scanf("%d",&p);
printf("%lld\n",a[p]+ask(p));
}
}
return 0;
}
直接差分:
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=500005;
ll c[maxn]={0};
ll ask(int x)
{
ll ans=0;
for( ; x ; x -= (x&-x))
ans+=c[x];
return ans;
}
void add(int x,int y)
{
for(; x<maxn ; x += (x&-x) )
c[x]+=y;
}
int main(void)
{
int n,m;
scanf("%d%d",&n,&m);
int k;
ll x,y;
for(int i=1;i<=n;i++)
{
scanf("%lld",&y);
add(i,y-x);
x=y;
}
int l,r,p;
for(int i=1;i<=m;i++)
{
scanf("%d",&k);
if(k==1)
{
scanf("%d%d%d",&l,&r,&p);
add(l,p);
add(r+1,-p);
}
else
{
scanf("%d",&p);
printf("%lld\n",ask(p));
}
}
return 0;
}
(二)、逆向树状数组加差分,c(i)表示这段区间当前被加了多少。
#include<iostream>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=500005;
ll c[maxn]={0};
ll a[maxn];
ll ask(int x)
{
ll ans=0;
for( ; x <maxn ; x += (x&-x))
ans+=c[x];
return ans;
}
void add(int x,int y)
{
for(; x ; x -= (x&-x) )
c[x]+=y;
}
int main(void)
{
int n,m;
scanf("%d%d",&n,&m);
int k;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
}
int l,r,p;
for(int i=1;i<=m;i++)
{
scanf("%d",&k);
if(k==1)
{
scanf("%d%d%d",&l,&r,&p);
add(r,p);
add(l-1,-p);
}
else
{
scanf("%d",&p);
printf("%lld\n",a[p]+ask(p));
}
}
return 0;
}
五、区间修改、区间查询:
将其转换为树状数组擅长的,单点修改,区间查询。
POJ 3468 A Simple Problem with Integers
You have N integers, A1, A2, … , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, … , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
“C a b c” means adding c to each of Aa, Aa+1, … , Ab. -10000 ≤ c ≤ 10000.
“Q a b” means querying the sum of Aa, Aa+1, … , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
Hint
The sums may exceed the range of 32-bit integers.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
const int maxn=100008;
ll c[2][maxn]={0};
ll sum[maxn];
void add(int k,int x,ll y)
{
for( ; x<maxn ;x += (x&-x))
c[k][x]+=y;
}
ll ask(int k,int x)
{
ll ans=0;
for(; x ; x -= (x&-x))
ans+=c[k][x];
return ans;
}
int main(void)
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(c,0,sizeof(c));
sum[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&sum[i]);
sum[i]+=sum[i-1];
}
char str[20];
int x,y;
ll z;
for(int i=1;i<=m;i++)
{
scanf("%s",str);
if(str[0]=='C')
{
scanf("%d%d%lld",&x,&y,&z);
add(0,x,z);
add(0,y+1,-z);
add(1,x,x*z);
add(1,y+1,-(y+1)*z);
}
else
{
scanf("%d%d",&x,&y);
ll ans=sum[y]-sum[x-1];
ans+=(y+1)*ask(0,y)-ask(1,y);
ans-=x*ask(0,x-1)-ask(1,x-1);
printf("%lld\n",ans);
}
}
}
return 0;
}
六、二维树状数组:
一、单点修改,区间查询:
void add(int x,int y,int z)
{
for(int i=x;i<maxn;i+=(i&-i))
for(int j=y;j<maxn;j+=(j&-j))
c[i][j]+=z;
}
int ask(int x,int y)
{
int ans=0;
for(int i=x;i;i-=(i&-i))
for(int j=y;j;j-=(j&-j))
ans+=c[i][j];
return ans;
}
如果想求a[x1][y1]--a[x2][y2]这个子矩阵的和,只需计算
ask(x2,y2)-ask(x2,y1-1)-ask(x1-1,y2)+ask(x1-1,y1-1)