标算应该是分块+链表。但链表写起来很烦,经yn大佬指点学会用deque,原来以为deque就是一个可以访问开头的queue,但今天用了之后发现功能吊炸天,可以像vector一样访问下表,删除,插入,强到飞起。
#pragma GCC optimize(3,"inline","Ofast")
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+1000;
const int B=350;
void read(int &x)
{
char c=getchar();x=0;bool f=0;
while(!isdigit(c))f|=(c=='-'),c=getchar();
while(isdigit(c))x=x*10+c-48,c=getchar();
if(f)x=-x;
}
void read(ll &x)
{
char c=getchar();x=0;bool f=0;
while(!isdigit(c))f|=(c=='-'),c=getchar();
while(isdigit(c))x=x*10+c-48,c=getchar();
if(f)x=-x;
}
int n,m,a[N],ld[B+10],rd[B+10],belong[N],sz;
int vis[B+10][N];
deque<int>hav[B+10];
int ask(int l,int r,int k)
{
int L=belong[l],R=belong[r],res=0,tp;
if(L!=R)
{
for(int i=L+1;i<R;i++)
res+=vis[i][k];
tp=ld[L];
for(int i=l;i<=rd[L];i++)
res+=(hav[L][i-tp]==k);
tp=ld[R];
for(int i=ld[R];i<=r;i++)
res+=(hav[R][i-tp]==k);
}
else
{
tp=ld[L];
for(int i=l;i<=r;i++)
res+=(hav[L][i-tp]==k);
}
return res;
}
void cg(int l,int r)
{
if(l==r)return;
int L=belong[l],R=belong[r],tp,ps;
if(L!=R)
{
ps=ld[R],ps=r-ps;
tp=hav[R][ps];
vis[R][tp]--;
vis[L][tp]++;
hav[R].erase(hav[R].begin()+ps);
ps=ld[L];
ps=l-ps;
hav[L].insert(hav[L].begin()+ps,tp);
for(int i=L;i<R;i++)
{
tp=hav[i].back();
hav[i].pop_back();
vis[i][tp]--;
vis[i+1][tp]++;
hav[i+1].push_front(tp);
}
}
else
{
ps=ld[L];
tp=hav[L][r-ps];
hav[L].erase(hav[L].begin()+(r-ps));
hav[L].insert(hav[L].begin()+(l-ps),tp);
}
}
int main()
{
int opt,las=0;
ll l,r,k;
read(n);
for(int i=1;i<=n;i++)
read(a[i]);
sz=n/B;
if(n%B)sz++;
for(int i=1;i<=sz;i++)
ld[i]=B*(i-1)+1,rd[i]=B*i;
rd[sz]=n;
for(int i=1;i<=sz;i++)
for(int j=ld[i];j<=rd[i];j++)
vis[i][a[j]]++,belong[j]=i,hav[i].push_back(a[j]);
read(m);
while(m--)
{
read(opt);
if(opt==2)
{
read(l),read(r),read(k);
l=(l+las-1)%n+1,r=(r+las-1)%n+1,k=(k+las-1)%n+1;
if(l>r)swap(l,r);
las=ask(l,r,k);
printf("%d\n",las);
}
else
{
read(l),read(r);
l=(l+las-1)%n+1,r=(r+las-1)%n+1,k=(k+las-1)%n+1;
if(l>r)swap(l,r);
cg(l,r);
}
}
}