$\Large \color{rgb(0,255,255)}Describe$

树上莫队 + 值域分块,复杂度$\Theta(n\sqrt n+nlogn)$

我们把一个盘子看做是两个点,然后当在莫队同时加入这个两个点时,我们就相应的把这个盘子加入值域分块中,删除同理。用一个$vector$维护每个点上带着的盘子。

值域分块的$trick$和这个题一样,查询$\sqrt n$,加入删除都是$\Theta(1)$的,和莫队的复杂度摊掉了,然后看到$1e9$的值域很显然是要离散化,然后这题差不多就做完了。

本蒟蒻做的时候$LCA$写错调了好久,希望各位不要犯这种睿智错误,$LCA$用的是$\Theta(nlogn)/\Theta(1)$$LCA$

$\Large Code:$

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
189
190
191
192
193
#pragma region revive
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define inl inline
#define re register int
#define fa(x) t[x].fa
#define son(x, y) t[x].child[y]
#define ls(x) t[x].child[0]
#define rs(x) t[x].child[1]
#define ll long long
const int inf = 0x3f3f3f3f;
#define lowbit(x) ((x) & (-x))
using namespace std;
#ifndef _DEBUG
#define getchar() (*(IOB.in.p++))
#define putchar(c) (*(IOB.out.p++) = (c))
#define io_eof() (IOB.in.p >= IOB.in.pend)
struct IOBUF {
struct {
char buff[1 << 26], *p, *pend;
} in;
struct {
char buff[1 << 26], *p;
} out;
IOBUF() {
in.p = in.buff;
out.p = out.buff;
in.pend = in.buff + fread(in.buff, 1, 1 << 26, stdin);
}
~IOBUF() { fwrite(out.buff, 1, out.p - out.buff, stdout); }
} IOB;
#endif
template <typename IO>
inl void write(IO x) {
if (x == 0) return (void)putchar('0');
if (x < 0) putchar('-'), x = -x;
static char buf[30];
char *p = buf;
while (x) {
*(p++) = x % 10 + '0';
x /= 10;
}
while (p > buf)
putchar(*(--p));
}
inl void writestr(const char *s) {
while (*s != 0)
putchar(*(s++));
}
template <typename IO>
inl void writeln(IO x) { write(x), putchar('\n'); }
template <typename IO>
inl void writesp(IO x) { write(x), putchar(' '); }
inl int readstr(char *s) {
char *begin = s, c = getchar();
while (c < 33 || c > 127) {
c = getchar();
}
while (c >= 33 && c <= 127) {
*(s++) = c;
c = getchar();
}
*s = 0;
return s - begin;
}
template <typename IO>
inl IO read() {
IO x = 0;
register bool w = 0;
register char c = getchar();
while (c > '9' || c < '0') {
if (c == '-') w = 1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
return w ? -x : x;
}
#pragma endregion
int f[100001][20], dep[50001], st[50001], ed[50001], o[100001], euler[100001], head[50001], mp[100001], lg[100001], len, tot, num, a[100001], b[100001], t[100001];
vector<int> ve[40001];
struct edge {
int next, to;
} e[100001];
inl void adde(int x, int y) {
e[++tot] = edge{ head[x], y }, head[x] = tot;
e[++tot] = edge{ head[y], x }, head[y] = tot;
}
inl void dfs(int x, int fa) {
euler[++euler[0]] = x, o[++len] = x, st[x] = euler[0], mp[x] = len, dep[x] = dep[fa] + 1;
for (re i = head[x]; i; i = e[i].next) {
if (e[i].to != fa) {
dfs(e[i].to, x);
o[++len] = x;
}
}
euler[++euler[0]] = x, ed[x] = euler[0];
}
inl int lca(int l, int r) {
l = mp[l], r = mp[r];
re x = min(l, r), y = max(l, r), k = lg[y - x + 1];
l = x, r = y;
return dep[f[l][k]] < dep[f[r - (1 << k) + 1][k]] ? f[l][k] : f[r - (1 << k) + 1][k];
}
int s[100001], sum[1001], sz[1001], cnt[100001], buc[100001], ans[100001];
inl void init() {
dfs(1, 0);
for (re i = 2; i <= len; i++)lg[i] = lg[i >> 1] + 1;
for (re i = 1; i <= len; i++)f[i][0] = o[i];
for (re j = 1; j <= lg[len]; j++) {
for (re i = 1; i + (1 << j) - 1 <= len; i++) {
f[i][j] = dep[f[i][j - 1]] < dep[f[i + (1 << (j - 1))][j - 1]] ? f[i][j - 1] : f[i + (1 << (j - 1))][j - 1];
}
}
num = sqrt(euler[0]);
for (re i = 1; i <= euler[0]; i++)s[i] = (i - 1) / num + 1, sz[s[i]]++;
}
bool vis[100001];
struct quiz {
int l, r, k, lca, id;
bool operator<(const quiz &a) {
return s[l] == s[a.l] ? s[l] & 1 ? r < a.r : r > a.r : s[l] < s[a.l];
}
} q[100001];
inl void add(int x) {
for (auto i : ve[x])((++buc[i]) == 2) ? (cnt[a[i]]++, sum[s[a[i]]]++) : 0;
}
inl void del(int x) {
for (auto i : ve[x])(buc[i] == 2) ? (cnt[a[i]]--, buc[i]--, sum[s[a[i]]]--) : (buc[i]--);
}
inl void oper(int x) {
(vis[x] ^= 1) ? add(x) : del(x);
}
signed main() {
re n = read<int>(), p = read<int>(), m = read<int>(), x, y, w, f;
for (re i = 1; i < n; i++) x = read<int>(), y = read<int>(), adde(x, y);
init();
for (re i = 1; i <= p; i++) {
x = read<int>(), y = read<int>(), w = read<int>();
ve[x].push_back(i), ve[y].push_back(i), a[i] = b[i] = w;
}
sort(b + 1, b + 1 + p);
re siz = unique(b + 1, b + 1 + p) - b - 1;
for (re i = 1; i <= p; i++) {
re k = a[i];
t[a[i] = lower_bound(b + 1, b + 1 + siz, a[i]) - b] = k;
}
for (re i = 1; i <= m; i++) {
x = read<int>(), y = read<int>(), w = read<int>();
if (st[x] > st[y]) swap(x, y);
f = lca(x, y);
if (x != f) q[i] = quiz{ ed[x], st[y], w, f, i };
else q[i] = quiz{ st[x], st[y], w, 0, i };
}
sort(q + 1, q + 1 + m);
re l = 1, r = 0, res = 0;
for (re i = 1; i <= m; i++) {
while (l < q[i].l)oper(euler[l++]);
while (l > q[i].l)oper(euler[--l]);
while (r < q[i].r)oper(euler[++r]);
while (r > q[i].r)oper(euler[r--]);
if (q[i].lca) oper(q[i].lca);
res = 0;
for (re j = 1; j <= s[siz]; j++) {
if (res + sum[j] < q[i].k)res += sum[j];
else {
for (re l = (j - 1) * num + 1, r = l + sz[j] - 1; l <= r; l++) {
if ((res += cnt[l]) >= q[i].k) {
ans[q[i].id] = t[l];
goto begin;
}
}
}
}
begin:
if (q[i].lca)oper(q[i].lca);
}
for (re i = 1; i <= m; i++)writeln(ans[i]);
}