forked from bnslakki/Spoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKQUERYO.cpp
64 lines (62 loc) · 1.21 KB
/
KQUERYO.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
#include "bits/stdc++.h"
using namespace std;
int arr[30003], B[30003];
int len, n;
void sqrt_decompose()
{
double l = sqrt(n);
len = (int)ceil(n / l);
int flip = 0;
while (1)
{
sort(B + flip*len, B + min(flip*len + len, n));
flip++;
if (flip*len + len > n){
sort(B + flip*len, B + n);
break;
}
}
}
int query(int i, int j, int K)
{
int ans = 0;
int startblock = i / len;
int endblock = j / len;
if (startblock == endblock){
for (int k = i; k <= j; k++)if (arr[k]>K)ans++;
return ans;
}
for (int k = i; k <= (startblock + 1)*len - 1; k++)if (arr[k] > K)ans++;
for (int k = startblock + 1; k <= endblock - 1; k++)ans += len - (upper_bound(B + k*len, B + min(k*len + len, n), K) - (B + k*len));
for (int k = endblock*len; k <= j; k++)if (arr[k] > K)ans++;
return ans;
}
int main()
{
//READ;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
B[i] = arr[i];
}
sqrt_decompose();
int q, a, b, c, i, j, k, ans = 0;
scanf("%d", &q);
while (q--)
{
scanf("%d%d%d", &a, &b, &c);
i = a^ans;
j = b^ans;
k = c^ans;
if (i < 1)i = 1;
if (j > n)j = n;
if (i > j) {
ans = 0;
printf("0\n");
continue;
}
i--, j--;
ans = query(i, j, k);
printf("%d\n", ans);
}
}