#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, A[500005], q, ans[500050];
vector <pi> Q[500005];
struct node{
int s, e, m, val, val2, lz;
node *l, *r;
node(int _s, int _e){
s = _s, e = _e, m = (s + e) >> 1;
if(s != e)l = new node(s, m), r = new node(m+1, e), val = max(l->val, r->val);
else val = A[s];
val2 = lz = 0;
}
void prop(){
if(lz){
val2 = max(val2, val + lz);
if(s != e)l->lz = max(l->lz, lz), r->lz = max(r->lz, lz);
lz = 0;
}
}
void upd(int a, int b, int c){
prop();
if(a == s && b== e)lz = max(lz, c);
else{
if(b <= m)l->upd(a, b, c);
else if(a > m)r->upd(a, b, c);
else l->upd(a, m, c), r->upd(m+1, b, c);
l->prop(), r->prop();
val2 = max(l->val2, r->val2);
}
}
int qry(int a, int b){
prop();
if(a == s && b == e)return val2;
if(b <= m)return l->qry(a, b);
if(a > m)return r->qry(a, b);
return max(l->qry(a, m), r->qry(m+1, b));
}
}*root;
vector <int> stuf[500005];
void solve(){
cin >> n;
for(int i = 1; i <= n; i++)cin >> A[i];
root = new node(1, n);
cin >> q;
for(int i = 1; i <= q; i++){
int l, r; cin >> l >> r;
Q[l].push_back({r, i});
}
stack <int> s;
for(int i = 1; i <= n; i++){
while(!s.empty() && A[s.top()] < A[i])s.pop();
if(!s.empty())stuf[s.top()].push_back(i);
s.push(i);
}
while(!s.empty())s.pop();
for(int i = n; i >= 1; i--){
while(!s.empty() && A[s.top()] < A[i])s.pop();
if(!s.empty())stuf[i].push_back(s.top());
s.push(i);
}
for(int i = n; i >= 1; i--){
for(auto j : stuf[i]){
if(j * 2 - i <= n)root->upd(j * 2 - i, n, A[i] + A[j]);
}
for(auto [a, b] : Q[i])ans[b] = root->qry(i, a);
}
for(int i = 1; i <= q; i++)cout << ans[i] << '\n';
}
main(){
ios::sync_with_stdio(0);cin.tie(0);
int tc = 1;
//cin >> tc;
for(int tc1=1;tc1<=tc;tc1++){
// cout << "Case #" << tc1 << ": ";
solve();
}
}
Compilation message
jumps.cpp:84:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
84 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27324 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27228 KB |
Output is correct |
7 |
Correct |
7 ms |
27320 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
6 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27324 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27228 KB |
Output is correct |
7 |
Correct |
7 ms |
27320 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
6 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27260 KB |
Output is correct |
11 |
Correct |
183 ms |
53084 KB |
Output is correct |
12 |
Correct |
181 ms |
53248 KB |
Output is correct |
13 |
Correct |
182 ms |
53112 KB |
Output is correct |
14 |
Correct |
178 ms |
53076 KB |
Output is correct |
15 |
Correct |
207 ms |
53172 KB |
Output is correct |
16 |
Correct |
195 ms |
52588 KB |
Output is correct |
17 |
Correct |
181 ms |
52436 KB |
Output is correct |
18 |
Correct |
185 ms |
52524 KB |
Output is correct |
19 |
Correct |
178 ms |
53052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
130 ms |
68128 KB |
Output is correct |
2 |
Correct |
78 ms |
68432 KB |
Output is correct |
3 |
Correct |
80 ms |
69460 KB |
Output is correct |
4 |
Correct |
126 ms |
69012 KB |
Output is correct |
5 |
Correct |
129 ms |
69148 KB |
Output is correct |
6 |
Correct |
119 ms |
69208 KB |
Output is correct |
7 |
Correct |
120 ms |
69104 KB |
Output is correct |
8 |
Correct |
126 ms |
69360 KB |
Output is correct |
9 |
Correct |
132 ms |
69456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
27228 KB |
Output is correct |
2 |
Correct |
6 ms |
27228 KB |
Output is correct |
3 |
Correct |
6 ms |
27228 KB |
Output is correct |
4 |
Correct |
6 ms |
27324 KB |
Output is correct |
5 |
Correct |
6 ms |
27228 KB |
Output is correct |
6 |
Correct |
6 ms |
27228 KB |
Output is correct |
7 |
Correct |
7 ms |
27320 KB |
Output is correct |
8 |
Correct |
6 ms |
27224 KB |
Output is correct |
9 |
Correct |
6 ms |
27228 KB |
Output is correct |
10 |
Correct |
6 ms |
27260 KB |
Output is correct |
11 |
Correct |
183 ms |
53084 KB |
Output is correct |
12 |
Correct |
181 ms |
53248 KB |
Output is correct |
13 |
Correct |
182 ms |
53112 KB |
Output is correct |
14 |
Correct |
178 ms |
53076 KB |
Output is correct |
15 |
Correct |
207 ms |
53172 KB |
Output is correct |
16 |
Correct |
195 ms |
52588 KB |
Output is correct |
17 |
Correct |
181 ms |
52436 KB |
Output is correct |
18 |
Correct |
185 ms |
52524 KB |
Output is correct |
19 |
Correct |
178 ms |
53052 KB |
Output is correct |
20 |
Correct |
130 ms |
68128 KB |
Output is correct |
21 |
Correct |
78 ms |
68432 KB |
Output is correct |
22 |
Correct |
80 ms |
69460 KB |
Output is correct |
23 |
Correct |
126 ms |
69012 KB |
Output is correct |
24 |
Correct |
129 ms |
69148 KB |
Output is correct |
25 |
Correct |
119 ms |
69208 KB |
Output is correct |
26 |
Correct |
120 ms |
69104 KB |
Output is correct |
27 |
Correct |
126 ms |
69360 KB |
Output is correct |
28 |
Correct |
132 ms |
69456 KB |
Output is correct |
29 |
Correct |
918 ms |
157776 KB |
Output is correct |
30 |
Correct |
865 ms |
158692 KB |
Output is correct |
31 |
Correct |
779 ms |
158684 KB |
Output is correct |
32 |
Correct |
907 ms |
157616 KB |
Output is correct |
33 |
Correct |
865 ms |
158040 KB |
Output is correct |
34 |
Correct |
847 ms |
155484 KB |
Output is correct |
35 |
Correct |
839 ms |
155208 KB |
Output is correct |
36 |
Correct |
866 ms |
155412 KB |
Output is correct |
37 |
Correct |
856 ms |
156400 KB |
Output is correct |
38 |
Correct |
491 ms |
160340 KB |
Output is correct |
39 |
Correct |
491 ms |
160340 KB |
Output is correct |
40 |
Correct |
477 ms |
156940 KB |
Output is correct |
41 |
Correct |
480 ms |
156584 KB |
Output is correct |
42 |
Correct |
472 ms |
156436 KB |
Output is correct |
43 |
Correct |
484 ms |
158104 KB |
Output is correct |
44 |
Correct |
539 ms |
159824 KB |
Output is correct |
45 |
Correct |
545 ms |
160064 KB |
Output is correct |
46 |
Correct |
530 ms |
156740 KB |
Output is correct |
47 |
Correct |
541 ms |
156516 KB |
Output is correct |
48 |
Correct |
538 ms |
156496 KB |
Output is correct |
49 |
Correct |
551 ms |
158548 KB |
Output is correct |
50 |
Correct |
656 ms |
159284 KB |
Output is correct |
51 |
Correct |
643 ms |
159292 KB |
Output is correct |
52 |
Correct |
633 ms |
156864 KB |
Output is correct |
53 |
Correct |
643 ms |
156504 KB |
Output is correct |
54 |
Correct |
647 ms |
156496 KB |
Output is correct |
55 |
Correct |
642 ms |
158292 KB |
Output is correct |