#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define jizz ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pb push_back
#define ET cout << "\n"
#define ALL(v) v.begin(),v.end()
#define MP make_pair
#define F first
#define S second
#define MEM(i,j) memset(i,j,sizeof i)
#define DB(a,s,e) {for(int i=s;i<e;++i) cout << a[i] << " ";ET;}
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
struct node
{
ll data,mx,lazy;
}seg[2000005];
const ll INF=1e18;
ll arr[500005],ans[500005],n;
vector<ll> v;
vector<pll> op[500005],qur[500005];
void down(ll rt)
{
if(seg[rt].lazy==-INF) return;
seg[rt<<1].lazy=max(seg[rt<<1].lazy,seg[rt].lazy),seg[rt<<1].data=max(seg[rt<<1].data,seg[rt<<1].mx+seg[rt].lazy);
seg[rt<<1|1].lazy=max(seg[rt<<1|1].lazy,seg[rt].lazy),seg[rt<<1|1].data=max(seg[rt<<1|1].data,seg[rt<<1|1].mx+seg[rt].lazy);
seg[rt].lazy=-INF;
}
node up(node a,node b,ll lazy)
{
return node{max({a.data,b.data,max(a.mx,b.mx)+lazy}),max(a.mx,b.mx),lazy};
}
void build(ll l,ll r,ll rt)
{
if(l==r)
return seg[rt]=node{-INF,arr[l],-INF},void();
ll m=l+r>>1;
build(l,m,rt<<1),build(m+1,r,rt<<1|1);
seg[rt]=up(seg[rt<<1],seg[rt<<1|1],-INF);
}
void modify(ll L,ll R,ll l,ll r,ll rt,ll v)
{
if(l!=r) down(rt);
if(L<=l&&R>=r)
return seg[rt].lazy=max(seg[rt].lazy,v),seg[rt].data=max(seg[rt].data,seg[rt].mx+v),void();
ll m=l+r>>1;
if(L<=m) modify(L,R,l,m,rt<<1,v);
if(R>m) modify(L,R,m+1,r,rt<<1|1,v);
seg[rt]=up(seg[rt<<1],seg[rt<<1|1],seg[rt].lazy);
}
node query(ll L,ll R,ll l,ll r,ll rt)
{
if(l!=r) down(rt);
if(L<=l&&R>=r)
return seg[rt];
ll m=l+r>>1;
if(R<=m) return query(L,R,l,m,rt<<1);
if(L>m) return query(L,R,m+1,r,rt<<1|1);
return up(query(L,R,l,m,rt<<1),query(L,R,m+1,r,rt<<1|1),seg[rt].lazy);
}
int main()
{jizz
ll q,l,r;
cin >> n;
for(int i=1;i<=n;++i)
cin >> arr[i];
build(1,n,1);
for(int i=1;i<=n;++i)
{
for(;!v.empty()&&arr[i]>=arr[v.back()];v.pop_back())
if(2*i-v.back()<=n)
op[v.back()].pb(MP(2*i-v.back(),arr[i]+arr[v.back()]));
if(!v.empty()&&2*i-v.back()<=n)
op[v.back()].pb(MP(2*i-v.back(),arr[i]+arr[v.back()]));
v.pb(i);
}
cin >> q;
for(int i=0;i<q;++i)
cin >> l >> r,qur[l].pb(MP(r,i));
for(int i=n;i>0;--i)
{
for(auto j:op[i])
modify(j.F,n,1,n,1,j.S);
for(auto j:qur[i])
ans[j.S]=query(i,j.F,1,n,1).data;
}
for(int i=0;i<q;++i)
cout << ans[i] << "\n";
}
Compilation message
jumps.cpp: In function 'void build(ll, ll, ll)':
jumps.cpp:44:8: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
ll m=l+r>>1;
~^~
jumps.cpp: In function 'void modify(ll, ll, ll, ll, ll, ll)':
jumps.cpp:54:8: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
ll m=l+r>>1;
~^~
jumps.cpp: In function 'node query(ll, ll, ll, ll, ll)':
jumps.cpp:65:8: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
ll m=l+r>>1;
~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
23800 KB |
Output is correct |
2 |
Correct |
25 ms |
23800 KB |
Output is correct |
3 |
Correct |
25 ms |
23824 KB |
Output is correct |
4 |
Correct |
25 ms |
23804 KB |
Output is correct |
5 |
Correct |
25 ms |
23804 KB |
Output is correct |
6 |
Correct |
25 ms |
23800 KB |
Output is correct |
7 |
Correct |
26 ms |
23928 KB |
Output is correct |
8 |
Correct |
25 ms |
23800 KB |
Output is correct |
9 |
Correct |
25 ms |
23928 KB |
Output is correct |
10 |
Correct |
25 ms |
23836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
23800 KB |
Output is correct |
2 |
Correct |
25 ms |
23800 KB |
Output is correct |
3 |
Correct |
25 ms |
23824 KB |
Output is correct |
4 |
Correct |
25 ms |
23804 KB |
Output is correct |
5 |
Correct |
25 ms |
23804 KB |
Output is correct |
6 |
Correct |
25 ms |
23800 KB |
Output is correct |
7 |
Correct |
26 ms |
23928 KB |
Output is correct |
8 |
Correct |
25 ms |
23800 KB |
Output is correct |
9 |
Correct |
25 ms |
23928 KB |
Output is correct |
10 |
Correct |
25 ms |
23836 KB |
Output is correct |
11 |
Correct |
406 ms |
46020 KB |
Output is correct |
12 |
Correct |
398 ms |
45940 KB |
Output is correct |
13 |
Correct |
398 ms |
46032 KB |
Output is correct |
14 |
Correct |
398 ms |
46200 KB |
Output is correct |
15 |
Correct |
406 ms |
46164 KB |
Output is correct |
16 |
Correct |
406 ms |
45504 KB |
Output is correct |
17 |
Correct |
408 ms |
45432 KB |
Output is correct |
18 |
Correct |
431 ms |
45368 KB |
Output is correct |
19 |
Correct |
397 ms |
45960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
218 ms |
48036 KB |
Output is correct |
2 |
Correct |
131 ms |
43932 KB |
Output is correct |
3 |
Correct |
130 ms |
45600 KB |
Output is correct |
4 |
Correct |
213 ms |
48128 KB |
Output is correct |
5 |
Correct |
211 ms |
48060 KB |
Output is correct |
6 |
Correct |
209 ms |
47992 KB |
Output is correct |
7 |
Correct |
208 ms |
47992 KB |
Output is correct |
8 |
Correct |
231 ms |
47964 KB |
Output is correct |
9 |
Correct |
209 ms |
48120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
23800 KB |
Output is correct |
2 |
Correct |
25 ms |
23800 KB |
Output is correct |
3 |
Correct |
25 ms |
23824 KB |
Output is correct |
4 |
Correct |
25 ms |
23804 KB |
Output is correct |
5 |
Correct |
25 ms |
23804 KB |
Output is correct |
6 |
Correct |
25 ms |
23800 KB |
Output is correct |
7 |
Correct |
26 ms |
23928 KB |
Output is correct |
8 |
Correct |
25 ms |
23800 KB |
Output is correct |
9 |
Correct |
25 ms |
23928 KB |
Output is correct |
10 |
Correct |
25 ms |
23836 KB |
Output is correct |
11 |
Correct |
406 ms |
46020 KB |
Output is correct |
12 |
Correct |
398 ms |
45940 KB |
Output is correct |
13 |
Correct |
398 ms |
46032 KB |
Output is correct |
14 |
Correct |
398 ms |
46200 KB |
Output is correct |
15 |
Correct |
406 ms |
46164 KB |
Output is correct |
16 |
Correct |
406 ms |
45504 KB |
Output is correct |
17 |
Correct |
408 ms |
45432 KB |
Output is correct |
18 |
Correct |
431 ms |
45368 KB |
Output is correct |
19 |
Correct |
397 ms |
45960 KB |
Output is correct |
20 |
Correct |
218 ms |
48036 KB |
Output is correct |
21 |
Correct |
131 ms |
43932 KB |
Output is correct |
22 |
Correct |
130 ms |
45600 KB |
Output is correct |
23 |
Correct |
213 ms |
48128 KB |
Output is correct |
24 |
Correct |
211 ms |
48060 KB |
Output is correct |
25 |
Correct |
209 ms |
47992 KB |
Output is correct |
26 |
Correct |
208 ms |
47992 KB |
Output is correct |
27 |
Correct |
231 ms |
47964 KB |
Output is correct |
28 |
Correct |
209 ms |
48120 KB |
Output is correct |
29 |
Correct |
1432 ms |
100144 KB |
Output is correct |
30 |
Correct |
1128 ms |
90192 KB |
Output is correct |
31 |
Correct |
1117 ms |
93944 KB |
Output is correct |
32 |
Correct |
1372 ms |
100236 KB |
Output is correct |
33 |
Correct |
1324 ms |
100040 KB |
Output is correct |
34 |
Correct |
1310 ms |
99632 KB |
Output is correct |
35 |
Correct |
1314 ms |
99360 KB |
Output is correct |
36 |
Correct |
1336 ms |
99516 KB |
Output is correct |
37 |
Correct |
1308 ms |
100088 KB |
Output is correct |
38 |
Correct |
897 ms |
102680 KB |
Output is correct |
39 |
Correct |
902 ms |
102744 KB |
Output is correct |
40 |
Correct |
875 ms |
101100 KB |
Output is correct |
41 |
Correct |
897 ms |
100828 KB |
Output is correct |
42 |
Correct |
905 ms |
100692 KB |
Output is correct |
43 |
Correct |
875 ms |
101816 KB |
Output is correct |
44 |
Correct |
1000 ms |
103076 KB |
Output is correct |
45 |
Correct |
987 ms |
103008 KB |
Output is correct |
46 |
Correct |
963 ms |
101356 KB |
Output is correct |
47 |
Correct |
1008 ms |
101320 KB |
Output is correct |
48 |
Correct |
974 ms |
101232 KB |
Output is correct |
49 |
Correct |
1000 ms |
102412 KB |
Output is correct |
50 |
Correct |
1104 ms |
103124 KB |
Output is correct |
51 |
Correct |
1129 ms |
103140 KB |
Output is correct |
52 |
Correct |
1086 ms |
102320 KB |
Output is correct |
53 |
Correct |
1104 ms |
102272 KB |
Output is correct |
54 |
Correct |
1087 ms |
102192 KB |
Output is correct |
55 |
Correct |
1110 ms |
103148 KB |
Output is correct |