1. 程式人生 > >Codeforces 455 D. Serega and Fun

Codeforces 455 D. Serega and Fun

stat ++ span 兩種 program long long main osi stdin

$ >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;
}

Codeforces 455 D. Serega and Fun