#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 5e5 + 2;
ll a[N + 2];
int b[N + 2];
int c[N + 2];
vector < pair < int , int > > query[N + 2];
ll st[4 * N + 2];
ll mx1[4 * N + 2];
ll mx2[4 * N + 2];
vector < int > d[N + 2];
vector < int > f[N + 2];
void update(int id , int l , int r , int post , ll val ){
if(l > post || r < post){
return;
}
if(l == r){
mx1[id] = max(mx1[id],a[l]);
mx2[id] = max(mx2[id],val);
st[id] = mx1[id] + mx2[id];
return ;
}
int mid = (l + r) / 2;
update(id * 2 , l , mid , post , val);
update(id * 2 + 1 , mid + 1 , r , post , val);
st[id] = max({st[id * 2 + 1] , st[id * 2] , mx2[id * 2] + mx1[id * 2 + 1]});
mx1[id] = max(mx1[id * 2] , mx1[id * 2 + 1]);
mx2[id ] = max(mx2[id * 2 + 1] , mx2[id * 2]);
}
pair < ll , pair <ll , ll > > get(int id , int l , int r ,int u , int v ){
if(l > v || r < u)return {0 , {0 , 0 }};
pair < ll , pair <ll , ll > > ans;
if(u <= l && r <= v){
return {st[id] , {mx1[id], mx2[id]}};
}
int mid =(l + r) /2;
auto[max1 , cur1] = get(id * 2 , l , mid ,u ,v);
auto[max2 , cur2] = get(id * 2 + 1 , mid + 1 , r ,u ,v);
ans.first = max({max1 , max2 , cur1.second + cur2.first});
ans.second.first = max(cur2.first, cur1.first);
ans.second.second = max(cur2.second ,cur1.second);
return ans;
}
ll res[N + 2];
void solve() {
int n ,q ;
cin >> n ;
stack< int > q1;
stack< int > q2;
for(int i = 1 ;i <= n ;i ++){
cin >> a[i];
}
q1.push(n + 1);
q2.push(n + 2);
a[n + 1] = INT64_MAX;
a[n + 2] = INT64_MIN;
for(int i = n ; i >= 1 ; i --){
while(a[q1.top()] < a[i])q1.pop();
while(a[q2.top()] >= a[i])q2.pop();
b[i] = q1.top();
c[i] = q2.top();
q1.push(i);
q2.push(i);
}
while(q1.size())q1.pop();
while(q2.size())q2.pop();
for(int i = 1; i <= n ;i ++){
while(q1.size() && a[q1.top()] < a[i])q1.pop();
while(q2.size()&& a[q2.top()] >= a[i])q2.pop();
if(q1.size())d[q1.top()].push_back(i);
if(q2.size())f[q2.top()].push_back(i);
q1.push(i);
q2.push(i);
}
cin >> q;
for(int i = 1; i <= q; i ++){
int l , r;
cin >> l >> r;
query[l].push_back({r , i});
}
for(int i = n ;i >= 1 ;i --){
update(1 , 1 , n , 2 * b[i] - i , a[i] + a[b[i]]);
update(1 , 1 , n , 2 * c[i] - i , a[i] + a[c[i]]);
for(auto vl : d[i]){
update(1 , 1 , n , 2 * vl - i , a[i] + a[vl]);
}
for(auto vl : f[i]){
update(1 , 1 , n , 2 * vl - i , a[i] + a[vl]);
}
for(auto[r , id] : query[i]){
res[id] = get(1 , 1 , n , i , r).first;
}
}
for(int i = 1; i <= q; i ++)cout << res[i] << "\n";
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
#define _ "digit."
if (fopen(_ "inp", "r")) {
freopen(_ "inp", "r", stdin);
freopen(_ "out", "w", stdout);
}
solve();
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen(_ "inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
jumps.cpp:107:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
107 | freopen(_ "out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
45400 KB |
Output is correct |
2 |
Correct |
10 ms |
45404 KB |
Output is correct |
3 |
Correct |
9 ms |
45548 KB |
Output is correct |
4 |
Correct |
9 ms |
45404 KB |
Output is correct |
5 |
Correct |
10 ms |
45404 KB |
Output is correct |
6 |
Correct |
10 ms |
45660 KB |
Output is correct |
7 |
Correct |
10 ms |
45404 KB |
Output is correct |
8 |
Correct |
10 ms |
45404 KB |
Output is correct |
9 |
Correct |
9 ms |
45496 KB |
Output is correct |
10 |
Correct |
10 ms |
45400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
45400 KB |
Output is correct |
2 |
Correct |
10 ms |
45404 KB |
Output is correct |
3 |
Correct |
9 ms |
45548 KB |
Output is correct |
4 |
Correct |
9 ms |
45404 KB |
Output is correct |
5 |
Correct |
10 ms |
45404 KB |
Output is correct |
6 |
Correct |
10 ms |
45660 KB |
Output is correct |
7 |
Correct |
10 ms |
45404 KB |
Output is correct |
8 |
Correct |
10 ms |
45404 KB |
Output is correct |
9 |
Correct |
9 ms |
45496 KB |
Output is correct |
10 |
Correct |
10 ms |
45400 KB |
Output is correct |
11 |
Correct |
217 ms |
64620 KB |
Output is correct |
12 |
Correct |
212 ms |
64524 KB |
Output is correct |
13 |
Correct |
209 ms |
64580 KB |
Output is correct |
14 |
Correct |
231 ms |
64780 KB |
Output is correct |
15 |
Correct |
224 ms |
64860 KB |
Output is correct |
16 |
Correct |
221 ms |
64116 KB |
Output is correct |
17 |
Correct |
224 ms |
64028 KB |
Output is correct |
18 |
Correct |
217 ms |
64056 KB |
Output is correct |
19 |
Correct |
218 ms |
64548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
179 ms |
68280 KB |
Output is correct |
2 |
Correct |
105 ms |
69316 KB |
Output is correct |
3 |
Correct |
102 ms |
69284 KB |
Output is correct |
4 |
Correct |
181 ms |
68296 KB |
Output is correct |
5 |
Correct |
220 ms |
68692 KB |
Output is correct |
6 |
Correct |
182 ms |
67756 KB |
Output is correct |
7 |
Correct |
180 ms |
67568 KB |
Output is correct |
8 |
Correct |
175 ms |
67668 KB |
Output is correct |
9 |
Correct |
181 ms |
67888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
45400 KB |
Output is correct |
2 |
Correct |
10 ms |
45404 KB |
Output is correct |
3 |
Correct |
9 ms |
45548 KB |
Output is correct |
4 |
Correct |
9 ms |
45404 KB |
Output is correct |
5 |
Correct |
10 ms |
45404 KB |
Output is correct |
6 |
Correct |
10 ms |
45660 KB |
Output is correct |
7 |
Correct |
10 ms |
45404 KB |
Output is correct |
8 |
Correct |
10 ms |
45404 KB |
Output is correct |
9 |
Correct |
9 ms |
45496 KB |
Output is correct |
10 |
Correct |
10 ms |
45400 KB |
Output is correct |
11 |
Correct |
217 ms |
64620 KB |
Output is correct |
12 |
Correct |
212 ms |
64524 KB |
Output is correct |
13 |
Correct |
209 ms |
64580 KB |
Output is correct |
14 |
Correct |
231 ms |
64780 KB |
Output is correct |
15 |
Correct |
224 ms |
64860 KB |
Output is correct |
16 |
Correct |
221 ms |
64116 KB |
Output is correct |
17 |
Correct |
224 ms |
64028 KB |
Output is correct |
18 |
Correct |
217 ms |
64056 KB |
Output is correct |
19 |
Correct |
218 ms |
64548 KB |
Output is correct |
20 |
Correct |
179 ms |
68280 KB |
Output is correct |
21 |
Correct |
105 ms |
69316 KB |
Output is correct |
22 |
Correct |
102 ms |
69284 KB |
Output is correct |
23 |
Correct |
181 ms |
68296 KB |
Output is correct |
24 |
Correct |
220 ms |
68692 KB |
Output is correct |
25 |
Correct |
182 ms |
67756 KB |
Output is correct |
26 |
Correct |
180 ms |
67568 KB |
Output is correct |
27 |
Correct |
175 ms |
67668 KB |
Output is correct |
28 |
Correct |
181 ms |
67888 KB |
Output is correct |
29 |
Correct |
960 ms |
118532 KB |
Output is correct |
30 |
Correct |
753 ms |
119712 KB |
Output is correct |
31 |
Correct |
779 ms |
119888 KB |
Output is correct |
32 |
Correct |
953 ms |
118176 KB |
Output is correct |
33 |
Correct |
929 ms |
118272 KB |
Output is correct |
34 |
Correct |
961 ms |
116116 KB |
Output is correct |
35 |
Correct |
973 ms |
115788 KB |
Output is correct |
36 |
Correct |
919 ms |
115756 KB |
Output is correct |
37 |
Correct |
935 ms |
117108 KB |
Output is correct |
38 |
Correct |
702 ms |
124028 KB |
Output is correct |
39 |
Correct |
716 ms |
123944 KB |
Output is correct |
40 |
Correct |
693 ms |
120656 KB |
Output is correct |
41 |
Correct |
691 ms |
120080 KB |
Output is correct |
42 |
Correct |
717 ms |
120080 KB |
Output is correct |
43 |
Correct |
704 ms |
122164 KB |
Output is correct |
44 |
Correct |
755 ms |
123452 KB |
Output is correct |
45 |
Correct |
732 ms |
123344 KB |
Output is correct |
46 |
Correct |
727 ms |
120292 KB |
Output is correct |
47 |
Correct |
734 ms |
119888 KB |
Output is correct |
48 |
Correct |
729 ms |
119832 KB |
Output is correct |
49 |
Correct |
738 ms |
121936 KB |
Output is correct |
50 |
Correct |
799 ms |
121568 KB |
Output is correct |
51 |
Correct |
798 ms |
121524 KB |
Output is correct |
52 |
Correct |
788 ms |
119072 KB |
Output is correct |
53 |
Correct |
781 ms |
118544 KB |
Output is correct |
54 |
Correct |
854 ms |
118696 KB |
Output is correct |
55 |
Correct |
878 ms |
120404 KB |
Output is correct |