#include<bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define in insert
#define er erase
#define pb push_back
#define ppb pop_back()
#define ph push
#define pp pop()
#define d3 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define cans cout << ans << "\n";
#define yes cout << "YES" << "\n";
#define no cout << "NO" << "\n";
#define pll pair<ll,ll>
#define lin cout << "\n";
#define sqr 340
#define mod 1000000007
#define mid ((l+r)/2)
#define lc (2*n)
#define rc (2*n+1)
using namespace std;
ll fp(ll x , ll y)
{
if(y==0)
return 1;
ll z = fp(x,y/2);
if(y&1)
return z*z*x;
else
return z*z;
}
int sqrot(ll x)
{
int l = 0 , r = INT_MAX;
while(l<=r)
{
if(mid*mid>=x)
r=mid-1;
else
l=mid+1;
}
return r+1;
}
ll cel(ll x , ll y)
{
return x/y + bool(x/y);
}
string tb(ll x)
{
string s = "";
while(x>0)
{
if(x&1)
s+='1';
else
s+='0';
x/=2;
}
while(s.size()<60)
s+='0';
return s;
}
ll fb(string s)
{
ll x = 0;
ll c = 1;
for(int8_t i = 0 ; 60>i ; i++)
{
if(s[i]=='1')
x+=c;
c*=2;
}
return x;
}
ll seg[800009];
ll a[200009];
void sb(ll n , ll l , ll r)
{
if(l==r)
{
seg[n]=a[l];
return;
}
sb(2*n,l,mid);
sb(2*n+1,mid+1,r);
seg[n]=max(seg[2*n],seg[2*n+1]);
}
ll sg(ll n , ll l , ll r , ll l1 , ll r1)
{
if(l>r1||r<l1)
return -1e18;
if(l>=l1&&r<=r1)
return seg[n];
return max(sg(2*n,l,mid,l1,r1),sg(2*n+1,mid+1,r,l1,r1));
}
int main()
{
d3
ll n;
cin >> n;
for(int i = 1 ; n>=i ; i++)
cin >> a[i];
sb(1,1,n);
pll p[n+1];
pll s[n+1];
ll mx = -1e18;
ll idx = 0;
for(int i = 1 ; n>=i ; i++)
{
if(a[i]>=mx)
{
mx=a[i];
idx=i;
}
p[i].F=mx;
p[i].S=idx;
}
mx = -1e18;
idx = 0;
for(int i = n ; i>0 ; i--)
{
if(a[i]>mx)
{
mx=a[i];
idx=i;
}
s[i].F=mx;
s[i].S=idx;
}
ll ans = -1e18;
for(int i = 2 ; n>i ; i++)
{
ll x = p[i-1].F;
ll idx = p[i-1].S;
ll y = s[i+1].F;
ll idy = s[i+1].S;
ll ans1 = -1e18 , ans2 = -1e18;
if(2*i-idx<=n)
ans1 = x+a[i]+s[2*i-idx].F;
ll w = 1;
ans2 = sg(1,1,n,max(2*i-idy,w),i-1)+a[i]+y;
ans=max(ans,max(ans1,ans2));
}
ll q;
cin >> q;
while(q--)
{
ll l , r;
cin >> l >> r;
cout << ans << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
12928 KB |
Output is correct |
2 |
Correct |
49 ms |
12892 KB |
Output is correct |
3 |
Correct |
41 ms |
12892 KB |
Output is correct |
4 |
Correct |
49 ms |
12892 KB |
Output is correct |
5 |
Correct |
49 ms |
13028 KB |
Output is correct |
6 |
Correct |
51 ms |
13972 KB |
Output is correct |
7 |
Correct |
52 ms |
13916 KB |
Output is correct |
8 |
Correct |
50 ms |
13916 KB |
Output is correct |
9 |
Correct |
48 ms |
14172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |