- #include <bits/stdc++.h>
- #define C 100003
- using namespace std;
- int Sack[C], Tree[C*3];
- void build(int b, int e, int node){
- if(b >= e){
- if(b == e)Tree[node] = Sack[b];
- return;
- }
- int mid = (b+e) >> 1, internal1 = (node << 1), internal2 = internal1+1;
- build(b, mid, internal1);
- build(mid+1, e, internal2);
- Tree[node] = Tree[internal1] + Tree[internal2];
- }
- void update(int b, int e, int node, int I){
- if(b > I || e < I)return;
- if(b >= e){
- if(b == e)Tree[node] = Sack[b];
- return;
- }
- int mid = (b+e) >> 1, internal1 = (node << 1), internal2 = internal1+1;
- update(b, mid, internal1, I);
- update(mid+1, e, internal2, I);
- Tree[node] = Tree[internal1] + Tree[internal2];
- }
- int query(int b, int e, int node, int I, int J){
- if(b >= I && e <= J)return Tree[node];
- if(b > J || e < I)return 0;
- if(b > e)return 0;
- int mid = (b+e) >> 1, internal1 = (node << 1), internal2 = internal1+1;
- int x = query(b, mid, internal1, I, J);
- int y = query(mid+1, e, internal2, I, J);
- return x+y;
- }
- int main(){
- int T, I, J, V, N, Q, F, CS = 0;
- scanf("%d", &T);
- while(T--){
- scanf("%d %d", &N, &Q);
- for(int i = 0; i < N; i++)scanf("%d", &Sack[i]);
- memset(Tree, 0, sizeof(Tree));
- build(0, N-1, 1);
- printf("Case %d:\n", ++CS);
- for(int i = 1; i <= Q; i++){
- scanf("%d", &F);
- if(F == 1){
- scanf("%d", &I);
- printf("%d\n", Sack[I]);
- Sack[I] = 0;
- update(0, N-1, 1, I);
- }
- if(F == 2){
- scanf("%d %d", &I, &V);
- Sack[I] += V;
- update(0, N-1, 1, I);
- }
- if(F == 3){
- scanf("%d %d", &I, &J);
- printf("%d\n", query(0, N-1, 1, I, J));
- }
- }
- }
- return 0;
- }
Saturday, June 6, 2015
LightOJ - 1112 - Curious Robin Hood
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment