#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAX_VAL=500*1000+5,INFINI=1000*1000*1000,DECA=(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";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
48340 KB |
Output is correct |
2 |
Correct |
20 ms |
48468 KB |
Output is correct |
3 |
Correct |
20 ms |
48456 KB |
Output is correct |
4 |
Correct |
21 ms |
48440 KB |
Output is correct |
5 |
Correct |
21 ms |
48456 KB |
Output is correct |
6 |
Correct |
21 ms |
48460 KB |
Output is correct |
7 |
Correct |
20 ms |
48456 KB |
Output is correct |
8 |
Correct |
20 ms |
48372 KB |
Output is correct |
9 |
Correct |
19 ms |
48448 KB |
Output is correct |
10 |
Correct |
25 ms |
48384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
48340 KB |
Output is correct |
2 |
Correct |
20 ms |
48468 KB |
Output is correct |
3 |
Correct |
20 ms |
48456 KB |
Output is correct |
4 |
Correct |
21 ms |
48440 KB |
Output is correct |
5 |
Correct |
21 ms |
48456 KB |
Output is correct |
6 |
Correct |
21 ms |
48460 KB |
Output is correct |
7 |
Correct |
20 ms |
48456 KB |
Output is correct |
8 |
Correct |
20 ms |
48372 KB |
Output is correct |
9 |
Correct |
19 ms |
48448 KB |
Output is correct |
10 |
Correct |
25 ms |
48384 KB |
Output is correct |
11 |
Correct |
160 ms |
75708 KB |
Output is correct |
12 |
Correct |
178 ms |
75492 KB |
Output is correct |
13 |
Correct |
160 ms |
75728 KB |
Output is correct |
14 |
Correct |
167 ms |
75736 KB |
Output is correct |
15 |
Correct |
165 ms |
75720 KB |
Output is correct |
16 |
Correct |
174 ms |
75104 KB |
Output is correct |
17 |
Correct |
184 ms |
75112 KB |
Output is correct |
18 |
Correct |
175 ms |
74920 KB |
Output is correct |
19 |
Correct |
164 ms |
75532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
60272 KB |
Output is correct |
2 |
Correct |
74 ms |
56236 KB |
Output is correct |
3 |
Correct |
82 ms |
57812 KB |
Output is correct |
4 |
Correct |
122 ms |
60236 KB |
Output is correct |
5 |
Correct |
110 ms |
60224 KB |
Output is correct |
6 |
Correct |
108 ms |
60256 KB |
Output is correct |
7 |
Correct |
104 ms |
60236 KB |
Output is correct |
8 |
Correct |
114 ms |
60284 KB |
Output is correct |
9 |
Correct |
103 ms |
60240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
48340 KB |
Output is correct |
2 |
Correct |
20 ms |
48468 KB |
Output is correct |
3 |
Correct |
20 ms |
48456 KB |
Output is correct |
4 |
Correct |
21 ms |
48440 KB |
Output is correct |
5 |
Correct |
21 ms |
48456 KB |
Output is correct |
6 |
Correct |
21 ms |
48460 KB |
Output is correct |
7 |
Correct |
20 ms |
48456 KB |
Output is correct |
8 |
Correct |
20 ms |
48372 KB |
Output is correct |
9 |
Correct |
19 ms |
48448 KB |
Output is correct |
10 |
Correct |
25 ms |
48384 KB |
Output is correct |
11 |
Correct |
160 ms |
75708 KB |
Output is correct |
12 |
Correct |
178 ms |
75492 KB |
Output is correct |
13 |
Correct |
160 ms |
75728 KB |
Output is correct |
14 |
Correct |
167 ms |
75736 KB |
Output is correct |
15 |
Correct |
165 ms |
75720 KB |
Output is correct |
16 |
Correct |
174 ms |
75104 KB |
Output is correct |
17 |
Correct |
184 ms |
75112 KB |
Output is correct |
18 |
Correct |
175 ms |
74920 KB |
Output is correct |
19 |
Correct |
164 ms |
75532 KB |
Output is correct |
20 |
Correct |
107 ms |
60272 KB |
Output is correct |
21 |
Correct |
74 ms |
56236 KB |
Output is correct |
22 |
Correct |
82 ms |
57812 KB |
Output is correct |
23 |
Correct |
122 ms |
60236 KB |
Output is correct |
24 |
Correct |
110 ms |
60224 KB |
Output is correct |
25 |
Correct |
108 ms |
60256 KB |
Output is correct |
26 |
Correct |
104 ms |
60236 KB |
Output is correct |
27 |
Correct |
114 ms |
60284 KB |
Output is correct |
28 |
Correct |
103 ms |
60240 KB |
Output is correct |
29 |
Correct |
606 ms |
110980 KB |
Output is correct |
30 |
Correct |
506 ms |
100832 KB |
Output is correct |
31 |
Correct |
501 ms |
104760 KB |
Output is correct |
32 |
Correct |
677 ms |
110972 KB |
Output is correct |
33 |
Correct |
660 ms |
110968 KB |
Output is correct |
34 |
Correct |
560 ms |
108700 KB |
Output is correct |
35 |
Correct |
599 ms |
108364 KB |
Output is correct |
36 |
Correct |
669 ms |
108408 KB |
Output is correct |
37 |
Correct |
582 ms |
109788 KB |
Output is correct |
38 |
Correct |
405 ms |
113608 KB |
Output is correct |
39 |
Correct |
412 ms |
113636 KB |
Output is correct |
40 |
Correct |
411 ms |
110236 KB |
Output is correct |
41 |
Correct |
398 ms |
109684 KB |
Output is correct |
42 |
Correct |
390 ms |
109728 KB |
Output is correct |
43 |
Correct |
391 ms |
111424 KB |
Output is correct |
44 |
Correct |
428 ms |
113876 KB |
Output is correct |
45 |
Correct |
414 ms |
113876 KB |
Output is correct |
46 |
Correct |
425 ms |
110700 KB |
Output is correct |
47 |
Correct |
411 ms |
110396 KB |
Output is correct |
48 |
Correct |
450 ms |
110244 KB |
Output is correct |
49 |
Correct |
423 ms |
112244 KB |
Output is correct |
50 |
Correct |
495 ms |
114100 KB |
Output is correct |
51 |
Correct |
462 ms |
114056 KB |
Output is correct |
52 |
Correct |
452 ms |
111556 KB |
Output is correct |
53 |
Correct |
455 ms |
111308 KB |
Output is correct |
54 |
Correct |
460 ms |
111312 KB |
Output is correct |
55 |
Correct |
448 ms |
112892 KB |
Output is correct |