#include <stdio.h>
#include <stdlib.h>
#define N 200000
#define N_ (1 << 18) /* N_ = pow2(ceil(log2(N + 2))) */
#define K 100
int n;
int depth(long long i) {
int lower = -1, upper = n;
while (upper - lower > 1) {
int d = (lower + upper) / 2;
if (i < (long long) (d + 1) * (d + 2) / 2)
upper = d;
else
lower = d;
}
return upper;
}
int *aaa[N_ * 2], sz[N_ * 2], n_;
void pul(int i) {
int l = i << 1, r = l | 1, hl, hr, h;
aaa[i] = (int *) malloc((sz[i] = sz[l] + sz[r]) * sizeof *aaa[i]);
hl = 0, hr = 0, h = 0;
while (hl < sz[l] && hr < sz[r])
if (aaa[l][hl] + hr < aaa[r][hr])
aaa[i][h++] = aaa[l][hl++] + hr;
else
aaa[i][h++] = aaa[r][hr++];
while (hl < sz[l])
aaa[i][h++] = aaa[l][hl++] + hr;
while (hr < sz[r])
aaa[i][h++] = aaa[r][hr++];
}
void build(int *aa, int n) {
int i;
n_ = 1;
while (n_ < n + 2)
n_ <<= 1;
for (i = 0; i < n_; i++)
if (i >= n)
sz[n_ + i] = 0;
else {
aaa[n_ + i] = (int *) malloc((sz[n_ + i] = 1) * sizeof *aaa[n_ + i]);
aaa[n_ + i][0] = aa[i];
}
for (i = n_ - 1; i > 0; i--)
pul(i);
}
int lift(int i, int a) {
int lower = -1, upper = sz[i];
while (upper - lower > 1) {
int h = (lower + upper) / 2;
if (aaa[i][h] < a)
lower = h;
else
upper = h;
}
return a - upper;
}
int lift1(int l, int r, int a) {
static int qu[K];
int cnt;
cnt = 0;
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
qu[cnt++] = l++;
if ((r & 1) == 0)
a = lift(r--, a);
}
while (cnt--)
a = lift(qu[cnt], a);
return a;
}
long long lift2(int r, int a, int b) {
int l = 0;
for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1)
if ((r & 1) == 0) {
int i = r--, a_ = lift(i, a), b_ = lift(i, b);
if (a_ != b_)
a = a_, b = b_;
else {
while (i < n_) {
a_ = lift(i << 1 | 1, a), b_ = lift(i << 1 | 1, b);
if (a_ != b_)
a = a_, b = b_, i = i << 1 | 0;
else
i = i << 1 | 1;
}
return (long long) (i - n_) * (i - n_ + 1) / 2 + lift(i, a);
}
}
return -1;
}
int main() {
static int ii[N];
int q, t, i, j;
long long n1, ans;
scanf("%d%d%d", &n, &q, &t);
for (i = 0; i < n; i++) {
long long a;
scanf("%lld", &a), a--, ii[i] = a - (long long) i * (i + 1) / 2;
}
build(ii, n);
n1 = (long long) (n + 1) * (n + 2) / 2, ans = 0;
while (q--) {
long long x, y, tmp;
int dx, dy;
scanf("%lld%lld", &x, &y), x--, y--;
x = (x + t * ans) % n1, y = (y + t * ans) % n1;
if (x > y)
tmp = x, x = y, y = tmp;
dx = depth(x), i = x - (long long) dx * (dx + 1) / 2;
dy = depth(y), j = y - (long long) dy * (dy + 1) / 2;
j = lift1(dx, dy - 1, j);
printf("%lld\n", ans = (i == j ? (long long) dx * (dx + 1) / 2 + i : lift2(dx - 1, i, j)) + 1);
}
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:117:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | scanf("%d%d%d", &n, &q, &t);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.c:121:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | scanf("%lld", &a), a--, ii[i] = a - (long long) i * (i + 1) / 2;
| ^~~~~~~~~~~~~~~~~
Main.c:129:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
129 | scanf("%lld%lld", &x, &y), x--, y--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
32960 KB |
Output is correct |
2 |
Correct |
6 ms |
3532 KB |
Output is correct |
3 |
Correct |
65 ms |
33360 KB |
Output is correct |
4 |
Correct |
35 ms |
17464 KB |
Output is correct |
5 |
Correct |
64 ms |
33256 KB |
Output is correct |
6 |
Correct |
20 ms |
9804 KB |
Output is correct |
7 |
Correct |
50 ms |
33348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
32960 KB |
Output is correct |
2 |
Correct |
6 ms |
3532 KB |
Output is correct |
3 |
Correct |
65 ms |
33360 KB |
Output is correct |
4 |
Correct |
35 ms |
17464 KB |
Output is correct |
5 |
Correct |
64 ms |
33256 KB |
Output is correct |
6 |
Correct |
20 ms |
9804 KB |
Output is correct |
7 |
Correct |
50 ms |
33348 KB |
Output is correct |
8 |
Correct |
255 ms |
1392 KB |
Output is correct |
9 |
Correct |
193 ms |
1360 KB |
Output is correct |
10 |
Correct |
235 ms |
1300 KB |
Output is correct |
11 |
Correct |
150 ms |
1040 KB |
Output is correct |
12 |
Correct |
240 ms |
1280 KB |
Output is correct |
13 |
Correct |
165 ms |
1160 KB |
Output is correct |
14 |
Correct |
243 ms |
1788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
32960 KB |
Output is correct |
2 |
Correct |
6 ms |
3532 KB |
Output is correct |
3 |
Correct |
65 ms |
33360 KB |
Output is correct |
4 |
Correct |
35 ms |
17464 KB |
Output is correct |
5 |
Correct |
64 ms |
33256 KB |
Output is correct |
6 |
Correct |
20 ms |
9804 KB |
Output is correct |
7 |
Correct |
50 ms |
33348 KB |
Output is correct |
8 |
Correct |
255 ms |
1392 KB |
Output is correct |
9 |
Correct |
193 ms |
1360 KB |
Output is correct |
10 |
Correct |
235 ms |
1300 KB |
Output is correct |
11 |
Correct |
150 ms |
1040 KB |
Output is correct |
12 |
Correct |
240 ms |
1280 KB |
Output is correct |
13 |
Correct |
165 ms |
1160 KB |
Output is correct |
14 |
Correct |
243 ms |
1788 KB |
Output is correct |
15 |
Correct |
1695 ms |
38244 KB |
Output is correct |
16 |
Correct |
730 ms |
12100 KB |
Output is correct |
17 |
Correct |
1680 ms |
40328 KB |
Output is correct |
18 |
Correct |
755 ms |
12116 KB |
Output is correct |
19 |
Correct |
1633 ms |
40064 KB |
Output is correct |
20 |
Correct |
706 ms |
11696 KB |
Output is correct |
21 |
Correct |
1814 ms |
41624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1639 ms |
33388 KB |
Output is correct |
2 |
Correct |
1089 ms |
21944 KB |
Output is correct |
3 |
Correct |
1657 ms |
40360 KB |
Output is correct |
4 |
Correct |
1037 ms |
21668 KB |
Output is correct |
5 |
Correct |
1652 ms |
40012 KB |
Output is correct |
6 |
Correct |
1072 ms |
22588 KB |
Output is correct |
7 |
Correct |
1824 ms |
41520 KB |
Output is correct |