1. 程式人生 > >樹狀陣列---區間更新,區間查詢

樹狀陣列---區間更新,區間查詢

對於區間修改、區間查詢這樣的簡單問題,打一大堆線段樹確實是不划算,今天來介紹一下區間查詢+區間修改的樹狀陣列

【一些基礎】

樹狀陣列的基本知識不再介紹,請自行百度

我們假設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)的時間內完成一次區間和查詢

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 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

Hint

The sums may exceed the range of 32-bit integers.

#include <iostream>
#include <cstdio>
#include <cstring>
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;
}

對比一下:

上面的樹狀陣列寫的,下面的是線段樹寫的