标签:
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 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<iostream> #include<stdio.h> #include<math.h> using namespace std; const int maxx = 100010; long long tree[maxx<<2]; long long addmark[maxx<<2]; long long a[maxx]; int n; void build(int root,int l,int r) { //cout<<"root:"<<root<<" l,r:"<<l<<" "<<r<<endl; addmark[root]=0; if(l==r) { tree[root]=a[l]; } else { int mid=(l+r)>>1; build(root<<1,l,mid); build((root<<1)+1,mid+1,r); tree[root]=(tree[root<<1]+tree[(root<<1)+1]); } } void push_down(int root,int l,int r) { if(addmark[root]!=0) { int mid=(l+r)>>1; addmark[root<<1]+=addmark[root]; addmark[(root<<1)+1]+=addmark[root]; tree[root<<1]+=addmark[root]*(mid-l+1); tree[(root<<1)+1]+=addmark[root]*(r-(mid+1)+1); addmark[root]=0; } } void update(int root,int l,int r,int pos,int val) { if(pos==l&&pos==r) { tree[root]+=val; return; } int mid=(l+r)>>1; if(pos<=mid) update(root<<1,l,mid,pos,val); else update((root<<1)+1,mid+1,r,pos,val); tree[root]=(tree[root<<1]+tree[(root<<1)+1]); } void update_interval(int root,int l,int r,int ql,int qr,int val) { if(l==ql&&r==qr) { tree[root]+=val*(qr-ql+1); addmark[root]+=val; return; } push_down(root,l,r); int mid=(l+r)>>1; if(qr<=mid) { update_interval(root<<1,l,mid,ql,qr,val); } else if(ql>mid) { update_interval((root<<1)+1,mid+1,r,ql,qr,val); } else { update_interval(root<<1,l,mid,ql,mid,val); update_interval((root<<1)+1,mid+1,r,mid+1,qr,val); } tree[root]=tree[root<<1]+tree[(root<<1)+1]; } long long query(int root,int l,int r,int ql,int qr) { if(l==ql&&qr==r) { return tree[root]; } int mid=(l+r)>>1; if(addmark[root]!=0) push_down(root,l,r); if(qr<=mid) { return query(root<<1,l,mid,ql,qr); } else if(ql>mid) { return query((root<<1)+1,mid+1,r,ql,qr); } else return (query(root<<1,l,mid,ql,mid)+query((root<<1)+1,mid+1,r,mid+1,qr)); } void print() { int j=1,t=1; for(int i=1; i<=(n<<2); i++) { j++; printf("%I64d ",tree[i]); if(j>pow(2,(t-1))) { printf("\n"); j=1; t++; } } printf("\n\n"); } int main() { int m; scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) { scanf("%I64d",a+i); } build(1,1,n); char cmd; for(int i=0; i<m; i++) { cin>>cmd; if(cmd==‘C‘) { int tmp1,tmp2,val; scanf("%d%d%d",&tmp1,&tmp2,&val); update_interval(1,1,n,tmp1,tmp2,val); } else if(cmd==‘Q‘) { int tmp1,tmp2; scanf("%d%d",&tmp1,&tmp2); long long ans=query(1,1,n,tmp1,tmp2); printf("%I64d\n",ans); } } return 0; }
poj3468 Simple Problem with Integers 区间更新
标签:
原文地址:http://www.cnblogs.com/superxuezhazha/p/5727304.html