Kruskal to reconstruct the tree.
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#defineMAXV 200050#defineMaxe 2000050#defineINF 2147483647using namespacestd;structedges{intU,v,w;} Es[maxe];structedge{intV,NXT;} E[maxe];intn,m,q,h[maxv],hash[maxv],x,y,z,lastans=0, val[maxv],g[maxv],nume=0, FATHER[MAXV];intdis[maxv],l[maxv],r[maxv],fdfn[maxv],anc[maxv][ +],times=0;introot[maxv],tot=0, tots=0, ls[maxv* at],rs[maxv* at],sum[maxv* at],len;BOOLcmp (edges x,edges y) {returnx.w<Y.W;}intRead () {CharChintData=0; while(ch<'0'|| Ch>'9') ch=GetChar (); while(ch>='0'&& ch<='9') {Data=data*Ten+ch-'0'; CH=GetChar (); } returndata;}voidAddedge (intUintv) {e[++nume].v=v; E[NUME].NXT=G[u]; G[u]=Nume;}intGetfather (intx) { if(father[x]!=x) father[x]=Getfather (father[x]); returnfather[x];}voidKruskal_tree () {Sort (es+1, es+m+1, CMP); for(intI=1; i<=2*n;i++) father[i]=i; for(intI=1; i<=m;i++) { intu=es[i].u,v=es[i].v,w=ES[I].W; intF1=getfather (es[i].u), f2=Getfather (ES[I].V); if(f1!=F2) {Tot++; FATHER[F1]=tot;father[f2]=tot;val[tot]=ES[I].W; Addedge (F1,tot); Addedge (TOT,F1); Addedge (F2,tot); Addedge (TOT,F2); } }}voidDfsintx) { intflag=0; l[x]=inf;r[x]=0; for(intI=g[x];i;i=e[i].nxt) { intv=e[i].v; if(anc[x][0]!=v) {flag=1; anc[v][0]=x;dis[v]=dis[x]+1; DFS (v); L[X]=min (L[x],l[v]); r[x]=Max (R[x],r[v]); } } if(!flag) {L[x]=r[x]=++Times ; Fdfn[times]=x; }}voidget_table () { for(intE=1; e<= -; e++) for(intI=1; i<=tot;i++) Anc[i][e]=anc[anc[i][e-1]][e-1];}intBndintx) { returnLower_bound (hash+1, hash+len+1, X)-Hash;}voidInsertintLastint&now,intLeftintRightintPOS) { Now=++tots;sum[now]=sum[last]+1; if(Left==right)return; Ls[now]=ls[last];rs[now]=Rs[last]; intMid=left+right>>1; if(pos<=mid) Insert (LS[LAST],LS[NOW],LEFT,MID,POS); ElseInsert (rs[last],rs[now],mid+1, Right,pos);}voidbuild_seg () {sort (hash+1, hash+n+1); Len=unique (hash+1, hash+n+1)-hash-1; for(intI=1; i<=n;i++) Insert (Root[i-1],root[i],1, Len,bnd (H[fdfn[i]));}intFindintXintk) { for(intE= -; e>=0; e--) { if((val[anc[x][e]]<=k) &&(Anc[x][e])) x=Anc[x][e]; } returnx;}intAskintLastintNowintLeftintRightintk) { if(Left==right)returnHash[left]; intMid=left+right>>1; intr=sum[rs[now]]-Sum[rs[last]]; if(r>=k)returnAsk (rs[last],rs[now],mid+1, right,k); Else returnAsk (ls[last],ls[now],left,mid,k-r);}voidWork () {x=read (); Y=read (); z=read (); X^=lastans;y^=lastans;z^=Lastans; intk=find (x, y); if(r[k]-l[k]+1<z) {printf ("-1\n"); lastans=0;} Else{Lastans=ask (root[l[k]-1],root[r[k]],1, len,z); printf ("%d\n", Lastans); }}intMain () {n=read (); M=read (); Q=read (); tot=N; for(intI=1; i<=n;i++) {h[i]=read (); hash[i]=h[i];} for(intI=1; i<=m;i++) {es[i].u=read (); Es[i].v=read (); es[i].w=read ();} Kruskal_tree (); DFS (TOT); Get_table (); Build_seg (); for(intI=1; i<=q;i++) work (); return 0;}
Bzoj 3545/bzoj 3551 Peaks