-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMo.hpp
More file actions
75 lines (70 loc) · 1.92 KB
/
Copy pathMo.hpp
File metadata and controls
75 lines (70 loc) · 1.92 KB
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
class Mo
{
struct Query
{
unsigned l, r, qid, gid;
Query(unsigned l, unsigned r, unsigned qid, unsigned gid) :
l(l), r(r), qid(qid), gid(gid)
{}
bool operator <(const Query &q) const
{ return gid != q.gid ? gid < q.gid : r < q.r; }
};
public:
Mo(unsigned size) : n(size), gsize(sqrt(n))
{}
template<typename Index, typename Value, typename Function1, typename Function2>
void query(vector<pair<Index, Index>> &queries, vector<Value> &ans,
Function1 insert, Function2 remove)
{
vector<Query> que;
for (const auto &p : queries) {
que.emplace_back(p.first, p.second, que.size(), p.first / gsize);
}
sort(que.begin(), que.end());
unsigned l = 1;
unsigned r = 0;
Value ca = 0;
ans.resize(queries.size());
for (const Query &q : que) {
while (l > q.l) {
ca += insert(--l - 1); // -1 on partial_sum
}
while (l < q.l) {
ca += remove(l++ - 1); // -1 on partial_sum
}
while (r < q.r) {
ca += insert(++r);
}
while (r > q.r) {
ca += remove(r--);
}
ans[q.qid] = ca;
}
}
protected:
unsigned n;
unsigned gsize;
};
// Codeforces 617E. XOR and Favorite Number
int main(int, char *[])
{
int n = getint(), m = getint(), k = 0;
vector<int> a = getints(n + 1), s(n + 1);
vector<long long> f(1 << 20), ans(m);
vector<pair<int, int>> q = getpairs(m);
swap(k, a[0]);
++f[0];
partial_sum(a.begin(), a.end(), s.begin(), [](int a, int b) {
return a ^ b;
});
Mo(n).query(q, ans, [&](int i) {
int r = f[s[i] ^ k];
f[s[i]]++;
return r;
}, [&](int i) {
f[s[i]]--;
return -f[s[i] ^ k];
});
writelns(ans);
return 0;
}