解释下关于数状数组区间更新、单点查询和区间更新、区间查询

首先说明一点,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(区间更新,区间查询).

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 109930   Accepted: 34223
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+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
#include 
#include  
#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 1
3 2 1
#include 
#include  
#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了。用为气球涂色是区间更新+单点查询。



你可能感兴趣的:(AMC&&算法)