#include <bits/stdc++.h>
using namespace std;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
const int inf = 1e9;
struct node {
int ab,c,abc;
node() {
ab=c=abc=-inf;
}
node(int _ab, int _c, int _abc) : ab(_ab), c(_c), abc(_abc) {}
};
struct segtree {
node merge(node x, node y) {
return { max(x.ab,y.ab), max(x.c,y.c), max({x.abc,y.abc,x.ab+y.c}) };
return x;
}
int n;
vector<node> t;
void init(int n) {
n += 10;
this->n = n;
t.resize(n*4);
}
void upd(int v, int tl, int tr, int i, int val, int stat) {
if (tl == tr) {
if (stat==0) {
t[v].ab=max(t[v].ab, val);
} else {
t[v].c=max(t[v].c, val);
}
t[v].abc = max(t[v].abc, t[v].ab + t[v].c);
} else {
int tm = (tl+tr)/2;
if (i<=tm) {
upd(2*v,tl,tm,i,val,stat);
} else {
upd(2*v+1,tm+1,tr,i,val,stat);
}
t[v] = merge(t[2*v], t[2*v+1]);
}
}
node qry(int v, int tl, int tr, int l, int r) {
if (l>tr || r<tl) {
return node();
}
if (l <= tl && tr <= r) {
return t[v];
}
int tm = (tl+tr)/2;
return merge(qry(2*v,tl,tm,l,r), qry(2*v+1,tm+1,tr,l,r));
}
};
const int maxn = 5e5+10;
int n;
int A[maxn];
vector<int> B[maxn];
int q;
int ans[maxn];
vector<pair<int,int>> Q[maxn]; //(r,i)
segtree tree;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n;
tree.init(n+10);
for (int i=1; i<=n; i++) {
cin>>A[i];
tree.upd(1,1,n,i,A[i],1);
}
cin>>q;
for (int i=0; i<q; i++) {
int l,r;
cin>>l>>r;
Q[l].push_back({r,i});
}
stack<int> stk;
for (int i=1; i<=n; i++) {
while (!stk.empty() && A[stk.top()] <= A[i]) {
B[stk.top()].push_back(i);
stk.pop();
}
if (!stk.empty()) {
B[stk.top()].push_back(i);
}
stk.push(i);
}
while (!stk.empty()) stk.pop();
for (int i=n; i>=1; i--) {
while (!stk.empty() && A[stk.top()] >= A[i]) {
B[i].push_back(stk.top());
stk.pop();
}
stk.push(i);
}
for (int a=n; a>=1; a--) {
for (int b: B[a]) {
int c = b+(b-a);
if (c <= n) {
tree.upd(1,1,n,c,A[a]+A[b],0);
}
}
for (auto qq: Q[a]) {
int idx = qq.second;
int r = qq.first;
ans[idx] = tree.qry(1,1,n,a,r).abc;
}
}
for (int i=0; i<q; i++) {
cout<<ans[i]<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23808 KB |
Output is correct |
2 |
Correct |
14 ms |
23808 KB |
Output is correct |
3 |
Correct |
14 ms |
23808 KB |
Output is correct |
4 |
Correct |
15 ms |
23808 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
14 ms |
23808 KB |
Output is correct |
7 |
Correct |
14 ms |
23808 KB |
Output is correct |
8 |
Correct |
15 ms |
23808 KB |
Output is correct |
9 |
Correct |
14 ms |
23936 KB |
Output is correct |
10 |
Correct |
14 ms |
23808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23808 KB |
Output is correct |
2 |
Correct |
14 ms |
23808 KB |
Output is correct |
3 |
Correct |
14 ms |
23808 KB |
Output is correct |
4 |
Correct |
15 ms |
23808 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
14 ms |
23808 KB |
Output is correct |
7 |
Correct |
14 ms |
23808 KB |
Output is correct |
8 |
Correct |
15 ms |
23808 KB |
Output is correct |
9 |
Correct |
14 ms |
23936 KB |
Output is correct |
10 |
Correct |
14 ms |
23808 KB |
Output is correct |
11 |
Correct |
396 ms |
37752 KB |
Output is correct |
12 |
Correct |
358 ms |
37372 KB |
Output is correct |
13 |
Correct |
356 ms |
37484 KB |
Output is correct |
14 |
Correct |
363 ms |
37496 KB |
Output is correct |
15 |
Correct |
362 ms |
37464 KB |
Output is correct |
16 |
Correct |
359 ms |
36856 KB |
Output is correct |
17 |
Correct |
364 ms |
36856 KB |
Output is correct |
18 |
Correct |
383 ms |
36728 KB |
Output is correct |
19 |
Correct |
368 ms |
37496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
203 ms |
41072 KB |
Output is correct |
2 |
Correct |
163 ms |
40312 KB |
Output is correct |
3 |
Correct |
121 ms |
41080 KB |
Output is correct |
4 |
Correct |
219 ms |
41148 KB |
Output is correct |
5 |
Correct |
208 ms |
41080 KB |
Output is correct |
6 |
Correct |
194 ms |
41080 KB |
Output is correct |
7 |
Correct |
195 ms |
41096 KB |
Output is correct |
8 |
Correct |
194 ms |
41208 KB |
Output is correct |
9 |
Correct |
203 ms |
41080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23808 KB |
Output is correct |
2 |
Correct |
14 ms |
23808 KB |
Output is correct |
3 |
Correct |
14 ms |
23808 KB |
Output is correct |
4 |
Correct |
15 ms |
23808 KB |
Output is correct |
5 |
Correct |
14 ms |
23808 KB |
Output is correct |
6 |
Correct |
14 ms |
23808 KB |
Output is correct |
7 |
Correct |
14 ms |
23808 KB |
Output is correct |
8 |
Correct |
15 ms |
23808 KB |
Output is correct |
9 |
Correct |
14 ms |
23936 KB |
Output is correct |
10 |
Correct |
14 ms |
23808 KB |
Output is correct |
11 |
Correct |
396 ms |
37752 KB |
Output is correct |
12 |
Correct |
358 ms |
37372 KB |
Output is correct |
13 |
Correct |
356 ms |
37484 KB |
Output is correct |
14 |
Correct |
363 ms |
37496 KB |
Output is correct |
15 |
Correct |
362 ms |
37464 KB |
Output is correct |
16 |
Correct |
359 ms |
36856 KB |
Output is correct |
17 |
Correct |
364 ms |
36856 KB |
Output is correct |
18 |
Correct |
383 ms |
36728 KB |
Output is correct |
19 |
Correct |
368 ms |
37496 KB |
Output is correct |
20 |
Correct |
203 ms |
41072 KB |
Output is correct |
21 |
Correct |
163 ms |
40312 KB |
Output is correct |
22 |
Correct |
121 ms |
41080 KB |
Output is correct |
23 |
Correct |
219 ms |
41148 KB |
Output is correct |
24 |
Correct |
208 ms |
41080 KB |
Output is correct |
25 |
Correct |
194 ms |
41080 KB |
Output is correct |
26 |
Correct |
195 ms |
41096 KB |
Output is correct |
27 |
Correct |
194 ms |
41208 KB |
Output is correct |
28 |
Correct |
203 ms |
41080 KB |
Output is correct |
29 |
Correct |
1210 ms |
83916 KB |
Output is correct |
30 |
Correct |
1080 ms |
81912 KB |
Output is correct |
31 |
Correct |
1004 ms |
83836 KB |
Output is correct |
32 |
Correct |
1271 ms |
83868 KB |
Output is correct |
33 |
Correct |
1281 ms |
84100 KB |
Output is correct |
34 |
Correct |
1207 ms |
83448 KB |
Output is correct |
35 |
Correct |
1242 ms |
83380 KB |
Output is correct |
36 |
Correct |
1213 ms |
83172 KB |
Output is correct |
37 |
Correct |
1249 ms |
83832 KB |
Output is correct |
38 |
Correct |
952 ms |
89528 KB |
Output is correct |
39 |
Correct |
947 ms |
89592 KB |
Output is correct |
40 |
Correct |
917 ms |
87928 KB |
Output is correct |
41 |
Correct |
953 ms |
87672 KB |
Output is correct |
42 |
Correct |
996 ms |
87672 KB |
Output is correct |
43 |
Correct |
997 ms |
88444 KB |
Output is correct |
44 |
Correct |
1006 ms |
88976 KB |
Output is correct |
45 |
Correct |
993 ms |
89208 KB |
Output is correct |
46 |
Correct |
965 ms |
87544 KB |
Output is correct |
47 |
Correct |
956 ms |
87400 KB |
Output is correct |
48 |
Correct |
978 ms |
87544 KB |
Output is correct |
49 |
Correct |
1008 ms |
88568 KB |
Output is correct |
50 |
Correct |
1091 ms |
86776 KB |
Output is correct |
51 |
Correct |
1062 ms |
86788 KB |
Output is correct |
52 |
Correct |
1070 ms |
86008 KB |
Output is correct |
53 |
Correct |
1078 ms |
86008 KB |
Output is correct |
54 |
Correct |
1062 ms |
85900 KB |
Output is correct |
55 |
Correct |
1064 ms |
86776 KB |
Output is correct |