#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define ss(x) (int) x.size()
#define FOR(i, j, n) for(int i = j; i <= n; ++i)
#define fi first
#define se second
#define cat(x) cerr << #x << " = " << x << endl;
#define ios cin.tie(0); ios_base::sync_with_stdio(0)
using namespace std;
const int nax = 6e5 + 111;
const int inf = 1e9 + 111;
const int pod = (1 << 19);
int n;
int a[nax];
int q, l, r;
int res[nax];
struct query {
int l, r, id;
};
vector <query> v;
vector <pair<int, int>> pairs;
stack <pair<int, int>> stos; // value, indeks
struct tree {
int lewo, prawo, best;
tree() {
lewo = -inf;
prawo = -inf;
best = -inf;
}
};
tree t[2 * pod];
void add(int u, int c) {
u += pod;
t[u].lewo = max(t[u].lewo, c);
t[u].best = max(t[u].best, t[u].lewo + t[u].prawo);
u /= 2;
while(u) {
t[u].lewo = max(t[2 * u].lewo, t[2 * u + 1].lewo);
t[u].best = max({t[2 * u].best, t[2 * u + 1].best, t[2 * u].lewo + t[2 * u + 1].prawo});
u /= 2;
}
}
tree merge(const tree &a, const tree &b) {
tree c = tree();
c.lewo = max(a.lewo, b.lewo);
c.prawo = max(a.prawo, b.prawo);
c.best = max({a.best, b.best, a.lewo + b.prawo});
return c;
}
tree Query(int x, int y, int u = 1, int l = 0, int r = pod - 1) {
if(x <= l && r <= y)
return t[u];
int m = (l + r) / 2;
tree ans = tree();
if(x <= m)
ans = merge(ans, Query(x, y, 2 * u, l, m));
if(m < y)
ans = merge(ans, Query(x, y, 2 * u + 1, m + 1, r));
return ans;
}
int main() {
scanf("%d", &n);
FOR(i, 1, n)
scanf("%d", &a[i]);
FOR(i, 1, n) {
while(!stos.empty() && stos.top().fi <= a[i]) {
pairs.pb(mp(stos.top().se, i));
stos.pop();
}
if(!stos.empty())
pairs.pb(mp(stos.top().se, i));
stos.push(mp(a[i], i));
}
sort(pairs.begin(), pairs.end());
scanf("%d", &q);
FOR(i, 1, q) {
scanf("%d %d", &l, &r);
query x = {l, r, i};
v.pb(x);
}
sort(v.begin(), v.end(), [](query a, query b) {
return a.l < b.l;
});
FOR(i, 0, pod - 1) {
t[i] = tree();
if(1 <= i && i <= n)
t[i + pod].prawo = a[i];
}
for(int i = pod - 1; 1 <= i; --i) {
t[i] = tree();
t[i].prawo = max(t[2 * i].prawo, t[2 * i + 1].prawo);
}
int wsk = ss(v) - 1;
int p = ss(pairs) - 1;
for(int i = n; 1 <= i; --i) {
while(p >= 0 && pairs[p].fi == i) {
int c = 2 * pairs[p].se - pairs[p].fi;
if(c <= n)
add(c, a[pairs[p].fi] + a[pairs[p].se]);
p -= 1;
}
while(wsk >= 0 && v[wsk].l == i) {
res[v[wsk].id] = Query(v[wsk].l, v[wsk].r).best;
wsk -= 1;
}
}
FOR(i, 1, q)
printf("%d\n", res[i]);
return 0;
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:81:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
jumps.cpp:83:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
jumps.cpp:95:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
jumps.cpp:97:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &l, &r);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
12664 KB |
Output is correct |
2 |
Correct |
52 ms |
12664 KB |
Output is correct |
3 |
Correct |
18 ms |
12592 KB |
Output is correct |
4 |
Correct |
16 ms |
12664 KB |
Output is correct |
5 |
Correct |
17 ms |
12664 KB |
Output is correct |
6 |
Correct |
18 ms |
12664 KB |
Output is correct |
7 |
Correct |
17 ms |
12664 KB |
Output is correct |
8 |
Correct |
17 ms |
12664 KB |
Output is correct |
9 |
Correct |
17 ms |
12808 KB |
Output is correct |
10 |
Correct |
18 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
12664 KB |
Output is correct |
2 |
Correct |
52 ms |
12664 KB |
Output is correct |
3 |
Correct |
18 ms |
12592 KB |
Output is correct |
4 |
Correct |
16 ms |
12664 KB |
Output is correct |
5 |
Correct |
17 ms |
12664 KB |
Output is correct |
6 |
Correct |
18 ms |
12664 KB |
Output is correct |
7 |
Correct |
17 ms |
12664 KB |
Output is correct |
8 |
Correct |
17 ms |
12664 KB |
Output is correct |
9 |
Correct |
17 ms |
12808 KB |
Output is correct |
10 |
Correct |
18 ms |
12636 KB |
Output is correct |
11 |
Correct |
492 ms |
30468 KB |
Output is correct |
12 |
Correct |
486 ms |
30360 KB |
Output is correct |
13 |
Correct |
476 ms |
30500 KB |
Output is correct |
14 |
Correct |
481 ms |
30592 KB |
Output is correct |
15 |
Correct |
497 ms |
30472 KB |
Output is correct |
16 |
Correct |
481 ms |
29788 KB |
Output is correct |
17 |
Correct |
505 ms |
29792 KB |
Output is correct |
18 |
Correct |
481 ms |
29668 KB |
Output is correct |
19 |
Correct |
513 ms |
30400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
19400 KB |
Output is correct |
2 |
Correct |
76 ms |
17384 KB |
Output is correct |
3 |
Correct |
77 ms |
18408 KB |
Output is correct |
4 |
Correct |
127 ms |
19364 KB |
Output is correct |
5 |
Correct |
127 ms |
19428 KB |
Output is correct |
6 |
Correct |
119 ms |
18788 KB |
Output is correct |
7 |
Correct |
119 ms |
18632 KB |
Output is correct |
8 |
Correct |
117 ms |
18660 KB |
Output is correct |
9 |
Correct |
122 ms |
18984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
12664 KB |
Output is correct |
2 |
Correct |
52 ms |
12664 KB |
Output is correct |
3 |
Correct |
18 ms |
12592 KB |
Output is correct |
4 |
Correct |
16 ms |
12664 KB |
Output is correct |
5 |
Correct |
17 ms |
12664 KB |
Output is correct |
6 |
Correct |
18 ms |
12664 KB |
Output is correct |
7 |
Correct |
17 ms |
12664 KB |
Output is correct |
8 |
Correct |
17 ms |
12664 KB |
Output is correct |
9 |
Correct |
17 ms |
12808 KB |
Output is correct |
10 |
Correct |
18 ms |
12636 KB |
Output is correct |
11 |
Correct |
492 ms |
30468 KB |
Output is correct |
12 |
Correct |
486 ms |
30360 KB |
Output is correct |
13 |
Correct |
476 ms |
30500 KB |
Output is correct |
14 |
Correct |
481 ms |
30592 KB |
Output is correct |
15 |
Correct |
497 ms |
30472 KB |
Output is correct |
16 |
Correct |
481 ms |
29788 KB |
Output is correct |
17 |
Correct |
505 ms |
29792 KB |
Output is correct |
18 |
Correct |
481 ms |
29668 KB |
Output is correct |
19 |
Correct |
513 ms |
30400 KB |
Output is correct |
20 |
Correct |
134 ms |
19400 KB |
Output is correct |
21 |
Correct |
76 ms |
17384 KB |
Output is correct |
22 |
Correct |
77 ms |
18408 KB |
Output is correct |
23 |
Correct |
127 ms |
19364 KB |
Output is correct |
24 |
Correct |
127 ms |
19428 KB |
Output is correct |
25 |
Correct |
119 ms |
18788 KB |
Output is correct |
26 |
Correct |
119 ms |
18632 KB |
Output is correct |
27 |
Correct |
117 ms |
18660 KB |
Output is correct |
28 |
Correct |
122 ms |
18984 KB |
Output is correct |
29 |
Correct |
1016 ms |
52440 KB |
Output is correct |
30 |
Correct |
841 ms |
45404 KB |
Output is correct |
31 |
Correct |
824 ms |
49608 KB |
Output is correct |
32 |
Correct |
1022 ms |
52516 KB |
Output is correct |
33 |
Correct |
1142 ms |
52528 KB |
Output is correct |
34 |
Correct |
979 ms |
50152 KB |
Output is correct |
35 |
Correct |
964 ms |
49972 KB |
Output is correct |
36 |
Correct |
935 ms |
49884 KB |
Output is correct |
37 |
Correct |
936 ms |
51252 KB |
Output is correct |
38 |
Correct |
763 ms |
52520 KB |
Output is correct |
39 |
Correct |
761 ms |
52560 KB |
Output is correct |
40 |
Correct |
723 ms |
49292 KB |
Output is correct |
41 |
Correct |
710 ms |
48644 KB |
Output is correct |
42 |
Correct |
733 ms |
48720 KB |
Output is correct |
43 |
Correct |
737 ms |
50344 KB |
Output is correct |
44 |
Correct |
811 ms |
52560 KB |
Output is correct |
45 |
Correct |
793 ms |
52456 KB |
Output is correct |
46 |
Correct |
764 ms |
49328 KB |
Output is correct |
47 |
Correct |
774 ms |
49052 KB |
Output is correct |
48 |
Correct |
776 ms |
49032 KB |
Output is correct |
49 |
Correct |
806 ms |
51016 KB |
Output is correct |
50 |
Correct |
825 ms |
52512 KB |
Output is correct |
51 |
Correct |
848 ms |
52456 KB |
Output is correct |
52 |
Correct |
802 ms |
50048 KB |
Output is correct |
53 |
Correct |
820 ms |
49744 KB |
Output is correct |
54 |
Correct |
834 ms |
49608 KB |
Output is correct |
55 |
Correct |
816 ms |
51460 KB |
Output is correct |