樹狀數組成段更新——POJ 3468

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   strong   

A Simple Problem with IntegersCrawling in process...Crawling failedTime Limit:5000MS    Memory Limit:131072KB     64bit IO Format:%I64d & %I64u SubmitStatus Practice POJ 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 abc" means adding c to each of Aa,Aa+1, ... ,Ab. -10000 ≤ c ≤ 10000.
"Q ab" 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 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915

 

這裡講得很詳細:點擊開啟連結

 

#include<cstdio>#include<cstdlib>#include<cmath>#include<map>#include<queue>#include<stack>#include<vector>#include<algorithm>#include<cstring>#include<string>#include<iostream>const int MAXN=100000+10;using namespace std;typedef long long LL;LL sum[MAXN];LL c1[MAXN];LL c2[MAXN];int lowbit(int x){return x&(-x);}void update(LL *c, int x, LL val){while(x<=MAXN){c[x]+=val;x+=lowbit(x);}}LL Sum(LL *c, int x){LL res=0;while(x>0){res+=c[x];x-=lowbit(x);}return res;}LL SUM(int x){return sum[x]+(x+1)*Sum(c1,x)-Sum(c2,x);}int main(){//reopen("in.txt","r",stdin);int n,m;while(scanf("%d%d", &n,&m)==2){memset(c1,0,sizeof(c1));memset(c2,0,sizeof(c2));int i,j,k;for(i=1; i<=n; i++){scanf("%d", &j);sum[i]=sum[i-1]+j;}char ch[3];while(m--){scanf("%s", ch);if(ch[0]=='Q'){scanf("%d%d", &i,&j);LL ans=SUM(j)-SUM(i-1);printf("%I64d\n", ans);}else{scanf("%d%d%d", &i,&j,&k);update(c1,i,k);update(c1,j+1,-k);update(c2,i,i*k);update(c2,j+1,-k*(j+1));}}}return 0;}


 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.