| Time Limit: 1 second(s) | Memory Limit: 64 MB |
Robin Hood likes to loot rich people since he helps the poor people with this money. Instead of keeping all the money together he does another trick. He keeps n sacks where he keeps this money. The sacks are numbered from 0 to n-1.
Now each time he can he can do one of the three tasks.
1) Give all the money of the ith sack to the poor, leaving the sack empty.
2) Add new amount (given in input) in the ith sack.
3) Find the total amount of money from ith sack to jth sack.
Since he is not a programmer, he seeks your help.
Input
Input starts with an integer T (≤ 5), denoting the number of test cases.
Each case contains two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). The next line contains n space separated integers in the range [0, 1000]. The ith integer denotes the initial amount of money in theith sack (0 ≤ i < n).
Each of the next q lines contains a task in one of the following form:
1 i Give all the money of the ith (0 ≤ i < n) sack to the poor.
2 i v Add money v (1 ≤ v ≤ 1000) to the ith (0 ≤ i < n) sack.
3 i j Find the total amount of money from ith sack to jth sack (0 ≤ i ≤ j < n).
Output
For each test case, print the case number first. If the query type is 1, then print the amount of money given to the poor. If the query type is 3, print the total amount from ith to jth sack.
Sample Input |
Output for Sample Input |
|
1 5 6 3 2 1 4 5 1 4 2 3 4 3 0 3 1 2 3 0 4 1 1 |
Case 1: 5 14 1 13 2 |
Notes
Dataset is huge, use faster I/O methods.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 #define lson l,mid,p<<1 7 #define rson mid+1,r,p<<1|1 8 #define demid int mid=(l+r)>>1 9 #define N 100010 10 11 int segt[N<<2],col[N<<2]; 12 13 void pushup(int p){ 14 segt[p]=segt[p<<1]+segt[p<<1|1]; 15 } 16 17 void update(int pnt,int key,int l,int r,int p){ 18 if(l==r){ 19 segt[p]+=key; 20 return; 21 } 22 demid; 23 if(pnt<=mid) update(pnt,key,lson); 24 else update(pnt,key,rson); 25 pushup(p); 26 } 27 28 int query(int L,int R,int l,int r,int p){ 29 if(L<=l && r<=R) return segt[p]; 30 demid; 31 int res=0; 32 if(L<=mid) res+=query(L,R,lson); 33 if(mid<R) res+=query(L,R,rson); 34 return res; 35 } 36 37 int main(){ 38 int t,cas=1; 39 cin>>t; 40 while(t--){ 41 int n,q; 42 cin>>n>>q; 43 memset(segt,0,sizeof(segt)); 44 for(int i=0;i<n;i++){ 45 int key; 46 scanf("%d",&key); 47 update(i,key,0,n-1,1); 48 } 49 printf("Case %d:\n",cas++); 50 while(q--){ 51 int key,i,j; 52 scanf("%d",&key); 53 if(key==1){ 54 scanf("%d",&i); 55 j=query(i,i,0,n-1,1); 56 update(i,-j,0,n-1,1); 57 printf("%d\n",j); 58 } 59 else if(key==2){ 60 scanf("%d%d",&i,&j); 61 update(i,j,0,n-1,1); 62 } 63 else{ 64 scanf("%d%d",&i,&j); 65 printf("%d\n",query(i,j,0,n-1,1)); 66 } 67 } 68 } 69 return 0; 70 }
线段树经典题。
给一个n的序列,有三种操作,包括查询。
简单讲一下线段树:
线段树利用二分的思想,把一段完整的区间分成两段,每段代表树上的一个节点,如此一直二分下去,知道段长度为1.
这样,原本需要on复杂度来维护的问题就可以在logn复杂度解决!
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 #define lson l,mid,p<<1 7 #define rson mid+1,r,p<<1|1 8 #define demid int mid=(l+r)>>1 9 #define N 100010 10 11 int segt[N<<2],col[N<<2]; 12 13 void pushup(int p){ 14 segt[p]=segt[p<<1]+segt[p<<1|1]; 15 } 16 17 void update(int pnt,int key,int l,int r,int p){ 18 if(l==r){ 19 segt[p]+=key; 20 return; 21 } 22 demid; 23 if(pnt<=mid) update(pnt,key,lson); 24 else update(pnt,key,rson); 25 pushup(p); 26 } 27 28 int query(int L,int R,int l,int r,int p){ 29 if(L<=l && r<=R) return segt[p]; 30 demid; 31 int res=0; 32 if(L<=mid) res+=query(L,R,lson); 33 if(mid<R) res+=query(L,R,rson); 34 return res; 35 } 36 37 int main(){ 38 int t,cas=1; 39 cin>>t; 40 while(t--){ 41 int n,q; 42 cin>>n>>q; 43 memset(segt,0,sizeof(segt)); 44 for(int i=0;i<n;i++){ 45 int key; 46 scanf("%d",&key); 47 update(i,key,0,n-1,1); 48 } 49 printf("Case %d:\n",cas++); 50 while(q--){ 51 int key,i,j; 52 scanf("%d",&key); 53 if(key==1){ 54 scanf("%d",&i); 55 j=query(i,i,0,n-1,1); 56 update(i,-j,0,n-1,1); 57 printf("%d\n",j); 58 } 59 else if(key==2){ 60 scanf("%d%d",&i,&j); 61 update(i,j,0,n-1,1); 62 } 63 else{ 64 scanf("%d%d",&i,&j); 65 printf("%d\n",query(i,j,0,n-1,1)); 66 } 67 } 68 } 69 return 0; 70 }
浙公网安备 33010602011771号