#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
const int N = 500001;
struct item{
int mx_f, mx_a, mx;
};
int n, q;
vector <int> a, v[N];
vector <pair <int, int> > qu[N];
item tree[4 * N];
item merge(item a, item b){
item ret = a;
ret.mx_f = max(ret.mx_f, b.mx_f);
ret.mx_a = max(ret.mx_a, b.mx_a);
ret.mx = max(ret.mx, max(b.mx, a.mx_f + b.mx_a));
return ret;
}
void update(int l, int r, int node, int idx, item c){
if(r < l || r < idx || l > idx) return;
if(l == r){
tree[node].mx_f = max(tree[node].mx_f, c.mx_f);
tree[node].mx_a = max(tree[node].mx_a, c.mx_a);
tree[node].mx = tree[node].mx_f + tree[node].mx_a;
return;
}
int mid = (l + r) / 2;
update(l, mid, 2 * node, idx, c);
update(mid + 1, r, 2 * node + 1, idx, c);
tree[node] = merge(tree[2 * node], tree[2 * node + 1]);
}
item query(int l, int r, int node, int s, int e){
if(r < l || r < s || l > e) return {0, 0, 0};
if(s <= l && r <= e) return tree[node];
int mid = (l + r) / 2;
return merge(query(l, mid, 2 * node, s, e), query(mid + 1, r, 2 * node + 1, s, e));
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
a.resize(n);
for(auto &i : a) cin >> i;
vector <int> s;
for(int i = 0 ; i < n ; i++){
while(s.size() && a[i] >= a[s.back()]){
v[s.back()].push_back(i);
s.pop_back();
}
if(s.size()){
v[s.back()].push_back(i);
}
s.push_back(i);
}
cin >> q;
for(int i = 0 ; i < q ; i++){
int l, r;
cin >> l >> r;
l--; r--;
qu[l].push_back(make_pair(r, i));
}
for(int i = 0 ; i < n ; i++){
update(0, n - 1, 1, i, {0, a[i], 0});
}
vector <int> ans(q);
for(int l = n - 1 ; l >= 0 ; l--){
for(auto &r : v[l]){
update(0, n - 1, 1, 2 * r - l, {a[l] + a[r], 0, 0});
}
for(auto &i : qu[l]){
ans[i.second] = query(0, n - 1, 1, l, i.first).mx;
}
}
for(auto &i : ans) cout << i << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
23756 KB |
Output is correct |
3 |
Correct |
15 ms |
23780 KB |
Output is correct |
4 |
Correct |
15 ms |
23884 KB |
Output is correct |
5 |
Correct |
16 ms |
23972 KB |
Output is correct |
6 |
Correct |
15 ms |
23756 KB |
Output is correct |
7 |
Correct |
15 ms |
23760 KB |
Output is correct |
8 |
Correct |
15 ms |
23716 KB |
Output is correct |
9 |
Correct |
16 ms |
23756 KB |
Output is correct |
10 |
Correct |
15 ms |
23712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
23756 KB |
Output is correct |
3 |
Correct |
15 ms |
23780 KB |
Output is correct |
4 |
Correct |
15 ms |
23884 KB |
Output is correct |
5 |
Correct |
16 ms |
23972 KB |
Output is correct |
6 |
Correct |
15 ms |
23756 KB |
Output is correct |
7 |
Correct |
15 ms |
23760 KB |
Output is correct |
8 |
Correct |
15 ms |
23716 KB |
Output is correct |
9 |
Correct |
16 ms |
23756 KB |
Output is correct |
10 |
Correct |
15 ms |
23712 KB |
Output is correct |
11 |
Correct |
403 ms |
42792 KB |
Output is correct |
12 |
Correct |
403 ms |
42800 KB |
Output is correct |
13 |
Correct |
409 ms |
42904 KB |
Output is correct |
14 |
Correct |
400 ms |
42784 KB |
Output is correct |
15 |
Correct |
403 ms |
42824 KB |
Output is correct |
16 |
Correct |
405 ms |
42188 KB |
Output is correct |
17 |
Correct |
409 ms |
42052 KB |
Output is correct |
18 |
Correct |
401 ms |
42140 KB |
Output is correct |
19 |
Correct |
406 ms |
42616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
330 ms |
38852 KB |
Output is correct |
2 |
Correct |
222 ms |
38792 KB |
Output is correct |
3 |
Correct |
227 ms |
39504 KB |
Output is correct |
4 |
Correct |
316 ms |
38924 KB |
Output is correct |
5 |
Correct |
319 ms |
38896 KB |
Output is correct |
6 |
Correct |
316 ms |
38264 KB |
Output is correct |
7 |
Correct |
322 ms |
38212 KB |
Output is correct |
8 |
Correct |
316 ms |
38076 KB |
Output is correct |
9 |
Correct |
330 ms |
38596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
23756 KB |
Output is correct |
2 |
Correct |
15 ms |
23756 KB |
Output is correct |
3 |
Correct |
15 ms |
23780 KB |
Output is correct |
4 |
Correct |
15 ms |
23884 KB |
Output is correct |
5 |
Correct |
16 ms |
23972 KB |
Output is correct |
6 |
Correct |
15 ms |
23756 KB |
Output is correct |
7 |
Correct |
15 ms |
23760 KB |
Output is correct |
8 |
Correct |
15 ms |
23716 KB |
Output is correct |
9 |
Correct |
16 ms |
23756 KB |
Output is correct |
10 |
Correct |
15 ms |
23712 KB |
Output is correct |
11 |
Correct |
403 ms |
42792 KB |
Output is correct |
12 |
Correct |
403 ms |
42800 KB |
Output is correct |
13 |
Correct |
409 ms |
42904 KB |
Output is correct |
14 |
Correct |
400 ms |
42784 KB |
Output is correct |
15 |
Correct |
403 ms |
42824 KB |
Output is correct |
16 |
Correct |
405 ms |
42188 KB |
Output is correct |
17 |
Correct |
409 ms |
42052 KB |
Output is correct |
18 |
Correct |
401 ms |
42140 KB |
Output is correct |
19 |
Correct |
406 ms |
42616 KB |
Output is correct |
20 |
Correct |
330 ms |
38852 KB |
Output is correct |
21 |
Correct |
222 ms |
38792 KB |
Output is correct |
22 |
Correct |
227 ms |
39504 KB |
Output is correct |
23 |
Correct |
316 ms |
38924 KB |
Output is correct |
24 |
Correct |
319 ms |
38896 KB |
Output is correct |
25 |
Correct |
316 ms |
38264 KB |
Output is correct |
26 |
Correct |
322 ms |
38212 KB |
Output is correct |
27 |
Correct |
316 ms |
38076 KB |
Output is correct |
28 |
Correct |
330 ms |
38596 KB |
Output is correct |
29 |
Correct |
1598 ms |
82112 KB |
Output is correct |
30 |
Correct |
1309 ms |
81504 KB |
Output is correct |
31 |
Correct |
1375 ms |
83420 KB |
Output is correct |
32 |
Correct |
1614 ms |
82120 KB |
Output is correct |
33 |
Correct |
1609 ms |
82264 KB |
Output is correct |
34 |
Correct |
1568 ms |
79752 KB |
Output is correct |
35 |
Correct |
1572 ms |
79492 KB |
Output is correct |
36 |
Correct |
1569 ms |
79520 KB |
Output is correct |
37 |
Correct |
1563 ms |
81052 KB |
Output is correct |
38 |
Correct |
1292 ms |
87824 KB |
Output is correct |
39 |
Correct |
1287 ms |
87820 KB |
Output is correct |
40 |
Correct |
1279 ms |
84548 KB |
Output is correct |
41 |
Correct |
1276 ms |
84004 KB |
Output is correct |
42 |
Correct |
1296 ms |
84120 KB |
Output is correct |
43 |
Correct |
1301 ms |
85828 KB |
Output is correct |
44 |
Correct |
1359 ms |
87188 KB |
Output is correct |
45 |
Correct |
1363 ms |
87220 KB |
Output is correct |
46 |
Correct |
1327 ms |
84168 KB |
Output is correct |
47 |
Correct |
1331 ms |
83640 KB |
Output is correct |
48 |
Correct |
1335 ms |
83564 KB |
Output is correct |
49 |
Correct |
1328 ms |
85680 KB |
Output is correct |
50 |
Correct |
1404 ms |
85404 KB |
Output is correct |
51 |
Correct |
1402 ms |
85316 KB |
Output is correct |
52 |
Correct |
1395 ms |
83048 KB |
Output is correct |
53 |
Correct |
1406 ms |
82580 KB |
Output is correct |
54 |
Correct |
1397 ms |
82624 KB |
Output is correct |
55 |
Correct |
1409 ms |
84260 KB |
Output is correct |