离线和在线一样做…
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
char c;
inline void read(int &a){a=0;do c=getchar();while(c<'0'||c>'9');while(c<='9'&&c>='0')a=(a<<3)+(a<<1)+c-'0',c=getchar();}
struct Node
{
Node*lc,*rc;
int size;
Node(){lc=NULL,rc=NULL,size=0;}
}*Empty;
Node*Modify(Node *Old,int l,int L,int R)
{
Node *N=new Node;
if(Old)*N=*Old;
else N->size=0,N->lc=N->rc=NULL;
N->size++;
if(L==R)return N;
if(l<=(L+R>>1))
N->lc=Modify(N->lc,l,L,L+R>>1);
else N->rc=Modify(N->rc,l,(L+R>>1)+1,R);
if(!N->lc)N->lc=Empty;
if(!N->rc)N->rc=Empty;
return N;
}
int Query(Node *Ol,Node *Ne,int k,int L,int R)
{
if(L==R)return L;
if(!Ol->lc)Ol=Empty;
if(Ne->rc->size-Ol->rc->size>=k)return Query(Ol->rc,Ne->rc,k,(L+R>>1)+1,R);
return Query(Ol->lc,Ne->lc,k+Ol->rc->size-Ne->rc->size,L,L+R>>1);
}
int f[200005];
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
int tot;
struct Chain
{
Chain *next;int u;
}*Head[200005];
int Bg[200005],Ed[200005];
Node *T[200005];
int Cnt;
int H[200005];
void Build(int u)
{
Bg[u]=++Cnt;
if(H[u])
T[Cnt]=Modify(T[Cnt-1],H[u],0,2e9);
else T[Cnt]=T[Cnt-1];
for(Chain *tp=Head[u];tp;tp=tp->next)Build(tp->u);
Ed[u]=Cnt;
}
struct Side{int len,u,v;friend bool operator <(Side a,Side b){return a.len<b.len;}}L[500005];
int n,m;
int F[200005][18];
int Max[200005][18];
int M[200005];
void Add(int u,int v)
{Chain *tp=new Chain;tp->u=v;tp->next=Head[u];Head[u]=tp;}
void DFS(int u)
{
if(u==10142)
u++,u--;
for(int i=1;i<=17;i++)F[u][i]=F[F[u][i-1]][i-1]
,Max[u][i]=max(Max[u][i-1],
Max[F[u][i-1]][i-1]);
for(Chain *tp=Head[u];tp;tp=tp->next)F[tp->u][0]=u,Max[tp->u][0]=M[u],DFS(tp->u);
}
int find(int u,int M)
{
int v=u,j=17;
while(~j)
{
if(Max[v][j]<=M)v=F[v][j];
j--;
}
return v;
}
int main()
{
Empty=new Node;
Empty->lc=Empty->rc=Empty;
Empty->size=0;
int Q;
read(n);read(m);read(Q);
for(int i=1;i<=n;i++)read(H[i]);
tot=n;
for(int i=1;i<=n<<1;i++)f[i]=i;
for(int i=1;i<=m;i++)read(L[i].u),read(L[i].v),read(L[i].len);
sort(L+1,L+1+m);
for(int i=1;i<=m;i++)
{
if(find(L[i].u)^find(L[i].v))
Add(tot+1,f[L[i].u]),Add(tot+1,f[L[i].v]),M[f[f[L[i].v]]=f[f[L[i].u]]=tot+1]=L[i].len,tot++;
}
T[0]=new Node;//(1,1<<29);
tot++;
while(--tot)
if(!F[tot][0])
F[tot][0]=tot,Max[tot][0]=1<<29,DFS(tot),Build(tot);
int lastans=0,v,x,k;
while(Q--)
{
if(lastans==-1)lastans=0;
read(v),read(x),read(k);
// v^=lastans,x^=lastans,k^=lastans;
int u=find(v,x);
if(T[Ed[u]]->size-T[Bg[u]-1]->size<k)
printf("%d\n",lastans=-1);
else
{
lastans=Query(T[Bg[u]-1],T[Ed[u]],k,0,2e9);
printf("%d\n",lastans);
}
}
return 0;
}