-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
SQRT_DECOMPOSITION.cpp
64 lines (55 loc) · 1.23 KB
/
SQRT_DECOMPOSITION.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
vector<int> arr, input_arr,blocks;
vector<int> parent,size;
int block_size;
void SQRT_update(int index, int val){
int blk = index/block_size;
blocks[blk] += (val - input_arr[index]);
input_arr[index] = val;
}
int SQRT_query(int l, int r){
int sum = 0;
while (l<r and l%block_size!=0 && l!=0)
{
sum += input_arr[l];
l++;
}
while (l+block_size <= r)
{
sum += blocks[l/block_size];
l += block_size;
}
while (l<=r)
{
sum += input_arr[l];
l++;
}
return sum;
}
void build_SQRT_DECOMPED_TREE(int n){
block_size = ceil(sqrt(n));
blocks.resize(block_size,0);
int index_of_block = -1;
rep(i,0,n){
if(i % block_size==0){
index_of_block++;
}
blocks [index_of_block] += input_arr[i];
}
}
int32_t main(){
IOS;
int n,q,x,y,z;
cin >> n;
arr.resize(n,0);
input_arr.resize(n,0);
rep(i,0,n)cin>>input_arr[i];
build_SQRT_DECOMPED_TREE(n);
cin >> q;
while(q--){
cin >> x >> y >> z;
if(x==1){
SQRT_update(y,z);
}else cout << SQRT_query(y,z) <<endl;
}
return 0;
}