A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 62431 | Accepted: 19141 | |
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
线段树的基础区域更新
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<stdlib.h> 5 #include<algorithm> 6 #define LL __int64 7 using namespace std; 8 const int MAXN=100000+10; 9 const int INF=0x3f3f3f3f; 10 int b[MAXN]; 11 struct node 12 { 13 int l,r; 14 LL val,col; 15 int mid() 16 { 17 return (l+r)/2; 18 } 19 }a[MAXN*4]; 20 21 void btree(int step,int l,int r) 22 { 23 a[step].l=l; 24 a[step].r=r; 25 a[step].col=0; 26 if(l==r) 27 { 28 a[step].val=b[l]; 29 //scanf("%d",&a[step].val); 这样输入会WA 30 return ; 31 } 32 int mid=a[step].mid(); 33 btree(step*2,l,mid); 34 btree(step*2+1,mid+1,r); 35 a[step].val=a[step*2].val+a[step*2+1].val; 36 } 37 38 void ptree(int step,int L,int R,LL num) 39 { 40 if(L==a[step].l&&a[step].r==R)//找到符合条件的区域就不更新了,记录下要要更新的值 41 { 42 a[step].col+=num; 43 return ; 44 } 45 46 a[step].val+=(R-L+1)*num;//如果不是符合条件的区域,对当前结点更新 47 48 int mid=a[step].mid(); 49 if(R<=mid) 50 ptree(step*2,L,R,num); 51 else if(L>mid) 52 ptree(step*2+1,L,R,num); 53 else 54 { 55 ptree(step*2,L,mid,num); 56 ptree(step*2+1,mid+1,R,num); 57 } 58 } 59 60 LL qtree(int step,int L,int R) 61 { 62 if(L==a[step].l&&a[step].r==R) 63 { 64 return a[step].val+a[step].col*(R-L+1); 65 } 66 67 a[step].val+=(a[step].r-a[step].l+1)*a[step].col;//对当前结点更新 68 69 int mid=a[step].mid();//对子结点更新 70 ptree(step*2,a[step].l,mid,a[step].col); 71 ptree(step*2+1,mid+1,a[step].r,a[step].col); 72 a[step].col=0; 73 74 if(R<=mid) 75 return qtree(step*2,L,R); 76 else if(L>mid) 77 return qtree(step*2+1,L,R); 78 else 79 return qtree(step*2,L,mid)+qtree(step*2+1,mid+1,R); 80 } 81 int main() 82 { 83 //freopen("in.txt","r",stdin); 84 int n,kase; 85 char ch; 86 int star,en,num; 87 while(scanf("%d %d",&n,&kase)!=EOF) 88 { 89 for(int i=1;i<=n;i++) 90 scanf("%d",&b[i]); 91 btree(1,1,n); 92 while(kase--) 93 { 94 cin>>ch; 95 if(ch==‘C‘) 96 { 97 scanf("%d %d %d",&star,&en,&num);//num输入%I64d会RE 98 ptree(1,star,en,num); 99 } 100 else 101 { 102 scanf("%d %d",&star,&en); 103 printf("%I64d\n",qtree(1,star,en)); 104 } 105 } 106 } 107 return 0; 108 }