#include<iostream>
#include<vector>
#define pa pair<int,int>
#define fi first
#define se second
using namespace std;
using ui=unsigned int;
const int N=5e5+5;
int n,q;
int a[N],p[N];
vector<int> wait[N];
vector<pa> que[N];
int xl[N<<2],xr[N<<2];
bool ok[N<<2];
ui IT[N<<2],ma[N<<2],ans[N];
int lazy[N<<2];
void build(int x=1,int l=1,int r=n){
xl[x]=l, xr[x]=r;
if(l==r){ma[x]=a[l]; return;}
int xm=(l+r)>>1;
build(x<<1,l,xm); build(x<<1|1,xm+1,r);
ma[x]=max(ma[x<<1],ma[x<<1|1]);
}
void fix(int x,int C){
if(C<=lazy[x]) return;
ok[x]=1, lazy[x]=C;
IT[x]=max(IT[x],ma[x]+C);
}
void down(int x){
for(int i=x<<1;i<=(x<<1|1);i++)
fix(i,lazy[x]);
ok[x]=0;
}
void update(int L,int C,int x=1){
if(C<=lazy[x]) return;
if(L<=xl[x]){fix(x,C); return;}
if(ok[x]) down(x);
if(xr[x<<1]>=L) update(L,C,x<<1);
update(L,C,x<<1|1);
IT[x]=max(IT[x<<1],IT[x<<1|1]);
}
ui query(int L,int R,int x=1){
if(xr[x]<L||R<xl[x]) return 0;
if(L<=xl[x]&&xr[x]<=R) return IT[x];
if(ok[x]) down(x);
return max(query(L,R,x<<1),query(L,R,x<<1|1));
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
// freopen("tripos.inp","r",stdin);
// freopen("tripos.out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i]; p[i]=i-1;
while(p[i]&&a[p[i]]<a[i]) p[i]=p[p[i]];
if(p[i]) wait[p[i]].push_back(i);
}
for(int i=n;i>=1;i--){
p[i]=i+1;
while(p[i]<=n&&a[p[i]]<a[i]) p[i]=p[p[i]];
if(p[i]<=n&&a[i]<a[p[i]]) wait[i].push_back(p[i]);
}
build();
cin>>q;
int l,r;
for(int i=1;i<=q;i++){
cin>>l>>r;
que[l].emplace_back(r,i);
}
for(int i=n;i>=1;i--){
for(auto j:wait[i])
if(2*j-i<=n) update(2*j-i,a[i]+a[j]);
for(auto x:que[i]) ans[x.se]=query(i,x.fi);
}
for(int i=1;i<=q;i++) cout<<ans[i]<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23812 KB |
Output is correct |
2 |
Correct |
13 ms |
23884 KB |
Output is correct |
3 |
Correct |
13 ms |
23840 KB |
Output is correct |
4 |
Correct |
15 ms |
23780 KB |
Output is correct |
5 |
Correct |
14 ms |
23772 KB |
Output is correct |
6 |
Correct |
14 ms |
23820 KB |
Output is correct |
7 |
Correct |
13 ms |
23828 KB |
Output is correct |
8 |
Correct |
14 ms |
23884 KB |
Output is correct |
9 |
Correct |
15 ms |
23824 KB |
Output is correct |
10 |
Correct |
13 ms |
23784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23812 KB |
Output is correct |
2 |
Correct |
13 ms |
23884 KB |
Output is correct |
3 |
Correct |
13 ms |
23840 KB |
Output is correct |
4 |
Correct |
15 ms |
23780 KB |
Output is correct |
5 |
Correct |
14 ms |
23772 KB |
Output is correct |
6 |
Correct |
14 ms |
23820 KB |
Output is correct |
7 |
Correct |
13 ms |
23828 KB |
Output is correct |
8 |
Correct |
14 ms |
23884 KB |
Output is correct |
9 |
Correct |
15 ms |
23824 KB |
Output is correct |
10 |
Correct |
13 ms |
23784 KB |
Output is correct |
11 |
Correct |
287 ms |
42964 KB |
Output is correct |
12 |
Correct |
234 ms |
42852 KB |
Output is correct |
13 |
Correct |
248 ms |
42892 KB |
Output is correct |
14 |
Correct |
310 ms |
42988 KB |
Output is correct |
15 |
Correct |
272 ms |
43012 KB |
Output is correct |
16 |
Correct |
251 ms |
42276 KB |
Output is correct |
17 |
Correct |
270 ms |
42300 KB |
Output is correct |
18 |
Correct |
275 ms |
42188 KB |
Output is correct |
19 |
Correct |
254 ms |
42872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
124 ms |
44296 KB |
Output is correct |
2 |
Correct |
81 ms |
44180 KB |
Output is correct |
3 |
Correct |
79 ms |
44080 KB |
Output is correct |
4 |
Correct |
140 ms |
44344 KB |
Output is correct |
5 |
Correct |
126 ms |
44312 KB |
Output is correct |
6 |
Correct |
130 ms |
43680 KB |
Output is correct |
7 |
Correct |
129 ms |
43580 KB |
Output is correct |
8 |
Correct |
117 ms |
43648 KB |
Output is correct |
9 |
Correct |
123 ms |
43972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23812 KB |
Output is correct |
2 |
Correct |
13 ms |
23884 KB |
Output is correct |
3 |
Correct |
13 ms |
23840 KB |
Output is correct |
4 |
Correct |
15 ms |
23780 KB |
Output is correct |
5 |
Correct |
14 ms |
23772 KB |
Output is correct |
6 |
Correct |
14 ms |
23820 KB |
Output is correct |
7 |
Correct |
13 ms |
23828 KB |
Output is correct |
8 |
Correct |
14 ms |
23884 KB |
Output is correct |
9 |
Correct |
15 ms |
23824 KB |
Output is correct |
10 |
Correct |
13 ms |
23784 KB |
Output is correct |
11 |
Correct |
287 ms |
42964 KB |
Output is correct |
12 |
Correct |
234 ms |
42852 KB |
Output is correct |
13 |
Correct |
248 ms |
42892 KB |
Output is correct |
14 |
Correct |
310 ms |
42988 KB |
Output is correct |
15 |
Correct |
272 ms |
43012 KB |
Output is correct |
16 |
Correct |
251 ms |
42276 KB |
Output is correct |
17 |
Correct |
270 ms |
42300 KB |
Output is correct |
18 |
Correct |
275 ms |
42188 KB |
Output is correct |
19 |
Correct |
254 ms |
42872 KB |
Output is correct |
20 |
Correct |
124 ms |
44296 KB |
Output is correct |
21 |
Correct |
81 ms |
44180 KB |
Output is correct |
22 |
Correct |
79 ms |
44080 KB |
Output is correct |
23 |
Correct |
140 ms |
44344 KB |
Output is correct |
24 |
Correct |
126 ms |
44312 KB |
Output is correct |
25 |
Correct |
130 ms |
43680 KB |
Output is correct |
26 |
Correct |
129 ms |
43580 KB |
Output is correct |
27 |
Correct |
117 ms |
43648 KB |
Output is correct |
28 |
Correct |
123 ms |
43972 KB |
Output is correct |
29 |
Correct |
922 ms |
93236 KB |
Output is correct |
30 |
Correct |
784 ms |
92736 KB |
Output is correct |
31 |
Correct |
763 ms |
92748 KB |
Output is correct |
32 |
Correct |
895 ms |
93292 KB |
Output is correct |
33 |
Correct |
851 ms |
93264 KB |
Output is correct |
34 |
Correct |
814 ms |
90952 KB |
Output is correct |
35 |
Correct |
854 ms |
90976 KB |
Output is correct |
36 |
Correct |
832 ms |
90736 KB |
Output is correct |
37 |
Correct |
857 ms |
92100 KB |
Output is correct |
38 |
Correct |
566 ms |
98976 KB |
Output is correct |
39 |
Correct |
615 ms |
98920 KB |
Output is correct |
40 |
Correct |
553 ms |
95656 KB |
Output is correct |
41 |
Correct |
539 ms |
95116 KB |
Output is correct |
42 |
Correct |
583 ms |
95140 KB |
Output is correct |
43 |
Correct |
561 ms |
96900 KB |
Output is correct |
44 |
Correct |
599 ms |
98396 KB |
Output is correct |
45 |
Correct |
607 ms |
98472 KB |
Output is correct |
46 |
Correct |
587 ms |
95320 KB |
Output is correct |
47 |
Correct |
607 ms |
94732 KB |
Output is correct |
48 |
Correct |
581 ms |
94812 KB |
Output is correct |
49 |
Correct |
599 ms |
96856 KB |
Output is correct |
50 |
Correct |
711 ms |
96536 KB |
Output is correct |
51 |
Correct |
702 ms |
96488 KB |
Output is correct |
52 |
Correct |
666 ms |
94032 KB |
Output is correct |
53 |
Correct |
654 ms |
93700 KB |
Output is correct |
54 |
Correct |
657 ms |
93700 KB |
Output is correct |
55 |
Correct |
647 ms |
95412 KB |
Output is correct |