Meaning of the questions:
To each of the x, y are wrong, you should restore them real (x + c)% n and (y + c)% n
Like a lot of practice, solution to a problem Y seniors posted here
However, I should be the goose that met the evaluation machine run very fast man! Persistable disjoint-set over Jesus.
If the algorithm out if I should choose to play offline algorithm (but I do not want to play lazy) posted can be persistent and check code set ~
#include<bits/stdc++.h> #define N 300002 using namespace std; int read() { int x=0,f=1;char s=getchar(); while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();} while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();} return x*f; } int angry=0; char s[3]; int c=0,n; int fa2[N],ndnum=0; int fa[N*20],lc[N*20],rc[N*20],root[N],dep[N*20]; void build(int k,int l,int r) { if(l==r){fa[k]=l;return;} int mid=(l+r)>>1; build(lc[k]=++ndnum,l,mid); build(rc[k]=++ndnum,mid+1,r); } int query(int k,int L,int R,int pos) { if(L==R) return k; int mid=(L+R)>>1; if(pos<=mid)return query(lc[k],L,mid,pos); else return query(rc[k],mid+1,R,pos); } void merge(int last,int &k,int L,int R,int pos,int f) { k=++ndnum;lc[k]=lc[last];rc[k]=rc[last];//复制 if(L==R) { fa[k]=f; dep[k]=dep[last]; return; } int mid=(L+R)>>1; if(pos<=mid)merge(lc[last],lc[k],L,mid,pos,f); else merge(rc[last],rc[k],mid+1,R,pos,f); } void update(int k,int L,int R,int pos) { if(L==R){dep[k]++;return;} int mid=(L+R)>>1; if(pos<=mid)update(lc[k],L,mid,pos); else update(rc[k],mid+1,R,pos); } int get(int k,int x) { int now=query(k,0,n-1,x); if(fa[now]==x)return now; return get(k,fa[now]); } int main() { freopen("history.in","r",stdin); freopen("history.out","w",stdout); n=read();int T=read(); int nian=0; build(root[0],0,n-1); for(int i=1;i<=T;++i) { scanf("%s",s); if(s[0]=='K')angry=0,c=read(); if(s[0]=='R') { nian++; int x=read(),y=read(); if(angry)x=(x+c)%n,y=(y+c)%n; root[nian]=root[nian-1]; int posx=get(root[nian],x); int posy=get(root[nian],y); if(fa[posx]!=fa[posy]) { if(dep[posx]>dep[posy])swap(posx,posy); merge(root[nian-1],root[nian],0,n-1,fa[posx],fa[posy]); if(dep[posx]==dep[posy])update(root[nian],0,n-1,fa[posy]); } } if(s[0]=='T') { int st=read(),ed=read(),t=read(); int f1=get(root[nian],st),f2=get(root[nian],ed); if(F1 = F2!) {Angry = R & lt 0. 1. 1 ; the printf ( " N \ n- " ); Continue ;} F1 = GET (the root [max ( 0 , Nian-T)], ST), F2 = GET (the root [max ( 0 , Nian-T)], ED ); // tips max (0, Nian-T) IF (FA [F1] == FA [F2]) = Angry . 1 , the printf ( " N \ n- " ); // time that the next output is N angry, the next output Y that is not angry the else angry = 0 , the printf ( " Y \ n- " ); } } } / * . 3. 7 T 0. 1. 1 K. 1 R & lt 0. 1 T 0 1 1 R 0 1 T 0 2 1 */