-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpersist_segtree.cpp
More file actions
70 lines (62 loc) · 1.67 KB
/
persist_segtree.cpp
File metadata and controls
70 lines (62 loc) · 1.67 KB
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
// Persist Segment Tree solution on PSEGTREE on SPOJ
#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 17;
struct node
{
int val,left,right;
}tree[N*100];
int n,q,res[N],root[N],cnt,ver;
int clone(int x){ tree[++cnt] = tree[x]; return cnt; }
void build(int l,int r,int idx)
{
cnt = max(cnt,idx);
if(l==r){ tree[idx] = {res[l],0,0}; return; }
int m = (l+r)/2;
build(l,m,idx*2),build(m+1,r,idx*2+1);
tree[idx].left = idx*2,tree[idx].right = idx*2+1;
tree[idx].val = tree[idx*2].val+tree[idx*2+1].val;
}
int update(int l,int r,int idx,int x,int k)
{
int now = clone(idx);
if(l==r){ tree[now].val+=k; return now; }
int m = (l+r)/2;
if(x<=m) tree[now].left = update(l,m,tree[idx].left,x,k);
else tree[now].right = update(m+1,r,tree[idx].right,x,k);
tree[now].val = tree[tree[now].left].val+tree[tree[now].right].val;
return now;
}
int read(int l,int r,int idx,int x,int y)
{
if(x>r or y<l) return 0;
if(x<=l and y>=r) return tree[idx].val;
int m = (l+r)/2;
return read(l,m,tree[idx].left,x,y)+read(m+1,r,tree[idx].right,x,y);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n;
for(int i = 1;i <= n;i++) cin >> res[i];
build(1,n,1);
root[0] = 1;
cin >> q;
while(q--)
{
int t;
cin >> t;
if(t==1)
{
int id,pos,k;
cin >> id >> pos >> k;
root[++ver] = update(1,n,root[id],pos,k);
}
else
{
int id,l,r;
cin >> id >> l >> r;
cout << read(1,n,root[id],l,r) << '\n';
}
}
}