下面是转载+原创的一篇文章:
【问题引入】
对于区间修改、区间查询这样的简单问题,打一大堆线段树确实是不划算,今天来介绍一下区间查询+区间修改的树状数组
【一些基础】
树状数组的基本知识不再介绍,请自行百度
我们假设sigma(r,i)表示r数组的前i项和,调用一次的复杂度是log2(i)
设原数组是a[n],差分数组c[n],c[i]=a[i]-a[i-1],那么明显地a[i]=sigma(c,i),如果想要修改a[i]到a[j](比如+v),只需令c[i]+=v,c[j+1]-=v
【今天的主要内容】
我们可以实现NlogN时间的“单点修改,区间查询”,“区间修改,单点查询”,其实后者就是前者的一个变形,要明白树状数组的本质就是“单点修改,区间查询”
怎么实现“区间修改,区间查询”呢?
观察式子:
a[1]+a[2]+...+a[n]
= (c[1]) + (c[1]+c[2]) + ... + (c[1]+c[2]+...+c[n])
= n*c[1] + (n-1)*c[2] +... +c[n]
= n * (c[1]+c[2]+...+c[n]) - (0*c[1]+1*c[2]+...+(n-1)*c[n]) (式子①)
那么我们就维护一个数组c2[n],其中c2[i] = (i-1)*c[i]
每当修改c的时候,就同步修改一下c2,这样复杂度就不会改变
那么
式子①
=n*sigma(c,n) - sigma(c2,n)
于是我们做到了在O(logN)的时间内完成一次区间和查询
一件很好的事情就是树状数组的常数比其他NlogN的数据结构小得多,实际上它的计算次数比NlogN要小很多,再加上它代码短,是OI中的利器
下面用一道例题来说明:
Problem T
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 262144/131072K (Java/Other)
Total Submission(s) : 73 Accepted Submission(s) : 17
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.
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
4 55 9 15
样例的意思是C:修改区间(3,6),使每一个数都增加3
q是查询区间的和。
下面是我的代码:
#include<iostream>
#include<stdio.h>
#define M 100100
using namespace std;
long long c[M],c2[M],a[M];
long long lowbit(long long i)
{
return i&(-i);
}
long long sum(long long z,long long i)
{
long long he=0;
while(i>0)
{
if(z==1)
he+=c[i];
else he+=c2[i];
i-=lowbit(i);
}
return he;
}
void add(long long wm,long long i,long long z)
{
if(wm==1)
while(i<=M)
{
c[i]+=z;
i+=lowbit(i);
}
else
while(i<=M)
{
c2[i]+=z;
i+=lowbit(i);
}
}
int main()
{
long long t,m,xx,yy,kk,k;
char s;
cin>>t>>m;
for(int i=1;i<=t;i++)
{
scanf("%lld",&a[i]);
k=a[i]-a[i-1];
add(1,i,k);
add(2,i,(i-1)*k);
}
while(m--)
{
cin>>s;
if(s=='Q')
{
//cin>>xx>>yy;
scanf("%lld%lld",&xx,&yy);
cout<<yy*sum(1,yy)-(xx-1)*sum(1,xx-1)-(sum(2,yy)-sum(2,xx-1))<<endl;
//cout<<(y*sum(1,y)-(x-1)*sum(1,x-1))-(sum(2,y)-sum(2,x-1))<<endl;
}
else
{
//cin>>xx>>yy>>kk;
scanf("%lld%lld%lld",&xx,&yy,&kk);
add(1,xx,kk);
add(2,xx,(xx-1)*kk);
add(1,yy+1,-kk);
add(2,yy+1,-yy*kk);
}
}
return 0;
}