Codeforces 455 D. Serega and Fun

$ >Codeforces \space 455 D. Serega and Fun<$

题目大意 :
你有一个长度为 \(n\) 的序列,要支持两种操作

  1. 给出 \(l, r\)\(a_{i+1}\) 变成 \(a_i\)\(a_l\) 变成 \(a_r, i\in[l, r)\)

  2. 给出 \(l, r, x\) ,求区间 \([l, r]\)\(x\) 的出现次数

\(1 \leq n, m \leq 10^5\)

解题思路 :


/*program by mangoyang*/
#pragma GCC optimize("Ofast","O3", "inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
     
inline char read() {
    static const int IN_LEN = 200000;
    static char buf[IN_LEN], *s, *t;
    return (s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin), (s == t ? -1 : *s++) : *s++);
}
template<class T>
inline void read(T &x) {
    static bool iosig;
    static char c;
    for (iosig = false, c = read(); !isdigit(c); c = read()) {
        if (c == '-') iosig = true;
        if (c == -1) return;
    }
    for (x = 0; isdigit(c); c = read()) x = ((x + (x << 2)) << 1) + (c ^ '0');
    if (iosig) x = -x;
}
const int OUT_LEN = 500000;
char obuf[OUT_LEN], *ooh = obuf;
inline void print(char c) {
    if (ooh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), ooh = obuf;
    *ooh++ = c;
}
template<class T>
inline void print(T x) {
    static int buf[30], cnt;
    if (x == 0) print('0');
    else {
        if (x < 0) print('-'), x = -x;
        for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
        while (cnt) print((char)buf[cnt--]);
    }
}
inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); }
const int N = 100005;
int a[N], s[N], n, m, lastans;
int main(){
    read(n);
    for(register int i = 1; i <= n; ++i) read(a[i]), s[a[i]]++;
    register int *p; read(m);
    for(register int i = 1, j, op, x, y, z, tmp, res; i <= m; ++i){
        read(op), read(x), read(y);
        x = (x + lastans - 1) % n + 1, y = (y + lastans - 1) % n + 1;
        if(x > y) swap(x, y);
        if(op & 1){
            p = a + y, tmp = *p, j = y - x;
            while(j) *p = *(p-1), --p, --j; *p = tmp;
        }
        else{
            read(z), z = (z + lastans - 1) % n + 1, res = 0;
            if(y - x + 1<= n / 2){
                p = a + x - 1;
                for(j = x; j + 8 <= y; j += 8) {
                    res += (*(++p) == z), res += (*(++p) == z);
                    res += (*(++p) == z), res += (*(++p) == z);
                    res += (*(++p) == z), res += (*(++p) == z);
                    res += (*(++p) == z), res += (*(++p) == z);
                }
                for(; j <= y; j++) res += (*(++p) == z);
                print(lastans = res), print('\n');
            }
            else{
                p = a;
                for(j = 1; j + 8 < x; j += 8){
                    res += (*(++p) == z), res += (*(++p) == z);
                    res += (*(++p) == z), res += (*(++p) == z);
                    res += (*(++p) == z), res += (*(++p) == z);
                    res += (*(++p) == z), res += (*(++p) == z);
                }
                for(; j < x; j++) res += (*(++p) == z);
                p = a + y;
                for(j = y + 1; j + 8 <= n; j += 8){
                    res += (*(++p) == z), res += (*(++p) == z);
                    res += (*(++p) == z), res += (*(++p) == z);
                    res += (*(++p) == z), res += (*(++p) == z);
                    res += (*(++p) == z), res += (*(++p) == z);
                }
                for(; j <= n; j++) res += (*(++p) == z);
                print(lastans = s[z] - res), print('\n');
            }
        }
    }
    return flush(), 0;
}

猜你喜欢

转载自www.cnblogs.com/mangoyang/p/9839265.html
今日推荐