http://poj.org/problem?id=3468
A Simple Problem with Integers
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 38088 | Accepted: 11046 | |
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
成段更新,区间求和。。。入门题。。
为嘛我做了这么久,还要照着模版打。。。。弱。
View Code
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define maxn 100010 4 #define lll __int64 5 lll num[maxn]; 6 lll ans; 7 struct Node 8 { 9 lll sum,p; 10 int l,r; 11 }node[maxn*4]; 12 void build_tree(int rt,int ll,int rr) 13 { 14 node[rt].l=ll; 15 node[rt].r=rr; 16 node[rt].p=0; 17 if(ll==rr) 18 { 19 node[rt].sum=num[ll]; 20 21 return; 22 } 23 int mid=(node[rt].l+node[rt].r)/2; 24 build_tree(rt*2,ll,mid); 25 build_tree(rt*2+1,mid+1,rr); 26 node[rt].sum=node[rt*2].sum+node[rt*2+1].sum; 27 } 28 29 void update(int rt,int s,int e,lll value) 30 { 31 if(node[rt].l==s&&node[rt].r==e) 32 { 33 node[rt].sum+=(e-s+1)*value; 34 node[rt].p+=value; 35 return; 36 } 37 if(node[rt].p) 38 { 39 node[rt*2].p+=node[rt].p; 40 node[rt*2+1].p+=node[rt].p; 41 node[rt*2].sum+=(node[rt*2].r-node[rt*2].l+1)*node[rt].p; 42 node[rt*2+1].sum+=(node[rt*2+1].r-node[rt*2+1].l+1)*node[rt].p; 43 node[rt].p=0; 44 } 45 int mid=(node[rt].l+node[rt].r)/2; 46 if(e<=mid) 47 { 48 update(rt*2,s,e,value); 49 } 50 else if(s>mid) 51 { 52 update(rt*2+1,s,e,value); 53 } 54 else 55 { 56 update(rt*2,s,mid,value); 57 update(rt*2+1,mid+1,e,value); 58 } 59 node[rt].sum=node[rt*2].sum+node[rt*2+1].sum; 60 } 61 62 void query(int rt,int x,int y) 63 { 64 if(node[rt].l==x&&node[rt].r==y) 65 { 66 ans+=node[rt].sum; 67 return; 68 } 69 else 70 { 71 node[rt*2].p+=node[rt].p; 72 node[rt*2+1].p+=node[rt].p; 73 node[rt*2].sum+=(node[rt*2].r-node[rt*2].l+1)*node[rt].p; 74 node[rt*2+1].sum+=(node[rt*2+1].r-node[rt*2+1].l+1)*node[rt].p; 75 node[rt].p=0; 76 } 77 int mid=(node[rt].l+node[rt].r)/2; 78 if(y<=mid) 79 { 80 query(rt*2,x,y); 81 } 82 else if(x>mid) 83 { 84 query(rt*2+1,x,y); 85 } 86 else 87 { 88 query(rt*2,x,mid); 89 query(rt*2+1,mid+1,y); 90 } 91 92 } 93 int main() 94 { 95 lll val; 96 int n,q; 97 int i,x,y; 98 char ch; 99 while(~scanf("%d%d",&n,&q)) 100 { 101 for(i=1;i<=n;i++) 102 { 103 scanf("%I64d",&num[i]); 104 } 105 build_tree(1,1,n); 106 for(i=0;i<q;i++) 107 { 108 getchar(); 109 scanf("%c",&ch); 110 if(ch=='C') 111 { 112 scanf("%d%d%I64d",&x,&y,&val); 113 update(1,x,y,val); 114 } 115 else if(ch=='Q') 116 { 117 ans=0; 118 scanf("%d%d",&x,&y); 119 query(1,x,y); 120 printf("%I64d\n",ans); 121 } 122 } 123 } 124 }