-
Notifications
You must be signed in to change notification settings - Fork 0
/
heavy_light_decomposition.cpp
158 lines (118 loc) · 3.64 KB
/
heavy_light_decomposition.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// BOJ 13510 트리와 쿼리 1
#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;
struct SegmentTree {
vector<int> tree;
SegmentTree(int n) { tree.resize(4 * n); }
int init(int idx, int s, int e, vector<int> &v) {
if (s == e)
return tree[idx] = v[s];
int m = (s + e) >> 1;
return tree[idx] = max(init(2 * idx, s, m, v), init(2 * idx + 1, m + 1, e, v));
}
int maximum(int idx, int s, int e, int l, int r) {
if (r < s || e < l)
return 0;
if (l <= s && e <= r)
return tree[idx];
int m = (s + e) >> 1;
return max(maximum(2 * idx, s, m, l, r), maximum(2 * idx + 1, m + 1, e, l, r));
}
void update(int idx, int s, int e, int l, int k) {
if (l < s || e < l)
return;
if (s == e) {
tree[idx] = k;
return;
}
int m = (s + e) >> 1;
update(2 * idx, s, m, l, k);
update(2 * idx + 1, m + 1, e, l, k);
tree[idx] = max(tree[2 * idx], tree[2 * idx + 1]);
}
};
void dfs1(int cur, vector<vector<pii>> &graph, vector<int> &parent, vector<int> &sub, vector<int> &depth) {
sub[cur] = 1;
for (pii &nxt : graph[cur]) {
if (sub[nxt.fi])
continue;
parent[nxt.fi] = cur;
depth[nxt.fi] = depth[cur] + 1;
dfs1(nxt.fi, graph, parent, sub, depth);
sub[cur] += sub[nxt.fi];
if (sub[graph[cur][0].fi] < sub[nxt.fi])
swap(graph[cur][0], nxt);
}
}
void dfs2(int cur, vector<vector<pii>> &graph, vector<int> &parent, vector<int> &weight, vector<int> &top, vector<int> &in, vector<int> &out, int &idx) {
in[cur] = ++idx;
for (pii &nxt : graph[cur]) {
if (nxt.fi == parent[cur])
continue;
if (nxt.fi == graph[cur][0].fi)
top[nxt.fi] = top[cur];
else
top[nxt.fi] = nxt.fi;
dfs2(nxt.fi, graph, parent, weight, top, in, out, idx);
weight[in[nxt.fi]] = nxt.se;
}
out[cur] = idx;
}
int query(int a, int b, vector<int> &parent, vector<int> &depth, vector<int> &top, vector<int> &in, SegmentTree &st, int n) {
int ret = 0;
while (top[a] != top[b]) {
if (depth[top[a]] > depth[top[b]])
swap(a, b);
ret = max(ret, st.maximum(1, 1, n, in[top[b]], in[b]));
b = parent[top[b]];
}
if (depth[a] > depth[b])
swap(a, b);
return max(ret, st.maximum(1, 1, n, in[a] + 1, in[b]));
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<pii> edges(n);
vector<vector<pii>> graph(n + 1);
for (int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
edges[i] = {u, v};
graph[u].push_back({v, w});
graph[v].push_back({u, w});
}
vector<int> parent(n + 1);
vector<int> sub(n + 1);
vector<int> depth(n + 1);
dfs1(1, graph, parent, sub, depth);
vector<int> weight(n + 1);
vector<int> top(n + 1);
vector<int> in(n + 1);
vector<int> out(n + 1);
top[1] = 1;
int idx = 0;
dfs2(1, graph, parent, weight, top, in, out, idx);
SegmentTree st(n);
st.init(1, 1, n, weight);
int q;
cin >> q;
while (q--) {
int op, u, v;
cin >> op >> u >> v;
if (op == 1) {
int lower = (depth[edges[u].fi] > depth[edges[u].se] ? edges[u].fi : edges[u].se);
st.update(1, 1, n, in[lower], v);
} else
cout << query(u, v, parent, depth, top, in, st, n) << '\n';
}
}