#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAX_VAL=500*1000+5,INFINI=1000*1000*1000,DECA=16;//(1<<19);
struct noeud {
int maxDF=-INFINI,maxD=-INFINI,maxF=-INFINI;
};
noeud prod(noeud a,noeud b) {
return {max(max(a.maxDF,b.maxDF),a.maxD+b.maxF),max(a.maxD,b.maxD),max(a.maxF,b.maxF)};
}
struct segtree {
vector<noeud> tree;
int n;
segtree(int _n) {
n=_n;
tree.resize(2*n);
}
void modif(int pos,noeud nouvVal) {
pos+=n;
tree[pos]=nouvVal;
while (pos>1) {
pos/=2;
tree[pos]=prod(tree[2*pos],tree[2*pos+1]);
}
}
void ajout(int pos,noeud nouvVal) {
modif(pos,prod(nouvVal,tree[pos+n]));
}
noeud calcMax(int deb,int fin) {
noeud repDeb,repFin;
deb+=n;
fin+=n;
while (deb<=fin) {
if (deb%2==1) {
repDeb=prod(repDeb,tree[deb]);
deb++;
}
if (fin%2==0) {
repFin=prod(tree[fin],repFin);
fin--;
}
deb/=2;
fin/=2;
}
return prod(repDeb,repFin);
}
};
int nbVal,nbReq,debReq,finReq;
int val[MAX_VAL];
int rep[MAX_VAL];
segtree arbreMax(DECA);
vector<pair<int,int>> interCommence[MAX_VAL];
vector<int> enCours;
vector<pair<int,int>> listeReq[MAX_VAL];
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>nbVal;
for (int i=0;i<nbVal;i++) {
cin>>val[i];
}
enCours.push_back(0);
for (int i=1;i<nbVal;i++) {
while (!enCours.empty() and val[enCours.back()]<=val[i]) {
if (nbVal-i>i-enCours.back()) {
interCommence[enCours.back()].push_back({2*i-enCours.back(),val[i]+val[enCours.back()]});
//cout<<enCours.back()<<" "<<2*i-enCours.back()<<" "<<val[i]+val[enCours.back()]<<endl;
}
enCours.pop_back();
}
if (!enCours.empty()) {
if (nbVal-i>i-enCours.back()) {
interCommence[enCours.back()].push_back({2*i-enCours.back(),val[i]+val[enCours.back()]});
//cout<<enCours.back()<<" "<<2*i-enCours.back()<<" "<<val[i]+val[enCours.back()]<<endl;
}
}
enCours.push_back(i);
}
cin>>nbReq;
for (int ireq=1;ireq<=nbReq;ireq++) {
cin>>debReq>>finReq;
listeReq[debReq-1].push_back({finReq-1,ireq});
}
for (int deb=nbVal-1;deb>=0;deb--) {
arbreMax.ajout(deb,{-INFINI,-INFINI,val[deb]});
for (auto i:interCommence[deb]) {
arbreMax.ajout(i.first,{-INFINI,i.second,-INFINI});
}
for (auto i:listeReq[deb]) {
rep[i.second]=arbreMax.calcMax(deb,i.first).maxDF;
}
}
//cout<<endl;
for (int ireq=1;ireq<=nbReq;ireq++) {
cout<<rep[ireq]<<"\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23812 KB |
Output is correct |
2 |
Incorrect |
14 ms |
23824 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23812 KB |
Output is correct |
2 |
Incorrect |
14 ms |
23824 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
80 ms |
72232 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23812 KB |
Output is correct |
2 |
Incorrect |
14 ms |
23824 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |