-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1112 - Curious Robin Hood.cpp
105 lines (101 loc) · 2.18 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <bits/stdc++.h>
using namespace std;
#define mx 100005
int arr[mx];
int tree[mx * 3];
void init (int node , int b , int e);
int query(int node , int b , int e , int i , int j);
void update (int node , int b , int e , int i , int value);
int main()
{
int tes , o = 0;
scanf("%d", &tes);
while(tes--)
{
o++;
int q ,n;
scanf("%d %d", &n , &q);
for(int i = 0; i < n; i++)
{
scanf(" %d", &arr[i]);
}
init(1, 0 , n - 1);
printf("Case %d:\n", o);
while(q--)
{
int ck;
scanf("%d", &ck);
if(ck == 1)
{
int i;
scanf("%d", &i);
printf("%d\n", arr[i]);
arr[i] = 0;
update(1, 0 , n - 1 , i , arr[i]);
}
else if(ck == 2)
{
int v, i;
scanf("%d %d", &i , &v);
arr[i] += v;
update(1 , 0 , n - 1 , i , arr[i]);
}
else
{
int i , j;
scanf("%d %d", &i , &j);
printf("%d\n", query(1, 0 , n - 1, i , j));
}
}
}
}
void init(int node , int b , int e)
{
if(b == e)
{
tree[node] = arr[b];
return;
}
int l , m , r;
l = node * 2;
r = node * 2 + 1;
m = (b + e )/ 2;
init(l , b , m);
init(r, m +1 , e);
tree[node] = tree[l] + tree[r];
}
int query(int node, int b , int e , int i , int j)
{
if(i > e or j < b)
return 0;
if(b >= i && e <= j)
{
return tree[node];
}
int l ,m , r;
l = node * 2;
r = node * 2 + 1;
m = (b + e) / 2;
int x , y;
x = query(l , b , m, i , j);
y = query(r , m + 1 , e ,i , j);
return x + y;
}
void update(int node , int b , int e, int i , int value)
{
if(i > e or i < b){
return;
}
if(b >= i && e <= i)
{
tree[node] = value;
return;
}
int l , m , r;
l = node * 2;
r = node * 2 + 1;
m = (b + e) / 2;
update(l , b, m , i , value);
update(r, m + 1, e, i , value);
tree[node] = tree[l] + tree[r];
}