codeforces 455 D. Serega and Fun (分块+双向链表)

D. Serega and Fun
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Serega loves fun. However, everyone has fun in the unique manner. Serega has fun by solving query problems. One day Fedor came up with such a problem.

You are given an array a consisting of n positive integers and queries to it. The queries can be of two types:

  1. Make a unit cyclic shift to the right on the segment from l to r (both borders inclusive). That is rearrange elements of the array in the following manner:
    a[l], a[l + 1], ..., a[r - 1], a[r] → a[r], a[l], a[l + 1], ..., a[r - 1].
  2. Count how many numbers equal to k are on the segment from l to r (both borders inclusive).

Fedor hurried to see Serega enjoy the problem and Serega solved it really quickly. Let's see, can you solve it?

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements of the array. The second line contains n integersa[1], a[2], ..., a[n] (1 ≤ a[i] ≤ n).

The third line contains a single integer q (1 ≤ q ≤ 105) — the number of queries. The next q lines contain the queries.

As you need to respond to the queries online, the queries will be encoded. A query of the first type will be given in format: 1 l'i r'i. A query of the second type will be given in format: 2 l'i r'i k'i. All the number in input are integer. They satisfy the constraints:1 ≤ l'i, r'i, k'i ≤ n.

To decode the queries from the data given in input, you need to perform the following transformations:

li = ((l'i + lastans - 1) mod n) + 1; ri = ((r'i + lastans - 1) mod n) + 1; ki = ((k'i + lastans - 1) mod n) + 1.

Where lastans is the last reply to the query of the 2-nd type (initially, lastans = 0). If after transformation li is greater than ri, you must swap these values.

Output

For each query of the 2-nd type print the answer on a single line.

Examples
input
7
6 6 2 7 4 2 5
7
1 3 6
2 2 4 2
2 2 4 7
2 2 2 5
1 2 6
1 1 4
2 1 7 3
output
2
1
0
0
input
8
8 4 2 2 7 7 8 8
8
1 8 8
2 8 1 7
1 8 1
1 7 3
2 8 8 3
1 1 4
1 2 7
1 4 5
output
2
0


题解:分块+双向链表

对于每个块维护块头的元素

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 100003
using namespace std;
int m,n,block;
int num[320][N],l[N],r[N],nxt[N],pre[N],belong[N];
int val[N];
void build(int x)
{
	for (int i=l[x];i<=r[x];i++) num[x][val[i]]++;
}
int find(int x,int pos)
{
	int now=l[pos];
	while (x) {
		x--;
		now=nxt[now];
	}
	return now; 
}
int main()
{
	freopen("a.in","r",stdin);
	freopen("my.out","w",stdout);
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d",&val[i]),pre[i]=i-1,nxt[i]=i+1;
    block=sqrt(n);
	int t1=ceil(n*1.0/block);
	for (int i=1;i<=n;i++) belong[i]=(i-1)/block+1;
	for (int i=1;i<=t1;i++) l[i]=n+1,r[i]=0;
	for (int i=1;i<=n;i++) {
		int x=belong[i];
		l[x]=min(l[x],i);
		r[x]=max(r[x],i);
	}
	for (int i=1;i<=t1;i++) build(i);
	scanf("%d",&m); int size=0;
	for (int j=1;j<=m;j++) {
		int opt,x,y,k,t; 
		scanf("%d%d%d",&opt,&x,&y);
		x=(x+size-1)%n+1; y=(y+size-1)%n+1;
		if (x>y) swap(x,y);
		int b1=(x-1)/block+1;  int b2=(y-1)/block+1;
		x=find(x-(b1-1)*block-1,b1); y=find(y-(b2-1)*block-1,b2);
	//	cout<<x<<" "<<y<<endl;
		if (opt==1) {
			if (x==y) continue;
			int lx=l[b1]; int ly=l[b2];
			if (lx==x) l[b1]=y;
			for (int i=b2;i>=b1+1;i--){
				l[i]=pre[l[i]];
				num[i-1][val[l[i]]]--;
				num[i][val[l[i]]]++;
			}
			num[b2][val[y]]--; num[b1][val[y]]++;
			t=pre[y]; nxt[t]=nxt[y]; pre[nxt[y]]=t;
			t=pre[x]; nxt[t]=y; pre[y]=t; 
			nxt[y]=x; pre[x]=y;
		}
		else {
			scanf("%d",&k);  k=(k+size-1)%n+1;
			size=0;
			if (b1==b2) {
				for (int i=x;i!=y;i=nxt[i])
				 if (val[i]==k) size++;
				if (val[y]==k) size++;
			    printf("%d\n",size);
			    continue;
			}
			int rx=find(min(block,n-(b1-1)*block)-1,b1);
			for (int i=x;i!=rx;i=nxt[i])
			 if (val[i]==k) size++;
			if (val[rx]==k) size++;
			for (int i=l[b2];i!=y;i=nxt[i])
			 if (val[i]==k) size++;
			if (val[y]==k) size++;
			for (int i=b1+1;i<=b2-1;i++) size+=num[i][k];
			printf("%d\n",size);
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值