1. 程式人生 > >poj3468 A Simple Problem with Integers (樹狀數組做法)

poj3468 A Simple Problem with Integers (樹狀數組做法)

align style cee arch puppet ret 定義 pre tab

題目傳送門 A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 142198 Accepted: 44136
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

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

Source

POJ Monthly--2007.11.25, Yang Yi 題意:
給一個數列,Q個操作,遇到C則往[l,r]中加上x,遇到Q則求[l,r]的和
題解:樹狀數組做法 樹狀數組基本功能是實現單點更新和求前綴和,本題是要實現快速實現區間的更新。 首先,我們來看,在[l,r]上加上x的話,i<l時,所求的前綴和不變,l<=i<=r時,所求的前綴和增加了x*(i-l+1)=x*i-x*(l-1); i>r時,增加了x*(r-l+1)=x*r-x*(l-1); 這時我們定義兩個樹狀數組 bit1:前綴和樹狀數組 bit0:加法樹狀數組 從上面的前綴和增加量來看,從大於L後的前綴和都會增加一個東西,就是-x*(l-1),那麽維護這個東西就是updata(bit0,l,-x*(l-1)); 這樣就實現了l以後的前綴和都會加上這個東西,然後我們發現x*r這個也可以提前維護,同理就是updata(bit0,r,x*r);但是這裏還差 一個x*i;因為i是變量,所以這個只能在求和的時候去加上,而加法數組中{updata(bit1,l,x);updata(bit1,r,x);}我們發現這個剛 好實現了在[l,r]的前綴和中加上x,那麽對於每個所求的i,在求前綴和的時候就*i就行了 則sum[i]=sum(bit1,i)*i+sum(bit0,i); 代碼:
#include<iostream>
#include<string.h>
using namespace std;
typedef long long ll;
#define MAX 100005
int n,q;
int a[MAX];
ll bit0[MAX],bit1[MAX];
void updata(ll *b,int i,int val)
{
    while(i<=n)
    {
        b[i]+=val;
        i+=i&-i;
    }
}
ll query(ll *b,int i)
{
    ll res=0;
    while(i>0)
    {
        res+=b[i];
        i-=i&-i;
    }
    return res;
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    while(cin>>n>>q)
    {
        memset(bit0,0,sizeof(bit0));
        memset(bit1,0,sizeof(bit1));
        for(int i=1;i<=n;i++)
        {
           cin>>a[i];
           updata(bit0,i,a[i]);
        }
        while(q--)
        {
            int l,r,x;
            char ch;
            cin>>ch;
            cin>>l>>r;
            if(ch==C)
            {
                cin>>x;
                updata(bit0,l,-x*(l-1));
                updata(bit1,l,x);
                updata(bit0,r+1,x*r);
                updata(bit1,r+1,-x);
            }
            else
            {
                ll sum=0;
                sum+=query(bit0,r)+query(bit1,r)*r;
                sum-=query(bit0,l-1)+query(bit1,l-1)*(l-1);
                cout<<sum<<endl;
            }
        }
    }
}

參考博客:https://www.cnblogs.com/detrol/p/7586083.html

https://blog.csdn.net/Puppet__/article/details/79098936

poj3468 A Simple Problem with Integers (樹狀數組做法)