NOI模拟(5.4) CQOID2T3 异或序列

异或序列

题目背景:

5.4 模拟 CQOI2018D2T3  

分析:莫队

 

莫队裸题······直接求前缀和,那么相当于一段区间里面sum[x - 1] ^ sum[y] == k的对数,直接莫队,记录一个桶,加入一个新的sum,对答案的贡献是cnt[sum ^ k],减去一个sum,同理减去cnt[sum ^ k]没什么细节,也没什么注意事项,直接写就行了·····

 

Source:

/*
    created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <bitset>
 
inline char read() {
    static const int IN_LEN = 1024 * 1024;
    static char buf[IN_LEN], *s, *t;
    if (s == t) {
        t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
        if (s == t) return -1;
    }
    return *s++;
}
 
///*
template<class T>
inline void R(T &x) {
    static char c;
    static bool iosig;
    for (c = read(), iosig = false; !isdigit(c); c = read()) {
        if (c == -1) return ;
        if (c == '-') iosig = true; 
    }
    for (x = 0; isdigit(c); c = read()) 
        x = ((x << 2) + x << 1) + (c ^ '0');
    if (iosig) x = -x;
}
//*/
 
const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
    if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
    *oh++ = c;
}
 
template<class T>
inline void W(T x) {
    static int buf[30], cnt;
    if (x == 0) write_char('0');
    else {
        if (x < 0) write_char('-'), x = -x;
        for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
        while (cnt) write_char(buf[cnt--]);
    }
}
 
inline void flush() {
    fwrite(obuf, 1, oh - obuf, stdout);
}
 
/*
template<class T>
inline void R(T &x) {
    static char c;
    static bool iosig;
    for (c = getchar(), iosig = false; !isdigit(c); c = getchar())
        if (c == '-') iosig = true; 
    for (x = 0; isdigit(c); c = getchar()) 
        x = ((x << 2) + x << 1) + (c ^ '0');
    if (iosig) x = -x;
}
//*/

const int MAXN = 150000 + 1;

int n, m, k, l, r, bs;
int a[MAXN], cnt[MAXN];
long long final_ans[MAXN];

struct query {
    int l, r, bl, id;
    query() {}
    query(int l, int r, int id) : l(l), r(r), bl((l - 1) / bs + 1), id(id) {}

    inline bool operator < (const query &a) const {
        return (bl == a.bl) ? (r < a.r) : (bl < a.bl);
    }
} q[MAXN];

inline void read_in() {
    R(n), R(m), R(k);
    for (int i = 1; i <= n; ++i) R(a[i]), a[i] ^= a[i - 1];
    bs = sqrt(n);
    for (int i = 1; i <= m; ++i) R(l), R(r), q[i] = query(l, r, i);
    std::sort(q + 1, q + m + 1);
}

long long ans;
inline void compose_l(int cur) {
    cur--, cnt[a[cur]]--, ans -= (long long)cnt[a[cur] ^ k];
}

inline void compose_r(int cur) {
    cnt[a[cur]]--, ans -= (long long)cnt[a[cur] ^ k];
}

inline void extend_l(int cur) {
    cur--, ans += (long long)cnt[a[cur] ^ k], cnt[a[cur]]++;
}

inline void extend_r(int cur) {
    ans += (long long)cnt[a[cur] ^ k], cnt[a[cur]]++;
}

inline void solve() {
    int l = 1, r = 1;
    cnt[a[1]]++, cnt[a[0]]++, ans = (a[1] == k);
    for (int i = 1; i <= m; ++i) {
        while (l < q[i].l) compose_l(l), l++;
        while (r > q[i].r) compose_r(r), r--;
        while (l > q[i].l) --l, extend_l(l);
        while (r < q[i].r) ++r, extend_r(r);
        final_ans[q[i].id] = ans;
    } 
    for (int i = 1; i <= m; ++i) W(final_ans[i]), write_char('\n');
}

int main() {
    freopen("xor.in", "r", stdin);
    freopen("xor.out", "w", stdout);
    read_in();
    solve();
    flush();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/scar_lyw/article/details/80212976
今日推荐