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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
| #include<cstdio> #include<cstring> #include<algorithm> using namespace std;
const int N=1e5+10,BL=320,MOD=500009; #define ci int
inline int Read(){ register int r=0,c=getchar(); while(c<'0' || '9'<c) c=getchar(); while('0'<=c && c<='9') r=(r<<1)+(r<<3)+(c^'0'),c=getchar(); return r; }
struct HASHTABLE{ int val[N],key[N],nxt[N],head[MOD],pol[N+3],npol; HASHTABLE(){for(int i=1;i<N;i++) pol[++npol]=i;} void Modify(ci id,ci num,ci del){ int nkey=id*N+num,nt=nkey%MOD,las=-1; for(int it=head[nt];it;it=nxt[it]) if(key[it]==nkey){ val[it]+=del; if(!val[it]){ pol[++npol]=it; if(~las) nxt[las]=nxt[it]; else head[nt]=nxt[it]; } return; } else las=it; if(del>0){ int now=pol[npol--]; nxt[now]=head[nt],key[now]=nkey,val[now]=del; head[nt]=now; } } int Query(ci id,ci num){ int nkey=id*N+num,nt=nkey%MOD; for(int it=head[nt];it;it=nxt[it]) if(key[it]==nkey) return val[it]; return 0; } }Ha;
struct NODE{int key,nxt,pre;}nod[N+2*BL]; int nnod,n,m,q;
struct LIST{ int st,ed,siz,id; void Build(ci i){ siz=0,id=i; st=++nnod,ed=++nnod; nod[st].nxt=ed,nod[ed].pre=st; } void Insert(ci now,ci pre){ int nxt=nod[pre].nxt; nod[pre].nxt=nod[nxt].pre=now; nod[now].pre=pre,nod[now].nxt=nxt; Ha.Modify(id,nod[now].key,1); siz++; } void Delete(ci now){ nod[nod[now].pre].nxt=nod[now].nxt; nod[nod[now].nxt].pre=nod[now].pre; Ha.Modify(id,nod[now].key,-1); siz--; } void PushFront(ci now){Insert(now,st);} void PushBack(ci now){Insert(now,nod[ed].pre);} void PopBack(){ if(!siz) return; Delete(nod[ed].pre); } void PopFront(){ if(!siz) return; Delete(nod[st].nxt); } int Back(){return siz? nod[ed].pre:0;} void Print(){ int it=nod[st].nxt;printf("!"); while(it!=ed){ printf(" %d",nod[it].key); it=nod[it].nxt; } printf("\n"); } }Li[BL];
void Find(int pos,int &A,int &B){ A=1;B=0; while(Li[A].siz<pos) pos-=Li[A++].siz; B=Li[A].st; while(pos--) B=nod[B].nxt; } int main(){ nnod=n=Read(),m=n/BL+bool(n%BL); for(int i=1;i<=m;i++) Li[i].Build(i); for(int i=1;i<=n;i++){ nod[i].key=Read(); Li[i/BL+bool(i%BL)].PushBack(i); } q=Read(); int lasans=0; while(q--){ int cmd=Read(),L=(Read()+lasans-1)%n+1,R=(Read()+lasans-1)%n+1; if(L>R) swap(L,R); int Lblk,Lpos,Rblk,Rpos; Find(L,Lblk,Lpos),Find(R,Rblk,Rpos); if(cmd==1){ if(Lblk==Rblk){ Li[Lblk].Delete(Rpos); Li[Lblk].Insert(Rpos,nod[Lpos].pre); } else{ Li[Rblk].Delete(Rpos); Li[Lblk].Insert(Rpos,nod[Lpos].pre); for(int i=Lblk+1;i<=Rblk;i++){ int it=Li[i-1].Back(); Li[i-1].PopBack(); Li[i].PushFront(it); } } } else{ int key=(Read()+lasans-1)%n+1; lasans=0; if(Lblk==Rblk){ for(int it=Lpos;it!=Rpos;it=nod[it].nxt) lasans+=nod[it].key==key; lasans+=nod[Rpos].key==key; } else{ for(int it=Lpos;it!=Li[Lblk].ed;it=nod[it].nxt) lasans+=nod[it].key==key; for(int it=Rpos;it!=Li[Rblk].st;it=nod[it].pre) lasans+=nod[it].key==key; for(int i=Lblk+1;i<Rblk;i++) lasans+=Ha.Query(i,key); } printf("%d\n",lasans); } } return 0; }
|