This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |