kruskal重构树本质就是给并查集显式建树来替代可持久化并查集。将边按困难度从小到大排序后建出该树,按dfs序建主席树即可。查询时跳到深度最浅的满足在该重要度下已被合并的点,在子树内查询第k大。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
#define N 200010
#define M 500010
int n,m,q,a[N],root[N],value[N],fa[N],p[N],size[N],dfn[N],id[N],f[N][],lastans,tot,cnt=,t=;
struct data
{
int x,y,z;
bool operator <(const data&a) const
{
return z<a.z;
}
}e[M];
struct data2{int to,nxt;
}edge[N];
struct data3{int l,r,x;
}tree[N<<];
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;}
void dfs(int k)
{
size[k]=;dfn[++cnt]=k;id[k]=cnt;
for (int i=p[k];i;i=edge[i].nxt)
{
dfs(edge[i].to);
size[k]+=size[edge[i].to];
}
}
void ins(int &k,int l,int r,int x)
{
tree[++cnt]=tree[k],k=cnt;tree[k].x++;
if (l==r) return;
int mid=l+r>>;
if (x<=mid) ins(tree[k].l,l,mid,x);
else ins(tree[k].r,mid+,r,x);
}
int query(int x,int y,int l,int r,int p)
{
if (!y) return -;
if (l==r) return p<=tree[y].x-tree[x].x?l:-;
int mid=l+r>>;
if (p<=tree[tree[y].r].x-tree[tree[x].r].x) return query(tree[x].r,tree[y].r,mid+,r,p);
else return query(tree[x].l,tree[y].l,l,mid,p-tree[tree[y].r].x+tree[tree[x].r].x);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj3551.in","r",stdin);
freopen("bzoj3551.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read(),m=read(),q=read();
for (int i=;i<=n;i++) a[i]=read();
for (int i=;i<=m;i++) e[i].x=read(),e[i].y=read(),e[i].z=read();
sort(e+,e+m+);
for (int i=;i<=n*;i++) fa[i]=i;tot=n;
for (int i=;i<=m;i++)
if (find(e[i].x)!=find(e[i].y))
{
value[++tot]=e[i].z;
addedge(tot,find(e[i].x)),addedge(tot,find(e[i].y));
f[find(e[i].x)][]=tot,f[find(e[i].y)][]=tot;
fa[find(e[i].x)]=tot,fa[find(e[i].y)]=tot;
}
for (int i=;i<=tot;i++) if (!f[i][]) f[i][]=i;
for (int j=;j<=;j++)
for (int i=;i<=tot;i++)
f[i][j]=f[f[i][j-]][j-];
for (int i=;i<=tot;i++)
if (f[i][]==i) dfs(i);
cnt=;
for (int i=;i<=tot;i++)
{
root[i]=root[i-];
ins(root[i],-,1E9,dfn[i]>n?-:a[dfn[i]]);
}
while (q--)
{
int x=read(),y=read(),z=read();
if (~lastans) x^=lastans,y^=lastans,z^=lastans;
for (int j=;~j;j--) if (value[f[x][j]]<=y) x=f[x][j];
lastans=query(root[id[x]-],root[id[x]+size[x]-],-,1E9,z);
printf("%d\n",lastans);
}
return ;
}