-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1737.cpp
72 lines (66 loc) · 1.54 KB
/
1737.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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
struct Node {
ll val;
Node *left = nullptr, *right = nullptr;
};
int n, q, cnt, x[N];
Node* root[N];
Node* build(int l, int r) {
Node *res = new Node;
if (l == r) {
res->val = x[l];
return res;
}
int mid = (l + r) >> 1;
res->left = build(l, mid);
res->right = build(mid + 1, r);
res->val = res->left->val + res->right->val;
return res;
}
Node* update(Node* old, int l, int r, int u, int x) {
Node *res = new Node;
if (l == r) {
res->val = x;
return res;
}
int mid = (l + r) >> 1;
if (u <= mid) {
res->left = update(old->left, l, mid, u, x);
res->right = old->right;
} else {
res->left = old->left;
res->right = update(old->right, mid + 1, r, u, x);
}
res->val = res->left->val + res->right->val;
return res;
}
ll get(Node* cur, int l, int r, int u, int v) {
if (v < l || r < u) return 0;
if (u <= l && r <= v) return cur->val;
int mid = (l + r) >> 1;
return get(cur->left, l, mid, u, v) + get(cur->right, mid + 1, r, u, v);
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> x[i];
}
root[cnt = 1] = build(1, n);
while (q--) {
int t, k; cin >> t >> k;
if (t == 1) {
int a, x; cin >> a >> x;
root[k] = update(root[k], 1, n, a, x);
} else if (t == 2) {
int a, b; cin >> a >> b;
cout << get(root[k], 1, n, a, b) << "\n";
} else {
root[++cnt] = root[k];
}
}
return 0;
}