首页 > 代码库 > Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法

链接:

http://codeforces.com/contest/617/problem/E

代码:

31 struct node {32     int l, r, id;33 }q[MAXN];34 35 int n, m, k;36 int a[MAXN];37 int pos[MAXN];38 ll ans[MAXN];39 ll sum[2000007];40 41 bool cmp(node a, node b) {42     if (pos[a.l] == pos[b.l]) return a.r < b.r;43     return pos[a.l] < pos[b.l];44 }45 46 int L = 1, R = 0;47 ll Ans = 0;48 49 void add(int x) {50     Ans += sum[a[x] ^ k];51     sum[a[x]]++;52 }53 54 void del(int x) {55     sum[a[x]]--;56     Ans -= sum[a[x] ^ k];57 }58 59 int main() {60     ios::sync_with_stdio(false), cin.tie(0);61     cin >> n >> m >> k;62     int sz = sqrt(n);63     rep(i, 1, n + 1) {64         cin >> a[i];65         a[i] ^= a[i - 1];66         pos[i] = i / sz;67     }68     rep(i, 0, m) {69         cin >> q[i].l >> q[i].r;70         q[i].id = i;71     }72     sort(q, q + m, cmp);73     sum[0] = 1;74     rep(i, 0, m) {75         while (L < q[i].l) del(L - 1), L++;76         while (L > q[i].l) L--, add(L - 1);77         while (R < q[i].r) R++, add(R);78         while (R > q[i].r) del(R), R--;79         ans[q[i].id] = Ans;80     }81     rep(i, 0, m) cout << ans[i] << endl;82     return 0;83 }

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 莫队算法