-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGao_on_a_tree.cpp
More file actions
188 lines (159 loc) · 3.92 KB
/
Gao_on_a_tree.cpp
File metadata and controls
188 lines (159 loc) · 3.92 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <bits/stdc++.h>
using namespace std;
// First you make it work, then you can always make it beautiful
class SegTree {
public:
int len;
vector<int> t;
SegTree() : len(0) {}
SegTree(int l) {
len = l;
t.assign(4 * len, -1);
}
void build(vector<int> &a, int v, int tl, int tr) {
if (tl == tr) {
t[v] = a[tl];
return;
}
int tm = (tl + tr) >> 1;
build(a, v << 1, tl, tm);
build(a, (v << 1) + 1, tm + 1, tr);
t[v] = max(t[v << 1], t[(v << 1) + 1]);
}
void update(int v, int tl, int tr, int id, int val) {
if (tl == id && tr == id) {
t[v] = val;
return;
}
if (id < tl || id > tr) return;
int tm = (tl + tr) >> 1;
update(v << 1, tl, tm, id, val);
update((v << 1) + 1, tm + 1, tr, id, val);
t[v] = max(t[v << 1], t[(v << 1) + 1]);
}
int query(int v, int tl, int tr, int l, int r) {
if (tl > r || tr < l) return -1;
if (l <= tl && tr <= r) return t[v];
int tm = (tl + tr) >> 1;
int left = query(v << 1, tl, tm, l, r);
int right = query((v << 1) + 1, tm + 1, tr, l, r);
return max(left, right);
}
void build(vector<int> &a) {
build(a, 1, 0, len - 1);
}
void update(int id, int val) {
update(1, 0, len - 1, id, val);
}
int query(int l, int r) {
return query(1, 0, len - 1, l, r);
}
};
SegTree sg;
const int N = 1e5 + 5;
vector<int> adj[N];
vector<array<int, 3>> qry[N];
int val[N];
int sz[N];
int depth[N];
int par[N];
int top[N];
int heavy[N];
int pos[N];
int curr_pos;
void dfs(int node, int p, int d) {
depth[node] = d;
sz[node] = 1;
int maxi = 0;
par[node] = p;
for(auto &j : adj[node]) if(j != p) {
dfs(j, node, d + 1);
sz[node] += sz[j];
if(sz[j] > maxi) {
maxi = sz[j];
heavy[node] = j;
}
}
}
void decompose(int node, int t) {
top[node] = t;
pos[node] = curr_pos++;
if(heavy[node] != -1) {
decompose(heavy[node], t);
}
for(auto &j : adj[node]) if(j != par[node] && j != heavy[node]) {
decompose(j, j);
}
}
int query(int a, int b) {
int res = -1;
for (; top[a] != top[b]; b = par[top[b]]) {
if(depth[top[a]] > depth[top[b]]) swap(a, b);
int mx = sg.query(pos[top[b]], pos[b]);
res = max(res, mx);
}
if(depth[a] > depth[b]) swap(a, b);
int mx = sg.query(pos[a], pos[b]);
res = max(res, mx);
return res;
}
void solve() {
int n, q;
while((cin >> n >> q)) {
for(int i=0;i<=n;i++) {
adj[i].clear();
qry[i].clear();
heavy[i] = -1;
}
for(int i=1;i<=n;i++) cin >> val[i];
for(int i=0;i<n-1;i++) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
curr_pos = 0;
for(int i=0;i<=n;i++) {
heavy[i] = -1;
}
dfs(1, 0, 0);
decompose(1, 1);
sg = SegTree(n);
for(int i=0;i<q;i++) {
int a, b, c;
cin >> a >> b >> c;
qry[c].push_back({a, b, i});
}
vector<int> nodes(n);
iota(nodes.begin(), nodes.end(), 1);
sort(nodes.begin(), nodes.end(), [&](auto &a, auto &b) {
return val[a] < val[b];
});
vector<int> ans(q);
int ptr = 0;
for(int i=0;i<=n;i++) {
if(qry[i].empty()) continue;
while(ptr < n && val[nodes[ptr]] <= i) {
sg.update(pos[nodes[ptr]], val[nodes[ptr]]);
ptr++;
}
for(auto &[a, b, id] : qry[i]) {
int mx = query(a, b);
if(mx == i) {
ans[id] = 1;
}
}
}
for(int i=0;i<q;i++) {
if(ans[i]) cout << "Find\n";
else cout << "NotFind\n";
}
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}