#include <stdio.h>
#define N 500000
#define Q 500000
#define M (N * 2 + Q)
#define N_ (1 << 21) /* N_ = pow2(ceil(log2(M))) */
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int ll[M], rr[M], ww[M], xx[M], idx[M], m;
int compare_l(int h1, int h2) {
return ll[h1] != ll[h2] ? ll[h1] - ll[h2] : h1 - h2;
}
int compare_r(int h1, int h2) {
return rr[h1] - rr[h2];
}
int (*compare)(int, int);
void sort(int *hh, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = compare(hh[j], h);
if (c == 0)
j++;
else if (c < 0) {
tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
i++, j++;
} else {
k--;
tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
}
}
sort(hh, l, i);
l = k;
}
}
int st1[N_ * 2], st2[N_ * 2], mn1[N_ * 2], mn2[N_ * 2], lz[N_], h_, n_;
void build(int m) {
int i;
h_ = 0;
while (1 << h_ < m)
h_++;
n_ = 1 << h_;
for (i = 1; i < n_ * 2; i++)
st1[i] = -1, st2[i] = -1, mn1[i] = 0, mn2[i] = INF;
}
void put(int i, int x) {
if (st1[i] != -1)
st1[i] += x;
mn1[i] += x;
if (i < n_)
lz[i] += x;
}
void pus(int i) {
if (lz[i]) {
int l = i << 1, r = l | 1;
if (mn1[l] < mn1[r])
put(i << 1 | 0, lz[i]);
else if (mn1[l] > mn1[r])
put(i << 1 | 1, lz[i]);
else
put(i << 1 | 0, lz[i]), put(i << 1 | 1, lz[i]);
lz[i] = 0;
}
}
void pul(int i) {
if (!lz[i]) {
int l = i << 1, r = l | 1;
st2[i] = max(st2[l], st2[r]);
if (mn1[l] < mn1[r]) {
st1[i] = st1[l];
st2[i] = max(st2[i], st1[r]);
mn1[i] = mn1[l];
mn2[i] = min(mn2[l], mn1[r]);
} else if (mn1[l] > mn1[r]) {
st1[i] = st1[r];
st2[i] = max(st2[i], st1[l]);
mn1[i] = mn1[r];
mn2[i] = min(mn1[l], mn2[r]);
} else {
st1[i] = max(st1[l], st1[r]);
mn1[i] = mn1[l];
mn2[i] = min(mn2[l], mn2[r]);
}
}
}
void push(int i) {
int h;
for (h = h_; h > 0; h--)
pus(i >> h);
}
void pull(int i) {
while (i > 1)
pul(i >>= 1);
}
void update(int i, int x) {
i += n_;
push(i);
st1[i] = x, st2[i] = -1, mn1[i] = 0, mn2[i] = INF;
pull(i);
}
void update_(int i, int x) {
if (x <= mn1[i])
return;
if (x < mn2[i]) {
put(i, x - mn1[i]);
return;
}
pus(i);
update_(i << 1 | 0, x), update_(i << 1 | 1, x);
pul(i);
}
int query(int l, int r) {
int x;
push(l += n_), push(r += n_);
x = -1;
for ( ; l <= r; l >>= 1, r >>= 1) {
if ((l & 1) == 1)
x = max(x, max(st1[l], st2[l])), l++;
if ((r & 1) == 0)
x = max(x, max(st1[r], st2[r])), r--;
}
return x;
}
int main() {
static int aa[N], qu[N], hh[M], ans[Q];
int n, q, h, h_, h1, h1_, i, cnt;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &aa[i]);
scanf("%d", &q);
for (h = 0; h < q; h++)
scanf("%d%d", &ll[h], &rr[h]), ll[h]--, rr[h]--;
m = q;
cnt = 0;
for (i = 0; i < n; i++) {
while (cnt && aa[qu[cnt - 1]] < aa[i])
cnt--;
if (cnt)
ll[m] = qu[cnt - 1], rr[m] = i * 2 - qu[cnt - 1], ww[m] = aa[qu[cnt - 1]] + aa[i], m++;
qu[cnt++] = i;
}
cnt = 0;
for (i = n - 1; i >= 0; i--) {
while (cnt && aa[qu[cnt - 1]] <= aa[i])
cnt--;
if (cnt)
ll[m] = i, rr[m] = qu[cnt - 1] * 2 - i, ww[m] = aa[i] + aa[qu[cnt - 1]], m++;
qu[cnt++] = i;
}
for (h = 0; h < m; h++)
hh[h] = h;
compare = compare_l, sort(hh, 0, m);
for (h = 0; h < m; h++) {
xx[h] = ll[hh[h]];
idx[hh[h]] = h;
}
build(m);
compare = compare_r, sort(hh, 0, m);
for (i = 0, h = 0; i < n; i++) {
h_ = h;
while (h_ < m && rr[hh[h_]] <= i)
h_++;
for (h1 = h; h1 < h_; h1++)
if ((h1_ = hh[h1]) >= q)
update(idx[h1_], ww[h1_]);
update_(1, aa[i]);
for (h1 = h; h1 < h_; h1++)
if ((h1_ = hh[h1]) < q)
ans[h1_] = query(idx[h1_], m - 1);
h = h_;
}
for (h = 0; h < q; h++)
printf("%d\n", ans[h]);
return 0;
}
Compilation message
jumps.c: In function 'main':
jumps.c:159:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
159 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
jumps.c:161:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
161 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
jumps.c:162:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
162 | scanf("%d", &q);
| ^~~~~~~~~~~~~~~
jumps.c:164:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
164 | scanf("%d%d", &ll[h], &rr[h]), ll[h]--, rr[h]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
360 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
424 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
412 KB |
Output is correct |
10 |
Correct |
1 ms |
424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
360 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
424 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
412 KB |
Output is correct |
10 |
Correct |
1 ms |
424 KB |
Output is correct |
11 |
Correct |
635 ms |
40392 KB |
Output is correct |
12 |
Correct |
589 ms |
40252 KB |
Output is correct |
13 |
Correct |
520 ms |
40336 KB |
Output is correct |
14 |
Correct |
522 ms |
40460 KB |
Output is correct |
15 |
Correct |
582 ms |
40432 KB |
Output is correct |
16 |
Correct |
584 ms |
39904 KB |
Output is correct |
17 |
Correct |
520 ms |
39792 KB |
Output is correct |
18 |
Correct |
582 ms |
39708 KB |
Output is correct |
19 |
Correct |
503 ms |
40396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
311 ms |
30332 KB |
Output is correct |
2 |
Correct |
179 ms |
17384 KB |
Output is correct |
3 |
Correct |
135 ms |
17360 KB |
Output is correct |
4 |
Correct |
305 ms |
30268 KB |
Output is correct |
5 |
Correct |
362 ms |
30236 KB |
Output is correct |
6 |
Correct |
309 ms |
29576 KB |
Output is correct |
7 |
Correct |
305 ms |
29736 KB |
Output is correct |
8 |
Correct |
305 ms |
29560 KB |
Output is correct |
9 |
Correct |
312 ms |
29848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
292 KB |
Output is correct |
4 |
Correct |
1 ms |
360 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
424 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
412 KB |
Output is correct |
10 |
Correct |
1 ms |
424 KB |
Output is correct |
11 |
Correct |
635 ms |
40392 KB |
Output is correct |
12 |
Correct |
589 ms |
40252 KB |
Output is correct |
13 |
Correct |
520 ms |
40336 KB |
Output is correct |
14 |
Correct |
522 ms |
40460 KB |
Output is correct |
15 |
Correct |
582 ms |
40432 KB |
Output is correct |
16 |
Correct |
584 ms |
39904 KB |
Output is correct |
17 |
Correct |
520 ms |
39792 KB |
Output is correct |
18 |
Correct |
582 ms |
39708 KB |
Output is correct |
19 |
Correct |
503 ms |
40396 KB |
Output is correct |
20 |
Correct |
311 ms |
30332 KB |
Output is correct |
21 |
Correct |
179 ms |
17384 KB |
Output is correct |
22 |
Correct |
135 ms |
17360 KB |
Output is correct |
23 |
Correct |
305 ms |
30268 KB |
Output is correct |
24 |
Correct |
362 ms |
30236 KB |
Output is correct |
25 |
Correct |
309 ms |
29576 KB |
Output is correct |
26 |
Correct |
305 ms |
29736 KB |
Output is correct |
27 |
Correct |
305 ms |
29560 KB |
Output is correct |
28 |
Correct |
312 ms |
29848 KB |
Output is correct |
29 |
Correct |
1620 ms |
125048 KB |
Output is correct |
30 |
Correct |
1282 ms |
80472 KB |
Output is correct |
31 |
Correct |
1068 ms |
80472 KB |
Output is correct |
32 |
Correct |
1642 ms |
124948 KB |
Output is correct |
33 |
Correct |
1591 ms |
125076 KB |
Output is correct |
34 |
Correct |
1633 ms |
122832 KB |
Output is correct |
35 |
Correct |
1635 ms |
122436 KB |
Output is correct |
36 |
Correct |
1671 ms |
122384 KB |
Output is correct |
37 |
Correct |
1741 ms |
123828 KB |
Output is correct |
38 |
Correct |
1433 ms |
124948 KB |
Output is correct |
39 |
Correct |
1339 ms |
125116 KB |
Output is correct |
40 |
Correct |
1330 ms |
121636 KB |
Output is correct |
41 |
Correct |
1408 ms |
121280 KB |
Output is correct |
42 |
Correct |
1428 ms |
121180 KB |
Output is correct |
43 |
Correct |
1299 ms |
122924 KB |
Output is correct |
44 |
Correct |
1285 ms |
125212 KB |
Output is correct |
45 |
Correct |
1296 ms |
125096 KB |
Output is correct |
46 |
Correct |
1262 ms |
121956 KB |
Output is correct |
47 |
Correct |
1273 ms |
121600 KB |
Output is correct |
48 |
Correct |
1305 ms |
121376 KB |
Output is correct |
49 |
Correct |
1269 ms |
123660 KB |
Output is correct |
50 |
Correct |
1360 ms |
125012 KB |
Output is correct |
51 |
Correct |
1348 ms |
125136 KB |
Output is correct |
52 |
Correct |
1353 ms |
122588 KB |
Output is correct |
53 |
Correct |
1335 ms |
122272 KB |
Output is correct |
54 |
Correct |
1333 ms |
122416 KB |
Output is correct |
55 |
Correct |
1327 ms |
123976 KB |
Output is correct |