对于区间修改、区间查询这样的简单问题,打一大堆线段树确实是不划算,今天来介绍一下区间查询+区间修改的树状数组
【一些基础】
树状数组的基本知识不再介绍,请自行百度
我们假设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)的时间内完成一次区间和查询
题目链接:http://poj.org/problem?id=3468
Description
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
#include
#include
using namespace std;
typedef long long ll ;
const int maxn = 1e5 + 10;
ll a[maxn], c1[maxn], c2[maxn];
int n , m;
int lowbit(int i){
return i & (-i);
}
void Build (ll *ar, int i, ll x){
while (i <= n){
ar[i] += x;
i += lowbit(i);
}
}
ll add (ll *ar, int x){
ll ans = 0;
while (x > 0){
ans += ar[x];
x -= lowbit(x);
}
return ans ;
}
int main (){
while (~scanf("%d%d",&n, &m)){
a[0] = 0;
for (int i = 1; i <= n; i++){
scanf("%lld", &a[i]);
Build (c1, i, (a[i] - a[i - 1]));
Build (c2, i, (i - 1) * (a[i] - a[ i - 1]));
}
char op[2];
int x, y ;
ll z;
while (m--){
scanf("%s",op);
if (op[0] == 'Q'){
scanf("%d%d",&x,&y);
ll ans = y * add(c1, y) - add(c2, y) - ((x - 1)*add(c1, x- 1) - add(c2, x - 1));
printf("%lld\n",ans);
}
else {
scanf("%d%d%lld",&x,&y,&z);
Build (c1, x, z);
Build (c1, y + 1, -z);
Build (c2, x, (x - 1) * z);
Build (c2, y + 1, y * (-z));
}
}
}
return 0;
}
上面的树状数组写的,下面的是线段树写的