-
Notifications
You must be signed in to change notification settings - Fork 0
/
1112 - Curious Robin Hood.cpp
91 lines (78 loc) · 2.09 KB
/
1112 - Curious Robin Hood.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <bits/stdc++.h>
using namespace std;
const int Max = 100001;
int arr[Max], tree[Max<<2];
void build (int at, int st, int ed)
{
if(st == ed) {
tree[at] = arr[st];
return;
}
int lft = at * 2;
int rgt = lft + 1;
int mid = (st + ed)/2;
build(lft, st, mid);
build(rgt, mid + 1, ed);
tree[at] = tree[lft] + tree[rgt];
}
void update (int at, int st, int ed, int pos, int u)
{
if(st == ed) {
tree[at] += u;// use tree[at] += u if value is increased by u;
return;
}
int lft = at*2;
int rgt = lft + 1;
int mid = (st + ed)/2;
if(pos <= mid)
update(lft, st, mid, pos, u);
else
update(rgt, mid + 1, ed, pos, u);
tree[at] = tree[lft] + tree[rgt];
}
int query(int at, int st, int ed, int l, int r)
{
if(r < st || l > ed)
return 0;
if(l<=st && r>=ed)//( l <= L && R <= r )
return tree[at];
int lft = at*2;
int rgt = lft + 1;
int mid = (st + ed)/2;
int sumL = query(lft, st, mid, l, r);
int sumR = query(rgt, mid + 1, ed, l, r);
return sumL + sumR;
}
int main()
{
freopen("1112.txt","r",stdin);
int n,q,t,l,r,op,v,idx;
scanf("%d",&t);
for(int j = 1; j<=t; j++) {
scanf("%d %d",&n,&q);
for(int i = 1; i<=n; i++)
scanf("%d",&arr[i]);
build(1, 1, n);
printf("Case %d:\n",j);
for(int i = 1; i<=q; i++) {
scanf("%d",&op);
if(op == 1) {
scanf("%d",&idx);
update(1,1,n,idx + 1,-arr[idx + 1]);
printf("%d\n",arr[idx + 1]);
arr[idx + 1] = 0;
}
else if(op == 2) {
scanf("%d %d",&idx,&v);
update(1,1,n,idx + 1,v);
arr[idx + 1] += v;
}
else {
scanf("%d %d",&l,&r);
int res = query(1,1,n,l+1,r+1);
printf("%d\n",res);
}
}
}
return 0;
}