就是把静态区间第 K 小问题搬到了树上。。。可以先做一下 主席树模板。
我们知道主席树本质上是一棵权值线段树,具有可减性。
然后用树上差分那一套就行了,两点间查询时的权值线段树是 (rt_u+rt_v-rt_{ ext{LCA}(u, v)} - rt_{fa_{ ext{LCA(u, v)}}})。
代码有点长。。。
#include <bits/stdc++.h>
#define DEBUG fprintf(stderr, "Passing [%s] line %d
", __FUNCTION__, __LINE__)
#define File(x) freopen(x".in","r",stdin); freopen(x".out","w",stdout)
using namespace std;
typedef long long LL;
typedef pair <int, int> PII;
typedef pair <int, PII> PIII;
template <typename T>
inline T gi()
{
T f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
const int INF = 0x3f3f3f3f, N = 100003, M = N << 1;
int n, m;
int a[N], b[N], lst, tott;
int tot, head[N], ver[M], nxt[M];
struct Node {int ls, rs, cnt;} tr[N * 40];
int rt[N * 40];
int sz[N], son[N], dep[N], fa[N], topp[N];
inline void add(int u, int v) {ver[++tot] = v, nxt[tot] = head[u], head[u] = tot;}
struct LCA
{
void dfs1(int u, int f)
{
fa[u] = f, sz[u] = 1, dep[u] = dep[f] + 1;
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i];
if (v == f) continue;
dfs1(v, u);
sz[u] += sz[v];
if (sz[v] > sz[son[u]]) son[u] = v;
}
}
void dfs2(int u, int f)
{
topp[u] = f;
if (!son[u]) return;
dfs2(son[u], f);
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i];
if (v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
}
int getLCA(int u, int v)
{
while (topp[u] != topp[v])
{
if (dep[topp[u]] < dep[topp[v]]) swap(u, v);
u = fa[topp[u]];
}
if (dep[u] < dep[v]) return u;
return v;
}
} lca;
struct ChairmanTree
{
int build(int l, int r)
{
int p = ++tott;
if (l == r) return p;
int mid = (l + r) >> 1;
tr[p].ls = build(l, mid);
tr[p].rs = build(mid + 1, r);
return p;
}
inline void pushup(int p)
{
tr[p].cnt = tr[tr[p].ls].cnt + tr[tr[p].rs].cnt;
}
int modify(int p, int val, int l, int r)
{
int q = ++tott; tr[q] = tr[p];
if (l == r) {++tr[q].cnt; return q;}
int mid = (l + r) >> 1;
if (val <= mid) tr[q].ls = modify(tr[p].ls, val, l, mid);
else tr[q].rs = modify(tr[p].rs, val, mid + 1, r);
pushup(q);
return q;
}
int query(int p, int q, int rr, int s, int k, int l, int r)
{
if (l == r) return l;
int lcnt = tr[tr[p].ls].cnt + tr[tr[q].ls].cnt - tr[tr[rr].ls].cnt - tr[tr[s].ls].cnt;
int mid = (l + r) >> 1;
if (k <= lcnt) return query(tr[p].ls, tr[q].ls, tr[rr].ls, tr[s].ls, k, l, mid);
else return query(tr[p].rs, tr[q].rs, tr[rr].rs, tr[s].rs, k - lcnt, mid + 1, r);
}
} ct;
void dfs(int u, int f)
{
rt[u] = ct.modify(rt[fa[u]], a[u], 1, n);
for (int i = head[u]; i; i = nxt[i])
{
int v = ver[i];
if (v == f) continue;
dfs(v, u);
}
}
int main()
{
//File("");
n = gi <int> (), m = gi <int> ();
for (int i = 1; i <= n; i+=1) a[i] = b[i] = gi <int> ();
sort(b + 1, b + 1 + n);
int cntt = unique(b + 1, b + 1 + n) - b;
for (int i = 1; i <= n; i+=1)
a[i] = lower_bound(b + 1, b + 1 + cntt, a[i]) - b;
for (int i = 1; i < n; i+=1)
{
int u = gi <int> (), v = gi <int> ();
add(u, v), add(v, u);
}
lca.dfs1(1, 0); lca.dfs2(1, 1);
rt[0] = ct.build(1, n);
dfs(1, 0);
while (m--)
{
int u = gi <int> () ^ lst, v = gi <int> (), k = gi <int> ();
int l = lca.getLCA(u, v);
printf("%d
", lst = b[ct.query(rt[u], rt[v], rt[l], rt[fa[l]], k, 1, n)]);
}
return 0;
}