poj 3468 A Simple Problem with Integers,poj3468

來源:互聯網
上載者:User

poj 3468 A Simple Problem with Integers,poj3468

A Simple Problem with IntegersTime Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64uSubmit Status

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

Hint

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



#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define maxn 200005#define inf 0x3f3f3f3ftypedef long long lls;lls a[maxn];lls sum[maxn<<2],add[maxn<<2],ll[maxn<<2],rr[maxn<<2];inline void pushup(int i){    sum[i]=sum[i<<1]+sum[i<<1|1];}inline void pushdown(lls i,lls m){    if(add[i]){        sum[i<<1]+=add[i]*(m-(m>>1));        sum[i<<1|1]+=add[i]*(m>>1);        add[i<<1]+=add[i];        add[i<<1|1]+=add[i];        add[i]=0;    }}void build(lls l,lls r,lls i){    ll[i]=l;    rr[i]=r;    add[i]=0;    if(l==r){        sum[i]=a[l];        return;    }    lls m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1;    build(l,m,ls);    build(m+1,r,rs);    pushup(i);}void update(lls l,lls r,lls v,lls i){    if(ll[i]>=l&&rr[i]<=r){        add[i]+=v;        sum[i]+=(lls)(rr[i]-ll[i]+1)*v;        return ;    }    pushdown(i,rr[i]-ll[i]+1);    lls m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1;    if(l<=m)update(l,r,v,ls);    if(m<r)update(l,r,v,rs);    pushup(i);}lls query(lls l,lls r,lls i){    if(ll[i]>=l&&rr[i]<=r){       return sum[i];    }    pushdown(i,rr[i]-ll[i]+1);    lls m=(ll[i]+rr[i])>>1,ls=i<<1,rs=ls|1;    lls ans=0;    if(l<=m)ans+=query(l,r,ls);    if(m<r)ans+=query(l,r,rs);    return ans;}int main(){    int n,qq;    int u,v,c;    //freopen("in.txt","r",stdin);    while(~scanf("%d%d",&n,&qq)){        for(int i=1;i<=n;i++){            scanf("%lld",&a[i]);        }        build(1,n,1);        char q[2];        while(qq--){            scanf("%s",&q);            if(q[0]=='Q'){                scanf("%d%d",&u,&v);                printf("%lld\n",query(u,v,1));            }            else {                scanf("%d%d%d",&u,&v,&c);                update(u,v,c,1);            }        }    }}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.