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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
using namespace std;
using pii = pair<int, int>;
const int XN = 1e5 * 40 * 4;
const int XM = 40;
int idx = 0;
int lc[XN], rc[XN];
int dat[XN];
void pushup(int n) {
dat[n] = dat[lc[n]] + dat[rc[n]];
}
int update(int pos, int x, int L, int R, int n) {
int ne = ++idx;
lc[ne] = lc[n], rc[ne] = rc[n];
dat[ne] = dat[n];
if (L == R && L == pos) {
dat[ne] += x;
return ne;
}
int mid = (L + R) / 2;
if (pos <= mid)lc[ne] = update(pos, x, L, mid, lc[ne]);
if (mid < pos)rc[ne] = update(pos, x, mid + 1, R, rc[ne]);
pushup(ne);
return ne;
}
int query(int l, int r, int L, int R, int n) {
if (l <= L && R <= r)return dat[n];
int res = 0;
int mid = (L + R) / 2;
if (l <= mid)res += query(l, r, L, mid, lc[n]);
if (mid < r)res += query(l, r, mid + 1, R, rc[n]);
return res;
}
int a[XN], b[XM];
vector<int> cha;
struct Line {
int l, r, x;
int delta;
};
vector<Line> lines;
vector<Line> offset;
int rt[XN];
int main() {
ios::sync_with_stdio(false);
int n; cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 0; i < XM; i++)b[i] = n + 1;
for (int i = n; i >= 1; i--) {
cha.clear();
for (int j = 0; j < XM; j++) {
if (((a[i] >> j) & 1) == 0) {
b[j] = min(b[j], i);
}
cha.push_back(b[j]);
}
sort(cha.begin(), cha.end());
int temp = (1 << 30) - 1;
for (auto pos : cha) {
if (pos > n)continue;
int t = temp & a[pos];
if (t == temp)continue;
temp = t;
lines.push_back({ i,pos,temp,1 });
}
}
sort(lines.begin(), lines.end(), [](const Line& a, const Line& b) {
if (a.x != b.x)return a.x < b.x;
if (a.l != b.l)return a.l < b.l;
return a.r < b.r;
});
/*
for (auto item : lines) {
cout << item.l << " " << item.r << "=" << item.x << "/" << item.delta << endl;
}
*/
for (int i = 0, j = 0; i < lines.size(); i++, j++) {
while (j < lines.size() && lines[i].x == lines[j].x) {
j++;
}
j--;
if (i == j) {
continue;
}
for (; i < j; i++)
offset.push_back({ lines[i].l,lines[i + 1].r,-1,-1 });
}
lines.insert(lines.end(), offset.begin(), offset.end());
sort(lines.begin(), lines.end(), [](auto a, auto b) {
if (a.l != b.l)return a.l > b.l;
return a.r < b.r;
});
/*
for (auto item : lines) {
cout << item.l << " " << item.r << "=" << item.x << "/" << item.delta << endl;
}
*/
int lastl = 0;
for (int i = 0; i < lines.size(); i++) {
rt[lines[i].l] = update(lines[i].r, lines[i].delta, 1, n, rt[lastl]);
lastl = lines[i].l;
}
int q; cin >> q;
int lastans = 0;
while (q--) {
int l1, r1; cin >> l1 >> r1;
int l = (l1 ^ lastans) % n + 1;
int r = (r1 ^ lastans) % n + 1;
if (l > r)swap(l, r);
lastans = query(l, r, 1, n, rt[l]);
//cout << l << " " << r << "=" << lastans << endl;
cout << lastans << "\n";
}
return 0;
}
|