#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
jumps.cpp: In function 'void update(ll, ll, ll, ll, ll, ll)':
jumps.cpp:43:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | ll mid=l+r>>1;
| ~^~
jumps.cpp: In function 'node get(ll, ll, ll, ll, ll)':
jumps.cpp:52:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
52 | ll mid=l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
58956 KB |
Output is correct |
2 |
Incorrect |
27 ms |
58916 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
58956 KB |
Output is correct |
2 |
Incorrect |
27 ms |
58916 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
203 ms |
64760 KB |
Output is correct |
2 |
Correct |
117 ms |
62712 KB |
Output is correct |
3 |
Correct |
113 ms |
63144 KB |
Output is correct |
4 |
Correct |
194 ms |
64792 KB |
Output is correct |
5 |
Correct |
210 ms |
64836 KB |
Output is correct |
6 |
Correct |
180 ms |
64840 KB |
Output is correct |
7 |
Correct |
176 ms |
64836 KB |
Output is correct |
8 |
Correct |
179 ms |
64784 KB |
Output is correct |
9 |
Correct |
196 ms |
64844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
58956 KB |
Output is correct |
2 |
Incorrect |
27 ms |
58916 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |