luogu P2633 Count on a tree
嘟嘟嘟
区间第\(k\)大扯到树上啦
但是不要慌,仍是不难,仔细想一下就行了。
主席树中的每一棵线段树维护的是一个前缀的答案。利用这一点,把前缀放到树上的话,就是每一个节点到根节点的答案。
所以主席树就建出来啦。
对于每一组询问\((x, y, k)\),令\(z = lca(x, y)\),则\((x, y)\)这条路径上的答案就可以拆成\(root[x] + root[y] - root[z] - root[fa(z)]\)四个主席树。然后就没啦。
刚开始我写成了减去\(root[z] * 2\),但这样就会把\(z\)的贡献减掉,也就WA了几发。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxt = 2e7 + 5;
const int maxn = 1e5 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, _n, m, a[maxn], b[maxn];
struct Edge
{
int nxt, to;
}e[maxn << 1];
int head[maxn], ecnt = -1;
void addEdge(int x, int y)
{
e[++ecnt] = (Edge){head[x], y};
head[x] = ecnt;
}
struct Tree
{
int ls, rs, sum;
}t[maxt];
int root[maxn], cnt = 0;
void insert(int old, int& now, int l, int r, int id)
{
t[now = ++cnt] = t[old];
t[now].sum++;
if(l == r) return;
int mid = (l + r) >> 1;
if(id <= mid) insert(t[old].ls, t[now].ls, l, mid, id);
else insert(t[old].rs, t[now].rs, mid + 1, r, id);
}
int query(int f, int z, int x, int y, int l, int r, int k)
{
if(l == r) return l;
int mid = (l + r) >> 1, Sum = t[t[x].ls].sum + t[t[y].ls].sum - t[t[z].ls].sum - t[t[f].ls].sum;
if(k <= Sum) return query(t[f].ls, t[z].ls, t[x].ls, t[y].ls, l, mid, k);
else return query(t[f].rs, t[z].rs, t[x].rs, t[y].rs, mid + 1, r, k - Sum);
}
int dep[maxn], fa[21][maxn];
void dfs(int now, int _f)
{
insert(root[_f], root[now], 1, _n, a[now]);
for(int i = 1; (1 << i) <= dep[now]; ++i)
fa[i][now] = fa[i - 1][fa[i - 1][now]];
for(int i = head[now], v; i != -1; i = e[i].nxt)
{
if((v = e[i].to) == _f) continue;
dep[v] = dep[now] + 1;
fa[0][v] = now;
dfs(v, now);
}
}
int lca(int x, int y)
{
if(dep[x] < dep[y]) swap(x, y);
for(int i = 20; i >= 0; --i)
if(dep[x] - (1 << i) >= dep[y]) x = fa[i][x];
if(x == y) return x;
for(int i = 20; i >= 0; --i)
if(fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y];
return fa[0][x];
}
int main()
{
Mem(head, -1);
n = read(); m = read();
for(int i = 1; i <= n; ++i) a[i] = b[i] = read();
sort(b + 1, b + n + 1);
_n = unique(b + 1, b + n + 1) - b - 1;
for(int i = 1; i <= n; ++i)
a[i] = lower_bound(b + 1, b + _n + 1, a[i]) - b;
for(int i = 1; i < n; ++i)
{
int x = read(), y = read();
addEdge(x, y); addEdge(y, x);
}
dfs(1, 0);
for(int i = 1, ans = 0; i <= m; ++i)
{
int x = read(), y = read(), k = read();
x = x ^ ans;
int z = lca(x, y);
ans = b[query(root[fa[0][z]], root[z], root[x], root[y], 1, _n, k)];
write(ans), enter;
}
return 0;
}