您的位置:首页 > 其它

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

2017-01-19 17:11 337 查看
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:

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].

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]
,l
,r
,nxt
,pre
,belong
;
int val
;
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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: