poj3468
阿新 • • 發佈:2017-09-17
gin res dea with poj lines sum printf urn 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. commands in order. One answer in a line.
A Simple Problem with Integers
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
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
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。 還是簡單的線段樹。1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cmath> 5#include<cstring> 6 const int MAXN=100007; 7 char s[20],c; 8 long long tree[MAXN*4]={0},add[MAXN*4]={0}; 9 int t,num,x,n,y,m,z,a[MAXN]={0}; 10 using namespace std; 11 void build(int l,int r,int p){ 12 if (l==r) tree[p]=a[l]; 13 else { 14 int mid=(l+r)>>1; 15 build(l,mid,p*2); 16 build(mid+1,r,p*2+1); 17 tree[p]=tree[p*2]+tree[p*2+1]; 18 } 19 } 20 long long query(int l,int r,int p,int x,int y){ 21 if (l==x&&r==y) return tree[p]+add[p]*(r-l+1); 22 else { 23 add[p*2+1]+=add[p]; 24 add[p*2]+=add[p]; 25 tree[p]+=add[p]*(r-l+1); 26 add[p]=0; 27 int mid=(l+r)>>1; 28 if (y<=mid) return query(l,mid,p*2,x,y); 29 else if(x>=mid+1) return query(mid+1,r,p*2+1,x,y); 30 else return query(l,mid,p*2,x,mid)+query(mid+1,r,p*2+1,mid+1,y); 31 } 32 } 33 void change(int l,int r,int p,int x,int y,int zhi) 34 { 35 if (l==x&&r==y) add[p]+=zhi; 36 else 37 { 38 tree[p]+=zhi*(y-x+1); 39 int mid=(l+r)>>1; 40 if (y<=mid) change(l,mid,p*2,x,y,zhi); 41 else if (x>mid) change(mid+1,r,p*2+1,x,y,zhi); 42 else 43 { 44 change(l,mid,p*2,x,mid,zhi); 45 change(mid+1,r,p*2+1,mid+1,y,zhi); 46 } 47 } 48 } 49 int main(){ 50 while (~scanf("%d%d",&n,&m)){ 51 for (int i=1;i<=n;i++) 52 scanf("%d",&a[i]); 53 build(1,n,1); 54 for (int i=1;i<=m;i++){ 55 scanf("%s%d%d",&s,&x,&y); 56 if (s[0]==‘Q‘) printf("%lld\n",query(1,n,1,x,y)); 57 else { 58 scanf("%d",&z); 59 change(1,n,1,x,y,z); 60 } 61 } 62 } 63 }
poj3468