-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimplementation.cpp
More file actions
42 lines (35 loc) · 975 Bytes
/
Copy pathimplementation.cpp
File metadata and controls
42 lines (35 loc) · 975 Bytes
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
#include<bits/stdc++.h>
using namespace std;
const int maxN = 2e5+10;
int n;
vector<int> tree(4*maxN);
//initially:
//v = 1 [first node of the tree]
//tl = 0 [first index of the range]
//tr = n-1 [last index of the range]
void build(vector<int>& a, int v, int tl, int tr){
if(tl == tr){
tree[v] = a[tl];
}
else{
int tm = (tl+tr)/2;
build(a,v*2,tl, tm);
build(a,v*2+1, tm+1, tr);
tree[v] = tree[v*2] + tree[v*2+1];
}
}
int sum(int v, int tl, int tr, int l, int r){
if(l>r) return 0;
if(l==tl and r==tr) return tree[v];
int tm = (tl+tr)/2;
return sum(v*2, tl, tm, l, min(r,tm)) + sum(v*2+1, tm+1, tr, max(l,tm+1), r);
}
void update(int v, int tl, int tr, int pos, int newVal){
if(tl == tr) tree[v] = newVal;
else{
int tm = (tl+tr)/2;
if(pos<=tm) update(v*2,tl, tm, pos,newVal);
else update(v*2+1,tm+1,tr,pos,newVal);
tree[v] = tree[v*2]+tree[v*2+1];
}
}