#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;
const int INF=0x3f3f3f3f;
const int N=5e5+5;
const int OFF=(1<<19);
struct Node{
int ab, c, res;
Node (int ab_=-INF, int c_=-INF, int res_=-INF) {
ab=ab_; c=c_; res=res_;
}
};
int n, q;
int p[N], sol[N];
vector <pii> que[N];
vector <int> v[N];
vector <int> st;
Node T[2*OFF];
#define DEBUG 0
Node mrg(Node a, Node b) {
Node ret=Node(max(a.ab, b.ab), max(a.c, b.c), max(a.res, b.res));
ret.res=max(ret.res, a.ab+b.c);
return ret;
}
void update(int pos, int val, int fl) {
if (pos>=n) return;
pos+=OFF;
if (fl) T[pos].ab=max(T[pos].ab, val);
else T[pos].c=max(T[pos].c, val);
T[pos].res=max(T[pos].res, T[pos].ab+T[pos].c);
pos/=2;
while (pos) {
T[pos]=mrg(T[pos*2], T[pos*2+1]);
pos/=2;
}
}
Node query(int a, int b, int pos=1, int lo=0, int hi=OFF) {
if (lo>=a && hi<=b) return T[pos];
if (lo>=b || hi<=a) return Node();
int mid=(lo+hi)/2;
return mrg(query(a, b, pos*2, lo, mid), query(a, b, pos*2+1, mid, hi));
}
void solve() {
for (int i=0; i<n; ++i) {
while (st.size() && p[st.back()]<=p[i]) {
v[st.back()].pb(i);
st.pop_back();
}
if (st.size()) v[st.back()].pb(i);
st.pb(i);
}
#if DEBUG
for (int i=0; i<n; ++i) {
for (int r:v[i]) printf("[%d, %d]\n", i, r);
}
#endif // DEBUG
for (int i=n-1; i>=0; --i) {
update(i, p[i], 0);
for (int r:v[i]) update(2*r-i, p[i]+p[r], 1);
for (pii pp:que[i]) sol[pp.Y]=query(i, pp.X+1).res;
}
for (int i=0; i<q; ++i) printf("%d\n", sol[i]);
}
void load() {
scanf("%d", &n);
for (int i=0; i<n; ++i) scanf("%d", &p[i]);
scanf("%d", &q);
for (int i=0; i<q; ++i) {
int l, r;
scanf("%d %d", &l, &r); l--; r--;
que[l].pb(mp(r, i));
}
}
int main() {
load();
solve();
return 0;
}
Compilation message
jumps.cpp: In function 'void load()':
jumps.cpp:85:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
85 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
jumps.cpp:86:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
86 | for (int i=0; i<n; ++i) scanf("%d", &p[i]);
| ~~~~~^~~~~~~~~~~~~
jumps.cpp:87:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
87 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
jumps.cpp:90:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
90 | scanf("%d %d", &l, &r); l--; r--;
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
36076 KB |
Output is correct |
2 |
Correct |
25 ms |
36076 KB |
Output is correct |
3 |
Correct |
25 ms |
36076 KB |
Output is correct |
4 |
Correct |
24 ms |
36076 KB |
Output is correct |
5 |
Correct |
25 ms |
36076 KB |
Output is correct |
6 |
Correct |
25 ms |
36076 KB |
Output is correct |
7 |
Correct |
24 ms |
36076 KB |
Output is correct |
8 |
Correct |
25 ms |
36076 KB |
Output is correct |
9 |
Correct |
25 ms |
36332 KB |
Output is correct |
10 |
Correct |
24 ms |
36076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
36076 KB |
Output is correct |
2 |
Correct |
25 ms |
36076 KB |
Output is correct |
3 |
Correct |
25 ms |
36076 KB |
Output is correct |
4 |
Correct |
24 ms |
36076 KB |
Output is correct |
5 |
Correct |
25 ms |
36076 KB |
Output is correct |
6 |
Correct |
25 ms |
36076 KB |
Output is correct |
7 |
Correct |
24 ms |
36076 KB |
Output is correct |
8 |
Correct |
25 ms |
36076 KB |
Output is correct |
9 |
Correct |
25 ms |
36332 KB |
Output is correct |
10 |
Correct |
24 ms |
36076 KB |
Output is correct |
11 |
Correct |
528 ms |
54784 KB |
Output is correct |
12 |
Correct |
519 ms |
54764 KB |
Output is correct |
13 |
Correct |
522 ms |
54892 KB |
Output is correct |
14 |
Correct |
542 ms |
54764 KB |
Output is correct |
15 |
Correct |
523 ms |
54892 KB |
Output is correct |
16 |
Correct |
531 ms |
54124 KB |
Output is correct |
17 |
Correct |
536 ms |
54056 KB |
Output is correct |
18 |
Correct |
518 ms |
54124 KB |
Output is correct |
19 |
Correct |
515 ms |
54636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
244 ms |
45164 KB |
Output is correct |
2 |
Correct |
172 ms |
44908 KB |
Output is correct |
3 |
Correct |
175 ms |
45792 KB |
Output is correct |
4 |
Correct |
240 ms |
45164 KB |
Output is correct |
5 |
Correct |
241 ms |
45292 KB |
Output is correct |
6 |
Correct |
236 ms |
44652 KB |
Output is correct |
7 |
Correct |
241 ms |
44396 KB |
Output is correct |
8 |
Correct |
234 ms |
44524 KB |
Output is correct |
9 |
Correct |
239 ms |
44908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
36076 KB |
Output is correct |
2 |
Correct |
25 ms |
36076 KB |
Output is correct |
3 |
Correct |
25 ms |
36076 KB |
Output is correct |
4 |
Correct |
24 ms |
36076 KB |
Output is correct |
5 |
Correct |
25 ms |
36076 KB |
Output is correct |
6 |
Correct |
25 ms |
36076 KB |
Output is correct |
7 |
Correct |
24 ms |
36076 KB |
Output is correct |
8 |
Correct |
25 ms |
36076 KB |
Output is correct |
9 |
Correct |
25 ms |
36332 KB |
Output is correct |
10 |
Correct |
24 ms |
36076 KB |
Output is correct |
11 |
Correct |
528 ms |
54784 KB |
Output is correct |
12 |
Correct |
519 ms |
54764 KB |
Output is correct |
13 |
Correct |
522 ms |
54892 KB |
Output is correct |
14 |
Correct |
542 ms |
54764 KB |
Output is correct |
15 |
Correct |
523 ms |
54892 KB |
Output is correct |
16 |
Correct |
531 ms |
54124 KB |
Output is correct |
17 |
Correct |
536 ms |
54056 KB |
Output is correct |
18 |
Correct |
518 ms |
54124 KB |
Output is correct |
19 |
Correct |
515 ms |
54636 KB |
Output is correct |
20 |
Correct |
244 ms |
45164 KB |
Output is correct |
21 |
Correct |
172 ms |
44908 KB |
Output is correct |
22 |
Correct |
175 ms |
45792 KB |
Output is correct |
23 |
Correct |
240 ms |
45164 KB |
Output is correct |
24 |
Correct |
241 ms |
45292 KB |
Output is correct |
25 |
Correct |
236 ms |
44652 KB |
Output is correct |
26 |
Correct |
241 ms |
44396 KB |
Output is correct |
27 |
Correct |
234 ms |
44524 KB |
Output is correct |
28 |
Correct |
239 ms |
44908 KB |
Output is correct |
29 |
Correct |
1381 ms |
82284 KB |
Output is correct |
30 |
Correct |
1220 ms |
81572 KB |
Output is correct |
31 |
Correct |
1213 ms |
83608 KB |
Output is correct |
32 |
Correct |
1418 ms |
82176 KB |
Output is correct |
33 |
Correct |
1406 ms |
82204 KB |
Output is correct |
34 |
Correct |
1419 ms |
79808 KB |
Output is correct |
35 |
Correct |
1425 ms |
79540 KB |
Output is correct |
36 |
Correct |
1418 ms |
79724 KB |
Output is correct |
37 |
Correct |
1378 ms |
81004 KB |
Output is correct |
38 |
Correct |
1075 ms |
87788 KB |
Output is correct |
39 |
Correct |
1066 ms |
87788 KB |
Output is correct |
40 |
Correct |
1058 ms |
84796 KB |
Output is correct |
41 |
Correct |
1045 ms |
83948 KB |
Output is correct |
42 |
Correct |
1064 ms |
83980 KB |
Output is correct |
43 |
Correct |
1059 ms |
85692 KB |
Output is correct |
44 |
Correct |
1114 ms |
87436 KB |
Output is correct |
45 |
Correct |
1125 ms |
87368 KB |
Output is correct |
46 |
Correct |
1107 ms |
84204 KB |
Output is correct |
47 |
Correct |
1107 ms |
83864 KB |
Output is correct |
48 |
Correct |
1107 ms |
83816 KB |
Output is correct |
49 |
Correct |
1112 ms |
85996 KB |
Output is correct |
50 |
Correct |
1195 ms |
85144 KB |
Output is correct |
51 |
Correct |
1202 ms |
85168 KB |
Output is correct |
52 |
Correct |
1177 ms |
82672 KB |
Output is correct |
53 |
Correct |
1184 ms |
82428 KB |
Output is correct |
54 |
Correct |
1182 ms |
82292 KB |
Output is correct |
55 |
Correct |
1187 ms |
84008 KB |
Output is correct |