1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #include<bits/stdc++.h> #define Tp template<typename Ty> #define Ts template<typename Ty,typename... Ar> #define W while #define I inline #define RI register int #define LL long long #define Cn const #define CI Cn int& #define gc getchar #define D isdigit(c=gc()) #define pc(c) putchar((c)) #define min(x,y) ((x)<(y)?(x):(y)) #define max(x,y) ((x)>(y)?(x):(y)) using namespace std; namespace Debug{ Tp I void _debug(Cn char* f,Ty t){cerr<<f<<'='<<t<<endl;} Ts I void _debug(Cn char* f,Ty x,Ar... y){W(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);} Tp ostream& operator<<(ostream& os,Cn vector<Ty>& V){os<<"[";for(Cn auto& vv:V) os<<vv<<",";os<<"]";return os;} #define gdb(...) _debug(#__VA_ARGS__,__VA_ARGS__) }using namespace Debug; namespace FastIO{ Tp I void read(Ty& x){char c;int f=1;x=0;W(!D) f=c^'-'?1:-1;W(x=(x<<3)+(x<<1)+(c&15),D);x*=f;} Ts I void read(Ty& x,Ar&... y){read(x),read(y...);} Tp I void write(Ty x){x<0&&(pc('-'),x=-x,0),x<10?(pc(x+'0'),0):(write(x/10),pc(x%10+'0'),0);} Tp I void writeln(Cn Ty& x){write(x),pc('\n');} }using namespace FastIO; Cn int N=8e4+10; int Tt,n,m,k,a[N],b[N],fa[N],cnt,fir[N],nxt[N<<1],son[N<<1],tot,sz[N],g[N][20],rt[N],dep[N]; I int GC(){char c=gc();W(c^'Q'&&c^'L') c=gc();return c^'L';} I void Add(CI x,CI y){nxt[++tot]=fir[x],fir[x]=tot,son[tot]=y;} #define to son[i] class ChairmanTree{ private: int id; struct node{int l,r,v;}T[N*400]; #define mid (l+r>>1) public: I void B(int& x,CI p,CI l,CI r,CI v){ if(T[x=++id]=T[p],T[x].v++,l==r) return ; v<=mid?B(T[x].l,T[p].l,l,mid,v):B(T[x].r,T[p].r,mid+1,r,v); } I int Q(CI s1,CI s2,CI s3,CI s4,CI l,CI r,CI k){ if(l==r) return b[l];RI t=T[T[s1].l].v+T[T[s2].l].v-T[T[s3].l].v-T[T[s4].l].v; return k<=t?Q(T[s1].l,T[s2].l,T[s3].l,T[s4].l,l,mid,k):Q(T[s1].r,T[s2].r,T[s3].r,T[s4].r,mid+1,r,k-t); } }T; I void Dfs(CI x,CI f,CI tp){ RI i;for(g[x][0]=f,i=1;i<=18;i++) g[x][i]=g[g[x][i-1]][i-1]; for(T.B(rt[x],rt[f],1,cnt,a[x]),dep[x]=dep[f]+1,fa[x]=tp,sz[tp]++,i=fir[x];i;i=nxt[i]) to^f&&(Dfs(to,x,tp),0); } I int LCA(RI x,RI y){ RI i;for(dep[x]<dep[y]&&(swap(x,y),0),i=18;~i;i--) if(dep[y]<=dep[g[x][i]]&&(x=g[x][i]),x==y) return x; for(i=18;~i;i--) if(g[x][i]^g[y][i]) x=g[x][i],y=g[y][i];return g[x][0]; } int main(){ RI i,o,x,y,z,p=0;for(read(Tt,n,m,k),i=1;i<=n;i++) read(a[i]),b[++cnt]=a[i],fa[i]=i; for(i=1;i<=m;i++) read(x,y),Add(x,y),Add(y,x); for(sort(b+1,b+cnt+1),cnt=unique(b+1,b+cnt+1)-b-1,i=1;i<=n;i++) a[i]=lower_bound(b+1,b+cnt+1,a[i])-b; for(i=1;i<=n;i++) fa[i]==i&&(Dfs(i,0,i),0); for(i=1;i<=k;i++) (o=GC())?read(x,y,z),x^=p,y^=p,z^=p,writeln(p=T.Q(rt[x],rt[y],rt[LCA(x,y)],rt[g[LCA(x,y)][0]],1,cnt,z)): (read(x,y),x^=p,y^=p,Add(x,y),Add(y,x),sz[fa[x]]>sz[fa[y]]?Dfs(y,x,fa[x]):Dfs(x,y,fa[y]));return 0; }
|