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>
using namespace std;
#define int long long
const int MAX_VAL=500*1000+5,INFINI=1000*1000*1000;
int nbVal,nbReq,debReq,finReq,rep;
int val[MAX_VAL];
int maxi[MAX_VAL];
vector<int> enCours;
vector<pair<int,int>> paireInter;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>nbVal;
for (int i=1;i<=nbVal;i++) {
cin>>val[i];
}
enCours.push_back(1);
for (int i=2;i<=nbVal;i++) {
while (!enCours.empty() and val[enCours.back()]<=val[i]) {
if (nbVal-i>=i-enCours.back()) {
paireInter.push_back({enCours.back(),i});
}
enCours.pop_back();
}
if (!enCours.empty()) {
if (nbVal-i>=i-enCours.back()) {
paireInter.push_back({enCours.back(),i});
}
}
enCours.push_back(i);
}
/*for (auto i:paireInter) {
cout<<i.first<<" "<<i.second<<endl;
}
cout<<endl;*/
cin>>nbReq;
for (int ireq=1;ireq<=nbReq;ireq++) {
cin>>debReq>>finReq;
maxi[finReq+1]=0;
for (int i=finReq;i>=debReq;i--) {
maxi[i]=max(maxi[i+1],val[i]);
}
rep=0;
for (auto i:paireInter) {
if (i.first>=debReq and 2*i.second-i.first<=finReq) {
//cout<<i.first<<" "<<i.second<<endl;
rep=max(rep,val[i.first]+val[i.second]+maxi[2*i.second-i.first]);
}
}
cout<<rep<<"\n";
}
}
# | 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... |