-
Notifications
You must be signed in to change notification settings - Fork 0
/
1112 - Curious Robin Hood.cpp
111 lines (100 loc) · 2.8 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
106
107
108
109
110
111
#include<bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define max_index 100005
using namespace std;
int tree[max_index*4];
int arr[max_index];
void segment_tree_init(int node, int start, int end){
if(start == end){
tree[node] = arr[start];
return ;
}
int left = node*2;
int right = (node*2) + 1;
int mid = (start+end)/2;
segment_tree_init(left,start,mid);
segment_tree_init(right,mid+1,end);
tree[node] = tree[left] + tree[right];
}
int segment_tree_query(int node, int start, int end, int q_start, int q_end){
if(q_start>end || q_end<start){
return 0;
}
if(q_start<=start && q_end>=end){
return tree[node];
}
int left = (node*2);
int right = (node*2)+1;
int mid = (start+end)/2;
int q1 = segment_tree_query(left,start,mid,q_start,q_end);
int q2 = segment_tree_query(right,mid+1,end,q_start,q_end);
return q1+q2;
}
void segment_tree_update(int node, int start, int end, int index, int new_value){
if(index<start || index>end){
return ;
}
if(index <=start && index >= end){
tree[node] += new_value;
return ;
}
int left = (node*2);
int right = (node*2)+1;
int mid = (start+end)/2;
segment_tree_update(left,start,mid,index,new_value);
segment_tree_update(right,mid+1,end,index,new_value);
tree[node] = tree[left] + tree[right];
}
void segment_tree_update_set_0(int node, int start, int end, int index, int new_value){
if(index<start || index>end){
return ;
}
if(index <=start && index >= end){
tree[node] = new_value;
return ;
}
int left = (node*2);
int right = (node*2)+1;
int mid = (start+end)/2;
segment_tree_update_set_0(left,start,mid,index,new_value);
segment_tree_update_set_0(right,mid+1,end,index,new_value);
tree[node] = tree[left] + tree[right];
}
int main()
{
int q,t,x,y,n,m,i,j,query,data=1,d;
//freopen("output2.txt","w",stdout);
scanf("%d",&q);
t=1;
while(q--){
scanf("%d%d",&n,&query);
for(i=1; i<=n; i++){
scanf("%d",&arr[i]);
}
segment_tree_init(1,1,n);
printf("Case %d:\n",t++);
while(query--){
scanf("%d",&d);
if(d==1){
scanf("%d",&x);
x++;
printf("%d\n",segment_tree_query(1,1,n,x,x));
segment_tree_update_set_0(1,1,n,x,0);
}
else if(d==2){
scanf("%d%d",&x,&y);
x++;
segment_tree_update(1,1,n,x,y);
}
else{
scanf("%d%d",&x,&y);
x++;
y++;
printf("%d\n",segment_tree_query(1,1,n,x,y));
}
}
}
return 0;
}