Codeforces 455D-- Serega and Fun

标算应该是分块+链表。但链表写起来很烦,经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);
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值