codeforces 455D Serega and Fun

18 篇文章 0 订阅

http://www.elijahqi.win/2018/03/02/codeforces-455d/
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 integers a[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

Copy
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

Copy
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
设sum[i][x]表示在i中第K种颜色的数量是多少 那么我sqrt(n)一分块 每次 整块暴力统计 如果有旋转操作 相当于也是只做sqrt块即可

对于这个其实应该用双端链表来搞比较好 每次遍历 块大小的复杂度 找到 删除 插入 但是我一开始比较偷懒写的vector 于是块大小需要sqrt(n)*log2(n)才可以过 因为vector工作原理是每次申请一段连续的空间 如果要删除就整体复制过去 常数很大啊..

#include<cmath>
#include<vector>
#include<cstdio>
#include<algorithm>
#define N 100010
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=gc();}
    while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int n,s[350][N],ans,left[350],right[350],a[N],b[N],nn,m;
vector<int> block[350];
inline void change(int l,int r){
//  printf("%d %d\n",l,r);
    int v=*(block[b[l]].end()-1),pre=v;
    int v1=block[b[r]][r-left[b[r]]];
    if (b[l]==b[r]){
        block[b[l]].erase(block[b[r]].begin()+r-left[b[r]]);
        block[b[l]].insert(block[b[l]].begin()+l-left[b[l]],v1);
        return;
    }
    --s[b[l]][v];++s[b[l]][v1];
    //vector<int>:: iterator it=block[b[l]].end();
    block[b[l]].erase(block[b[l]].end()-1);
    block[b[l]].insert(block[b[l]].begin()+l-left[b[l]],v1);
    for (int i=b[l]+1;i<=b[r]-1;++i){
        --s[i][*(block[i].end()-1)];++s[i][pre];int pp=block[i][block[i].size()-1];
        block[i].erase(block[i].end()-1);
        block[i].insert(block[i].begin(),pre);pre=pp;
    }
    --s[b[r]][v1];++s[b[r]][pre];
    block[b[r]].erase(block[b[r]].begin()+r-left[b[r]]);
    block[b[r]].insert(block[b[r]].begin(),pre);
/*  for (int i=1;i<=n/nn+1;++i) 
        for (int j=0;j<block[i].size();++j) printf("%d ",block[i][j]);puts("");*/
        /*

    for (int i=1;i<=n/nn+1;++i){
        for (int j=1;j<=n;++j) printf("%d:%d\n",j,s[i][j]);
    }*/
}
inline int query(int l,int r,int k){
    int tmp=0;
    if (b[l]==b[r]) {for (int i=l;i<=r;++i) tmp+=(block[b[l]][i-left[b[l]]]==k);return tmp;}
    for (int i=l;i<=right[b[l]];++i) tmp+=(block[b[l]][i-left[b[l]]]==k);
    for (int i=left[b[r]];i<=r;++i) tmp+=(block[b[r]][i-left[b[r]]]==k);
    for (int i=b[l]+1;i<=b[r]-1;++i) tmp+=s[i][k];return tmp;
}
int main(){
//  freopen("cf455d.in","r",stdin);
//  freopen("cf.out","w",stdout); 
    n=read();nn=sqrt(n)*log2(n);
    for (int i=1;i<=n/nn+1;++i) left[i]=(i-1)*nn+1,right[i]=i*nn; left[n/nn+1]=(n/nn)*nn+1;right[n/nn+1]=n;
//  for (int i=1;i<=n/nn+1;++i) printf("%d %d\n",left[i],right[i]);
    for (int i=1;i<=n;++i){
        a[i]=read();b[i]=(i-1)/nn+1;++s[b[i]][a[i]];block[b[i]].push_back(a[i]);
    }m=read();
    //
    while(m--){
        int op=read(),l1=(read()+ans-1)%n+1,r1=(read()+ans-1)%n+1,k;
        if(l1>r1) swap(l1,r1);
        if (op==1) change(l1,r1);
        if (op==2) k=(read()+ans-1)%n+1,printf("%d\n",ans=query(l1,r1,k));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值