-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathABC406F.cpp
More file actions
80 lines (80 loc) · 2 KB
/
ABC406F.cpp
File metadata and controls
80 lines (80 loc) · 2 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
71
72
73
74
75
76
77
78
79
80
/*
* @FilePath: \c++\ABC406F.cpp
* @Author: WRT_Partisan
* @Date: 2025-10-04 17:37:00
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef unsigned long long ull;
#define mid ((l + r) >> 1)
struct Edge
{
int u, v;
} e[300005];
int n, u, v, q, op, w[300005], sum, d[300005], s[300005], cnt, id[300005];
vector<int> mp[300005];
struct Node
{
int s[1200005];
void pushup(int x) { s[x] = s[x << 1] + s[x << 1 | 1]; }
void build(int x, int l, int r)
{
if (l == r)
return s[x] = 1, void();
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
pushup(x);
}
int ask(int x, int l, int r, int ql, int qr)
{
if (ql <= l && r <= qr)
return s[x];
int res = 0;
if (ql <= mid)
res += ask(x << 1, l, mid, ql, qr);
if (mid < qr)
res += ask(x << 1 | 1, mid + 1, r, ql, qr);
return res;
}
void update(int x, int l, int r, int p, int u)
{
if (l == r)
return s[x] += u, void();
if (p <= mid)
update(x << 1, l, mid, p, u);
else
update(x << 1 | 1, mid + 1, r, p, u);
pushup(x);
}
} tree;
void dfs(int x, int f)
{
id[x] = ++cnt, d[x] = d[f] + 1, s[x] = 1;
for (auto i : mp[x])
if (i != f)
dfs(i, x), s[x] += s[i];
}
signed main()
{
cin.tie(0)->sync_with_stdio(0);
cin >> n, sum = n;
fill(w + 1, w + n + 1, 1);
for (int i = 1; i < n; i++)
cin >> e[i].u >> e[i].v, mp[e[i].u].push_back(e[i].v), mp[e[i].v].push_back(e[i].u);
dfs(1, 0);
tree.build(1, 1, cnt);
for (int i = 1; i < n; i++)
if (d[e[i].u] < d[e[i].v])
swap(e[i].u, e[i].v);
cin >> q;
while (q--)
{
cin >> op >> u;
if (op == 1)
cin >> v, tree.update(1, 1, cnt, id[u], v), sum += v;
else
cout << abs(tree.ask(1, 1, cnt, id[e[u].u], id[e[u].u] + s[e[u].u] - 1) * 2 - sum) << "\n";
}
return 0;
}