#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,idReq,posRep,debInter,midInter;
int val[MAX_VAL];
int maxi[MAX_VAL];
int rep[MAX_VAL];
vector<int> enCours;
vector<pair<int,int>> paireInter;
vector<tuple<int,int,int>> listeReq;
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);
}
sort(paireInter.begin(),paireInter.end());
/*for (auto i:paireInter) {
cout<<i.first<<" "<<i.second<<endl;
}
cout<<endl;*/
cin>>nbReq;
for (int ireq=1;ireq<=nbReq;ireq++) {
cin>>debReq>>finReq;
listeReq.push_back(make_tuple(debReq,finReq,ireq));
}
sort(listeReq.begin(),listeReq.end());
for (auto r:listeReq) {
debReq=get<0>(r);
finReq=get<1>(r);
idReq=get<2>(r);
maxi[finReq+1]=0;
for (int i=finReq;i>=debReq;i--) {
maxi[i]=max(maxi[i+1],val[i]);
}
for (int i=posRep;i<(int)paireInter.size();i++) {
debInter=paireInter[i].first;
midInter=paireInter[i].second;
if (debInter>=debReq and 2*midInter-debInter<=finReq and rep[idReq]<val[debInter]+val[midInter]+maxi[2*midInter-debInter]) {
rep[idReq]=val[debInter]+val[midInter]+maxi[2*midInter-debInter];
posRep=i;
}
}
//cout<<posRep<<" ";
}
//cout<<endl;
for (int ireq=1;ireq<=nbReq;ireq++) {
cout<<rep[ireq]<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
11880 KB |
Output is correct |
2 |
Correct |
22 ms |
8468 KB |
Output is correct |
3 |
Correct |
24 ms |
11972 KB |
Output is correct |
4 |
Correct |
49 ms |
11972 KB |
Output is correct |
5 |
Correct |
42 ms |
11920 KB |
Output is correct |
6 |
Correct |
36 ms |
11232 KB |
Output is correct |
7 |
Correct |
37 ms |
11228 KB |
Output is correct |
8 |
Correct |
35 ms |
11200 KB |
Output is correct |
9 |
Correct |
38 ms |
11508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |