This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=5e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
struct node
{
ll f,s,val;
node()
{
f=-inf;
s=-inf;
val=-inf;
}
node operator+(const node&o)
{
node ans;
ans.f=max(f,o.f);
ans.s=max(s,o.s);
ans.val=max({-inf,val,o.val,f+o.s});
return ans;
}
}st[4*maxN];
ll n;
//ll st[4*maxN];
void update(ll t,ll pos,ll val,ll id=1,ll l=1,ll r=n)
{
if(pos>r||pos<l) return ;
if(l==r)
{
if(t==1) st[id].f=val;
else st[id].s=val;
st[id].val=max(-inf,st[id].f+st[id].s);
return;
}
ll mid=l+r>>1;
if(pos<=mid) update(t,pos,val,id*2,l,mid);
else update(t,pos,val,id*2+1,mid+1,r);
st[id]=st[id*2]+st[id*2+1];
}
node get(ll i,ll j,ll id=1,ll l=1,ll r=n)
{
if(j<l||r<i) return node();
if(i<=l&&r<=j) return st[id];
ll mid=l+r>>1;
return get(i,j,id*2,l,mid)+get(i,j,id*2+1,mid+1,r);
}
ll q;
ll a[maxN];
vector<pli>adu[maxN];
ll ans[maxN];
void solve()
{
cin >> n;
deque<int>dq;
for(int i=1;i<=n;i++)
{
cin >> a[i];
update(0,i,a[i]);
}
cin >> q;
for(int i=1;i<=q;i++)
{
ll l,r;
cin >> l >> r;
adu[l].pb({r,i});
}
vector<pli>vd;
for(int i=1;i<=n;i++)
{
while(dq.size()>0&&a[i]>=a[dq.back()])
{
vd.pb({dq.back(),i});
dq.pop_back();
}
if(dq.size()>0)
{
vd.pb({dq.back(),i});
}
dq.pb(i);
}
sort(vd.begin(),vd.end());
ll j=(int)vd.size()-1;
for(int i=n;i>=1;i--)
{
while(j>=0&&vd[j].fi>=i)
{
update(1,vd[j].se*2-vd[j].fi,a[vd[j].fi]+a[vd[j].se]);
j--;
}
for(auto zz:adu[i])
{
ans[zz.se]=get(i,zz.fi).val;
}
}
for(int i=1;i<=q;i++) cout << ans[i]<<'\n';
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message (stderr)
bitaro.cpp: In function 'void update(ll, ll, ll, ll, ll, ll)':
bitaro.cpp:43:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | ll mid=l+r>>1;
| ~^~
bitaro.cpp: In function 'node get(ll, ll, ll, ll, ll)':
bitaro.cpp:52:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | ll mid=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... |