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>
#pragma optimize("Ofast,unroll-loops")
#pragma target("avx2")
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define mem(a,i) memset(a,i,sizeof(a))
#define sz(s) (int)s.size()
#define y1 yy
#define ppb pop_back
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b) __gcd(a,b)
#define in insert
#define int ll
const int MAX=2e5+10;
const int B=500;
const int maxB=1000;
const int N=104;
const int block=450;
const int inf=2e9;
const int mod=1e9+7;
const int mod1=1e9+9;
const ld eps=1e-9;
int dx[8]={1,0,-1,0,1,-1,-1,1};
int dy[8]={0,1,0,-1,1,-1,1,-1};
int binpow(int a,int n){
if(!n)return 1;
if(n%2==1)return a*binpow(a,n-1)%mod;
int k=binpow(a,n/2);
return k*k%mod;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n,q;
int l[MAX],r[MAX];
int a[MAX];
int nxt[MAX];
int lg[MAX],sp[MAX][21];
int get(int l,int r){
int g=lg[r-l+1];
return max(sp[l][g],sp[r-(1<<g)+1][g]);
}
void prec(){
for(int i=1;i<=n;i++){
sp[i][0]=a[i];
}
for(int i=2;i<=n;i++)lg[i]=lg[i/2]+1;
for(int i=1;i<20;i++){
for(int j=1;j<=n;j++){
int r=j+(1<<(i-1));
r=min(r,n);
sp[j][i]=max(sp[j][i-1],sp[r][i-1]);
}
}
}
int f[5010][5010];
void solve(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
prec();
cin>>q;
if(n<=5000){
for(int l=1;l<=n;l++){
for(int r=l+2;r<=n;r++){
f[l][r]=a[l]+a[r]+get(l+1,(l+r)/2);
}
}
for(int s=4;s<=n;s++){
for(int l=1;l+s-1<=n;l++){
int r=l+s-1;
f[l][r]=max({f[l][r],f[l+1][r],f[l][r-1]});
}
}
while(q--){
int l,r;
cin>>l>>r;
cout<<f[l][r]<<"\n";
}
return;
}
for(int i=1;i<=q;i++)cin>>l[i]>>r[i];
for(int i=n;i>=1;i--){
if(a[i]>a[nxt[i+1]])nxt[i]=i;
else nxt[i]=nxt[i+1];
}
int ans=0;
vector<int> vec;
for(int i=2;i<=n;i++){
while(!vec.empty()&&a[i-1]>=a[vec.back()])vec.ppb();
vec.pb(i-1);
int l=0,r=sz(vec)-1;
while(r-l>3){
int m1=l+(r-l)/3;
int m2=r-(r-l)/3;
int r1=-1,r2=-1;
{
int pos=vec[m1];
if(i-pos+i>n)r1=-1;
else{
r1=a[pos]+a[nxt[i-pos+i]]+a[i];
}
}
{
int pos=vec[m2];
if(i-pos+i>n)r2=-1;
else{
r2=a[pos]+a[nxt[i-pos+i]]+a[i];
}
}
if(r1==-1){
l=m1;
continue;
}
if(r1<r2){
l=m1;
}
else r=m2;
}
for(int j=l;j<=r;j++){
int res=-1;
int pos=vec[j];
if(i-pos+i>n)res=-1;
else{
res=a[pos]+a[nxt[i-pos+i]]+a[i];
}
ans=max(ans,res);
}
}
cout<<ans<<"\n";
}
signed main(){
// freopen("triangles.in","r",stdin);
// freopen("triangles.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// prec();
int t=1;
// cin>>t;
while(t--){
solve();
}
}
Compilation message (stderr)
jumps.cpp:3: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
3 | #pragma optimize("Ofast,unroll-loops")
|
jumps.cpp:4: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
4 | #pragma target("avx2")
|
# | 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... |