-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler_tour_technique.cpp
73 lines (63 loc) · 1.46 KB
/
euler_tour_technique.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
73
// BOJ 2820 자동차 공장
#include <iostream>
#include <bits/stdc++.h>
#define sz size()
#define bk back()
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAX = 500005;
int N, Q, idx, A[MAX], in[MAX], out[MAX];
ll tree[4 * MAX];
vector<int> graph[MAX];
void dfs(int cur) {
in[cur] = ++idx;
for (int nxt: graph[cur])
dfs(nxt);
out[cur] = idx;
}
void range(int idx, int s, int e, int l, int r, ll v) {
if (r < s || e < l)
return;
if (l <= s && e <= r) {
tree[idx] += v;
return;
}
int m = (s + e) >> 1;
range(2 * idx, s, m, l, r, v);
range(2 * idx + 1, m + 1, e, l, r, v);
}
ll point(int idx, int s, int e, int l) {
if (l < s || e < l)
return 0;
if (s == e)
return tree[idx];
int m = (s + e) >> 1;
return tree[idx] + point(2 * idx, s, m, l) + point(2 * idx + 1, m + 1, e, l);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> N >> Q >> A[1];
for (int i = 2; i <= N; i++) {
int p;
cin >> A[i] >> p;
graph[p].push_back(i);
}
dfs(1);
while (Q--) {
char q;
cin >> q;
if (q == 'p') {
int a, x;
cin >> a >> x;
range(1, 1, N, in[a] + 1, out[a], x);
} else {
int a;
cin >> a;
cout << point(1, 1, N, in[a]) + A[a] << '\n';
}
}
}