-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQRT Decomposition + Mo's algorithm.cpp
103 lines (82 loc) · 1.92 KB
/
SQRT Decomposition + Mo's algorithm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Count of pairs in range [L,R] whose XOR is equal to K - Codeforces problem 617E
#include <bits/stdc++.h>
#include <ext/rope>
using namespace std;
using namespace __gnu_cxx;
using ld = long double;
using llint = long long;
using ullint = unsigned long long;
using pii = pair <int,int>;
using pcc = pair <char,char>;
using pss = pair <string,string>;
using vi = vector<int>;
using vii = vi::iterator;
#define INF (1<<30)
#define MOD 1000000007
#define mp make_pair
#define mt make_tuple
#define all(c) c.begin(),c.end()
#define ms(name,val) memset(name, val, sizeof name)
#define np nullptr
int n, m, k, sz;
struct Query
{
int l, r, i;
bool operator()(const Query &q1, const Query &q2) const
{
int q1b = q1.l/sz;
int q2b = q2.l/sz;
if (q1b != q2b) return (q1b < q2b);
return (q1.r < q2.r);
}
};
vector <int> A, pref;
vector <llint> qans;
vector <Query> Q;
int cnt[1<<20];
llint ans;
void Insert(int v)
{
ans += cnt[v^k];
++cnt[v];
}
void Remove(int v)
{
--cnt[v];
ans -= cnt[v^k];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> k;
sz = ceil(sqrt(n));
A.resize(n);
pref.resize(n+1);
qans.resize(m);
Q.resize(m);
for (int t1 = 0; t1 < n; ++t1)
cin >> A[t1];
for (int t1 = 1; t1 <= n; ++t1)
pref[t1] = pref[t1-1] ^ A[t1-1];
for (int t1 = 0; t1 < m; ++t1)
{
cin >> Q[t1].l >> Q[t1].r;
--Q[t1].l;
Q[t1].i = t1;
}
sort(all(Q),Query());
int lcur = 0, rcur = 0;
for (int t1 = 0; t1 < m; ++t1)
{
int L = Q[t1].l, R = Q[t1].r;
while (lcur < L) Remove(pref[lcur]), ++lcur;
while (lcur > L) Insert(pref[lcur-1]), --lcur;
while (rcur <= R) Insert(pref[rcur]), ++rcur;
while (rcur > R+1) Remove(pref[rcur-1]), --rcur;
qans[Q[t1].i] = ans;
}
for (auto &it: qans)
cout << it << '\n';
return 0;
}