解釋下關於數狀陣列區間更新、單點查詢和區間更新、區間查詢
首先說明一點,HDU - 1556 Color the ball (一維樹狀陣列 + 區間修改 + 單點求值),比如給區間[a, b]加1,就直接用,add(a, 1),add(b+1, 1),那是因為這個題本來就滿足字首和,可以這麼用,對於其他的序列這樣用肯定是錯的。現在來講一下樹狀陣列正確的區間更新,好坑啊!網上找了很多部落格和文章,講的都不清楚,有的還以為像氣球塗色一樣,這樣做就是正確的區間更新。所以決定寫篇文章好好講一下樹狀陣列的區間更新。
對原陣列做拆分,即令的d[i] = a[i] – a[i-1],特別地d[i] = a[i].則a[i] = d[1] + d[2] + … + d[i];
做單點查詢就是在求d[1….i]的和。給整個區間[l, r]增加k,d[l] += k, d[r+1] -= k;維護的d陣列就行了。區間求和,仍然沿用d陣列,考慮a陣列[1,x]區間和的計算。d[1]被累加了x次,d[2]被累加了x-1次,...,d[x]被累加了1次。sigma(a[i]) =sigma{d[i]*(x-i+1)}
=sigma{ d[i]*(x+1) - d[i]*i } =(x+1)*sigma(d[i])-sigma(d[i]*i)。相當於把每個的d[i]都加x+1次,然後減去多加的。如果區間[r,x],用兩個和減一下。sum=(x+1)*sigma(d[i])-sigma(d[i]*i)-(r)*sigma(d[i])+sigma(d[i]*i)。[請注意我們上面的討論都假定了a[]初始全是0。如果不是這樣呢?下面的程式裡給出了一個相對簡便的處理辦法。]我們來手撕一道例題POJ3468(區間更新,區間查詢).
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 109930 | Accepted: 34223 | |
Case Time Limit: 2000MS |
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
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
#include <stdio.h>
#include <string.h>
#define maxn 100010
long long a[maxn],b[maxn],c[maxn]; //b陣列維護字首和,c陣列維護區間和
int n,m;
int lowbit(const int &x){
return x&(-x);
}
long long query(long long *a, int x){
long long sum=0;
while (x) {
sum += a[x];
x -= lowbit(x);
}
return sum;
}
void update(long long *a, int x, long long w){
while ( x<= n) {
a[x] += w;
x += lowbit(x);
}
}
int main(){
int l,r,i;
long long ans,w;
char ch;
scanf("%d%d",&n,&m);
a[0]=0;
for(i = 1;i <= n; ++i){
scanf("%I64d",&a[i]);
a[i] += a[i-1];
}
while(m--){
scanf("%c",&ch);
while(ch != 'Q' && ch != 'C') scanf("%c",&ch);
if(ch == 'Q'){
scanf("%d%d",&l,&r);
ans = a[r] - a[l - 1] + (r+1) * query(b,r) - query(c,r) - l * query(b, l-1) + query(c, l - 1); //利用起點和終點差,求區間和
printf("%I64d\n",ans);
}else{
scanf("%d%d%I64d", &l, &r, &w);
update(b, l, w);
update(b, r + 1, -w); //字首和更新
update(c, l, w * l);
update(c, r + 1, -(r + 1) * w); //區間和更新
}
}
return 0;
}
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 20225 Accepted Submission(s): 10074
Problem Description N個氣球排成一排,從左到右依次編號為1,2,3....N.每次給定2個整數a b(a <= b),lele便為騎上他的“小飛鴿"牌電動車從氣球a開始到氣球b依次給每個氣球塗一次顏色。但是N次以後lele已經忘記了第I個氣球已經塗過幾次顏色了,你能幫他算出每個氣球被塗過幾次顏色嗎?
Input 每個測試例項第一行為一個整數N,(N <= 100000).接下來的N行,每行包括2個整數a b(1 <= a <= b <= N)。
當N = 0,輸入結束。
Output 每個測試例項輸出一行,包括N個整數,第I個數代表第I個氣球總共被塗色的次數。
Sample Input 3 1 1 2 2 3 3 3 1 1 1 2 1 3 0
Sample Output 1 1 13 2 1
#include <stdio.h>
#include <string.h>
#define maxn 100010
long long a[maxn],b[maxn],c[maxn]; //b陣列維護字首和,c陣列維護區間和
int n;
int lowbit(const int &x){
return x&(-x);
}
long long query(long long *a, int x){
long long sum=0;
while (x) {
sum += a[x];
x -= lowbit(x);
}
return sum;
}
void update(long long *a, int x, long long w){
while ( x<= n) {
a[x] += w;
x += lowbit(x);
}
}
int main() {
int l, r, ans;
memset(a, 0, sizeof(a));
memset(c, 0, sizeof(c));
while(scanf("%d", &n) && n) {
memset(b, 0, sizeof(b));
for (int i = 0; i < n; i++) {
scanf("%d%d",&l,&r);
update(b, l, 1);
update(b, r + 1, -1); //字首和更新
}
for (int i = 1; i <= n; i++) {
if (i == n) {
printf("%I64d\n",query(b, i));
} else {
printf("%I64d ",query(b, i));
}
}
}
return 0;
}
用上題程式碼直接修改了下,就AC了。用為氣球塗色是區間更新+單點查詢。