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 F first
#define S second
#define pb push_back
#define ll long long
#define ii pair<int,int>
#define iii pair<int,pair<int,int>>
#define iil pair<ll,ll>
#define iiil pair<ll,pair<ll,ll>>
#define oo 1e18
#define check(n) (prime[n>>6]&(1<<((n&63)>>1)))
#define set(n) prime[n>>6]|=(1<<((n&63)>>1))
using namespace std;
const int N=5e5+5;
const ll Mod=1e9+7;
const ll MOD=13141702292180207;
const int dx[]={-1,0,0,1};
const int dy[]={0,-1,1,0};
int n;
ll a[N], val[4*N], lz[4*N], gt[4*N], ans[N];
stack<int> s;
vector<int> b[N];
vector<ii> c[N];
void build(int id,int l,int r)
{
if (l>r) return;
if (l==r)
{
val[id]=a[l];
return;
}
int mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
val[id]=max(val[id*2], val[id*2+1]);
return;
}
void down(int id)
{
lz[id*2]=max(lz[id*2], lz[id]);
lz[id*2+1]=max(lz[id*2+1], lz[id]);
gt[id*2]=max(gt[id*2], lz[id*2] + val[id*2]);
gt[id*2+1]=max(gt[id*2+1], lz[id*2+1] + val[id*2+1]);
}
void up(int id,int l,int r,int u,int v,ll x)
{
if (l>r || v<l || u>r || u>v) return;
if (u<=l && r<=v)
{
lz[id]=max(lz[id],x);
gt[id]=max(gt[id],val[id]+lz[id]);
return;
}
down(id);
int mid=(l+r)/2;
up(id*2,l,mid,u,v,x);
up(id*2+1,mid+1,r,u,v,x);
gt[id]=max(gt[id*2+1], gt[id]);
gt[id]=max(gt[id*2], gt[id]);
return;
}
ll get(int id,int l,int r,int u,int v)
{
if (l>r || u>v || u>r || v<l) return -1e17;
if (u<=l && r<=v) return gt[id];
down(id);
int mid=(l+r)/2;
return max(get(id*2,l,mid,u,v), get(id*2+1,mid+1,r,u,v));
}
void slove()
{
cin >> n; a[0]=1e18;
for (int i=1;i<=n;i++) cin >> a[i];
s.push(0);
for (int i=n;i>=1;i--)
{
while (a[s.top()]<a[i]) s.pop();
if (s.top()>0) b[i].pb(s.top());
s.push(i);
}
while(!s.empty()) s.pop();
s.push(0);
for (int i=1;i<=n;i++)
{
while (a[s.top()]<a[i]) s.pop();
if (s.top()>0) b[s.top()].pb(i);
s.push(i);
}
build(1,1,n);
int q;
cin >> q;
for (int i=1;i<=q;i++)
{
int l,r;
cin >> l >> r;
c[l].pb(ii(r,i));
}
for (int i=n;i>=1;i--)
{
for (int j: b[i])
if (j+j-i<=n) up(1,1,n,j+j-i,n,a[i]+a[j]);
for (ii u: c[i]) ans[u.S]=get(1,1,n,i,u.F);
}
for (int i=1;i<=q;i++) cout << ans[i] << '\n';
}
int main()
{
// freopen("test.inp","r",stdin);
// freopen("test.out","w",stdout);
ios_base::sync_with_stdio(0);
cout.tie(0); cin.tie(0);
int T=1;
//cin >> T;
while(T--) slove();
}
# | 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... |