-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersistence Array
More file actions
42 lines (39 loc) · 1.06 KB
/
Persistence Array
File metadata and controls
42 lines (39 loc) · 1.06 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
struct Node{
int x;
int l, r;
Node* left;
Node* right;
Node(int x = 0, int l = 0, int r = 0) : x(x), l(l), r(r) {
this -> left = nullptr;
this -> right = nullptr;
}
};
Node* update(Node* v, int pos, int val){
// cout << v -> l << " " << v -> r << " " << pos << endl;
if (v -> l > pos || v -> r <= pos)
return v;
Node* u = new Node(0, v -> l, v -> r);
u -> left = v -> left, u -> right = v -> right;
if (u -> l == pos && u -> r == pos + 1){
u -> x = val;
return u;
}
u -> left = update(u -> left, pos, val);
u -> right = update(u -> right, pos, val);
return u;
}
int get(Node* v, int pos){
if (v -> l > pos || v -> r <= pos)
return 0;
if (v -> l == pos && v -> r == pos + 1)
return v -> x;
return get(v -> left, pos) + get(v -> right, pos);
}
Node* build(Node* v){
if (v -> r - v -> l == 1)
return v;
int m = (v -> l + v -> r) / 2;
v -> left = build(new Node(0, v -> l, m));
v -> right = build(new Node(0, m, v -> r));
return v;
}