#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
using ll = long long;
using ld = long double;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int)x.size()
#define endl '\n'
const int mod = 1e9 + 7;
const int inf = 2e9 + 5;
const ll linf = 9e18 + 5;
int n, q;
const int N = 5e5 + 5;
const int L = (1 << 20);
int a[N];
vector<int> bigs[N];
vector<pair<int, int>> quer[N];
int seg[L][3];
void cre(int l, int r, int ind) {
if (l == r) {
seg[ind][0] = -inf;
seg[ind][1] = -inf;
seg[ind][2] = a[l];
return;
}
int m = (l + r) / 2;
cre(l, m, 2 * ind);
cre(m + 1, r, 2 * ind + 1);
seg[ind][0] = -inf;
seg[ind][1] = -inf;
seg[ind][2] = max(seg[2 * ind][2], seg[2 * ind + 1][2]);
}
void upd(int b, int x, int l, int r, int ind) {
if (b <= l) {
seg[ind][1] = max(seg[ind][1], x);
seg[ind][0] = max(seg[ind][0], seg[ind][1] + seg[ind][2]);
return;
}
if (r < b) {
return;
}
int m = (l + r) / 2;
upd(b, x, l, m, 2 * ind);
upd(b, x, m + 1, r, 2 * ind + 1);
seg[ind][0] = max(seg[ind][0], max(seg[2 * ind][0], seg[2 * ind + 1][0]));
}
pair<int, int> get(int b, int e, int l, int r, int ind) {
if (b <= l && r <= e) {
return mp(seg[ind][0], seg[ind][2]);
}
if (e < l || r < b) {
return mp(-inf, 0);
}
int m = (l + r) / 2;
pair<int, int> p1 = get(b, e, l, m, 2 * ind);
pair<int, int> p2 = get(b, e, m + 1, r, 2 * ind + 1);
return mp(max(seg[ind][1] + max(p1.se, p2.se), max(p1.fi, p2.fi)), max(p1.se, p2.se));
}
void init() {
}
void input() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
cin >> q;
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
quer[l].pb(mp(r, i));
}
}
void solve() {
vector<int> big;
for (int i = n; i >= 1; i--) {
while (!big.empty()) {
int x = big.back();
if (a[i] <= a[x]) {
break;
}
bigs[i].pb(x);
big.pop_back();
}
if (!big.empty()) {
bigs[i].pb(big.back());
}
big.pb(i);
}
cre(1, n, 1);
int ans[q + 1];
for (int l = n; l >= 1; l--) {
for (auto i : bigs[l]) {
upd(2 * i - l, a[l] + a[i], 1, n, 1);
}
for (auto p : quer[l]) {
ans[p.se] = get(l, p.fi, 1, n, 1).fi;
}
}
for (int i = 1; i <= q; i++) {
cout << ans[i] << endl;
}
}
void output() {
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int number_of_testcases = 1;
//cin >> number_of_testcases;
while (number_of_testcases--) {
init();
input();
solve();
output();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23772 KB |
Output is correct |
3 |
Correct |
15 ms |
23764 KB |
Output is correct |
4 |
Correct |
16 ms |
23764 KB |
Output is correct |
5 |
Correct |
16 ms |
23768 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23824 KB |
Output is correct |
8 |
Correct |
17 ms |
23756 KB |
Output is correct |
9 |
Correct |
14 ms |
23764 KB |
Output is correct |
10 |
Correct |
14 ms |
23816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23772 KB |
Output is correct |
3 |
Correct |
15 ms |
23764 KB |
Output is correct |
4 |
Correct |
16 ms |
23764 KB |
Output is correct |
5 |
Correct |
16 ms |
23768 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23824 KB |
Output is correct |
8 |
Correct |
17 ms |
23756 KB |
Output is correct |
9 |
Correct |
14 ms |
23764 KB |
Output is correct |
10 |
Correct |
14 ms |
23816 KB |
Output is correct |
11 |
Correct |
294 ms |
42552 KB |
Output is correct |
12 |
Correct |
292 ms |
42644 KB |
Output is correct |
13 |
Correct |
294 ms |
42600 KB |
Output is correct |
14 |
Correct |
245 ms |
42700 KB |
Output is correct |
15 |
Correct |
280 ms |
42540 KB |
Output is correct |
16 |
Correct |
264 ms |
41960 KB |
Output is correct |
17 |
Correct |
298 ms |
41860 KB |
Output is correct |
18 |
Correct |
274 ms |
41832 KB |
Output is correct |
19 |
Correct |
273 ms |
42452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
149 ms |
39052 KB |
Output is correct |
2 |
Correct |
88 ms |
39544 KB |
Output is correct |
3 |
Correct |
89 ms |
38732 KB |
Output is correct |
4 |
Correct |
148 ms |
38924 KB |
Output is correct |
5 |
Correct |
138 ms |
38964 KB |
Output is correct |
6 |
Correct |
140 ms |
38312 KB |
Output is correct |
7 |
Correct |
133 ms |
38132 KB |
Output is correct |
8 |
Correct |
136 ms |
38160 KB |
Output is correct |
9 |
Correct |
135 ms |
38476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23764 KB |
Output is correct |
2 |
Correct |
14 ms |
23772 KB |
Output is correct |
3 |
Correct |
15 ms |
23764 KB |
Output is correct |
4 |
Correct |
16 ms |
23764 KB |
Output is correct |
5 |
Correct |
16 ms |
23768 KB |
Output is correct |
6 |
Correct |
13 ms |
23764 KB |
Output is correct |
7 |
Correct |
13 ms |
23824 KB |
Output is correct |
8 |
Correct |
17 ms |
23756 KB |
Output is correct |
9 |
Correct |
14 ms |
23764 KB |
Output is correct |
10 |
Correct |
14 ms |
23816 KB |
Output is correct |
11 |
Correct |
294 ms |
42552 KB |
Output is correct |
12 |
Correct |
292 ms |
42644 KB |
Output is correct |
13 |
Correct |
294 ms |
42600 KB |
Output is correct |
14 |
Correct |
245 ms |
42700 KB |
Output is correct |
15 |
Correct |
280 ms |
42540 KB |
Output is correct |
16 |
Correct |
264 ms |
41960 KB |
Output is correct |
17 |
Correct |
298 ms |
41860 KB |
Output is correct |
18 |
Correct |
274 ms |
41832 KB |
Output is correct |
19 |
Correct |
273 ms |
42452 KB |
Output is correct |
20 |
Correct |
149 ms |
39052 KB |
Output is correct |
21 |
Correct |
88 ms |
39544 KB |
Output is correct |
22 |
Correct |
89 ms |
38732 KB |
Output is correct |
23 |
Correct |
148 ms |
38924 KB |
Output is correct |
24 |
Correct |
138 ms |
38964 KB |
Output is correct |
25 |
Correct |
140 ms |
38312 KB |
Output is correct |
26 |
Correct |
133 ms |
38132 KB |
Output is correct |
27 |
Correct |
136 ms |
38160 KB |
Output is correct |
28 |
Correct |
135 ms |
38476 KB |
Output is correct |
29 |
Correct |
916 ms |
82112 KB |
Output is correct |
30 |
Correct |
791 ms |
83444 KB |
Output is correct |
31 |
Correct |
939 ms |
81500 KB |
Output is correct |
32 |
Correct |
868 ms |
82104 KB |
Output is correct |
33 |
Correct |
1025 ms |
82136 KB |
Output is correct |
34 |
Correct |
899 ms |
79812 KB |
Output is correct |
35 |
Correct |
883 ms |
79460 KB |
Output is correct |
36 |
Correct |
848 ms |
79452 KB |
Output is correct |
37 |
Correct |
873 ms |
80952 KB |
Output is correct |
38 |
Correct |
646 ms |
87728 KB |
Output is correct |
39 |
Correct |
688 ms |
87820 KB |
Output is correct |
40 |
Correct |
645 ms |
84472 KB |
Output is correct |
41 |
Correct |
806 ms |
84068 KB |
Output is correct |
42 |
Correct |
657 ms |
83976 KB |
Output is correct |
43 |
Correct |
633 ms |
85708 KB |
Output is correct |
44 |
Correct |
686 ms |
87136 KB |
Output is correct |
45 |
Correct |
742 ms |
87176 KB |
Output is correct |
46 |
Correct |
670 ms |
83968 KB |
Output is correct |
47 |
Correct |
651 ms |
83596 KB |
Output is correct |
48 |
Correct |
649 ms |
83520 KB |
Output is correct |
49 |
Correct |
728 ms |
85692 KB |
Output is correct |
50 |
Correct |
726 ms |
85112 KB |
Output is correct |
51 |
Correct |
835 ms |
85064 KB |
Output is correct |
52 |
Correct |
711 ms |
82552 KB |
Output is correct |
53 |
Correct |
747 ms |
82296 KB |
Output is correct |
54 |
Correct |
725 ms |
82296 KB |
Output is correct |
55 |
Correct |
761 ms |
83888 KB |
Output is correct |