#include <bits/stdc++.h>
using namespace std;
const int N = (int) 5e5 + 7;
const int INF = (int) 2e9;
int n, a[N], nextBigger[N], q;
struct rmq {
const int K = 20;
vector<vector<int>> tab;
vector<int> lg2;
int n;
void init(int nn) {
n = nn;
lg2.resize(n + 1);
for (int i = 2; i <= n; i++) {
lg2[i] = 1 + lg2[i / 2];
}
tab.resize(K);
for (int i = 0; i < K; i++) {
tab[i].resize(n + 1);
}
}
void build(int a[]) {
for (int i = 1; i <= n; i++) {
tab[0][i] = a[i];
}
for (int k = 1; k < K; k++) {
for (int i = 1; i + (1 << k) - 1 <= n; i++) {
tab[k][i] = max(tab[k - 1][i], tab[k - 1][i + (1 << (k - 1))]);
}
}
}
int query(int x, int y) {
if (x > y) {
return -INF;
}
assert(1 <= x && x <= y && y <= n);
int p = lg2[y - x + 1];
return max(tab[p][x], tab[p][y - (1 << p) + 1]);
}
};
struct Offer {
int value;
int lft;
int rgh;
};
struct Question {
int l;
int r;
int ind;
};
bool operator < (Offer a, Offer b) {
return a.lft > b.lft;
}
vector<Question> questionsAt[N];
int best[N];
struct node {
int maxA;
int maxAB;
};
node operator + (node a, node b) {
return {max(a.maxA, b.maxA), max(a.maxAB, b.maxAB)};
}
node t[4 * N];
int applyToAll[4 * N];
void push(int v, int tl, int tr) {
if (applyToAll[v] == -INF) {
return;
}
t[v].maxAB = max(t[v].maxAB, t[v].maxA + applyToAll[v]);
if (tl < tr) {
applyToAll[2 * v] = max(applyToAll[2 * v], applyToAll[v]);
applyToAll[2 * v + 1] = max(applyToAll[2 * v + 1], applyToAll[v]);
}
applyToAll[v] = -INF;
}
void build(int v, int tl, int tr) {
if (tl == tr) {
t[v].maxA = a[tl];
} else {
int tm = (tl + tr) / 2;
build(2 * v, tl, tm);
build(2 * v + 1, tm + 1, tr);
t[v] = t[2 * v] + t[2 * v + 1];
}
}
void apply(int v, int tl, int tr, int l, int r, int x) {
push(v, tl, tr);
if (tr < l || r < tl) {
return;
}
if (l <= tl && tr <= r) {
applyToAll[v] = x;
push(v, tl, tr);
return;
}
int tm = (tl + tr) / 2;
apply(2 * v, tl, tm, l, r, x);
apply(2 * v + 1, tm + 1, tr, l, r, x);
t[v] = t[2 * v] + t[2 * v + 1];
}
node get(int v, int tl, int tr, int l, int r) {
push(v, tl, tr);
if (tr < l || r < tl) {
return {-INF, -INF};
}
if (l <= tl && tr <= r) {
return t[v];
}
int tm = (tl + tr) / 2;
return get(2 * v, tl, tm, l, r) + get(2 * v + 1, tm + 1, tr, l, r);
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
//freopen("input", "r", stdin);
for (int i = 0; i < 4 * N; i++) {
applyToAll[i] = -INF;
t[i].maxA = -INF;
t[i].maxAB = -INF;
}
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
build(1, 1, n);
rmq rmq;
rmq.init(n);
rmq.build(a);
{
vector<int> st;
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[i] >= a[st.back()]) {
st.pop_back();
}
if (st.empty()) {
nextBigger[i] = n + 1;
} else {
nextBigger[i] = st.back();
}
st.push_back(i);
}
}
vector<Offer> offers;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= min(n, nextBigger[i]); j = nextBigger[j]) {
if (2 * j - i <= n) {
offers.push_back({a[i] + a[j], i, 2 * j - i});
}
}
}
sort(offers.begin(), offers.end());
cin >> q;
vector<int> sol(q, -1);
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
questionsAt[l].push_back({l, r, i});
}
for (int i = 1; i <= n; i++) {
best[i] = -INF;
}
int last = -1;
for (int l = n; l >= 1; l--) {
while (last + 1 < (int) offers.size() && l <= offers[last + 1].lft) {
last++;
apply(1, 1, n, offers[last].rgh, n, offers[last].value);
}
for (auto &it : questionsAt[l]) {
sol[it.ind] = get(1, 1, n, l, it.r).maxAB;
}
}
for (auto &x : sol) {
assert(x != -1);
cout << x << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
35540 KB |
Output is correct |
2 |
Correct |
20 ms |
35540 KB |
Output is correct |
3 |
Correct |
19 ms |
35560 KB |
Output is correct |
4 |
Correct |
19 ms |
35516 KB |
Output is correct |
5 |
Correct |
18 ms |
35560 KB |
Output is correct |
6 |
Correct |
19 ms |
35460 KB |
Output is correct |
7 |
Correct |
19 ms |
35576 KB |
Output is correct |
8 |
Correct |
18 ms |
35572 KB |
Output is correct |
9 |
Correct |
18 ms |
35540 KB |
Output is correct |
10 |
Correct |
19 ms |
35564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
35540 KB |
Output is correct |
2 |
Correct |
20 ms |
35540 KB |
Output is correct |
3 |
Correct |
19 ms |
35560 KB |
Output is correct |
4 |
Correct |
19 ms |
35516 KB |
Output is correct |
5 |
Correct |
18 ms |
35560 KB |
Output is correct |
6 |
Correct |
19 ms |
35460 KB |
Output is correct |
7 |
Correct |
19 ms |
35576 KB |
Output is correct |
8 |
Correct |
18 ms |
35572 KB |
Output is correct |
9 |
Correct |
18 ms |
35540 KB |
Output is correct |
10 |
Correct |
19 ms |
35564 KB |
Output is correct |
11 |
Correct |
293 ms |
54680 KB |
Output is correct |
12 |
Correct |
302 ms |
54604 KB |
Output is correct |
13 |
Correct |
253 ms |
54628 KB |
Output is correct |
14 |
Correct |
248 ms |
54676 KB |
Output is correct |
15 |
Correct |
258 ms |
54804 KB |
Output is correct |
16 |
Correct |
257 ms |
54020 KB |
Output is correct |
17 |
Correct |
268 ms |
54204 KB |
Output is correct |
18 |
Correct |
256 ms |
54012 KB |
Output is correct |
19 |
Correct |
257 ms |
54620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
148 ms |
59836 KB |
Output is correct |
2 |
Correct |
89 ms |
58304 KB |
Output is correct |
3 |
Correct |
98 ms |
56828 KB |
Output is correct |
4 |
Correct |
145 ms |
59840 KB |
Output is correct |
5 |
Correct |
151 ms |
59852 KB |
Output is correct |
6 |
Correct |
165 ms |
59840 KB |
Output is correct |
7 |
Correct |
156 ms |
59840 KB |
Output is correct |
8 |
Correct |
142 ms |
59860 KB |
Output is correct |
9 |
Correct |
146 ms |
59800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
35540 KB |
Output is correct |
2 |
Correct |
20 ms |
35540 KB |
Output is correct |
3 |
Correct |
19 ms |
35560 KB |
Output is correct |
4 |
Correct |
19 ms |
35516 KB |
Output is correct |
5 |
Correct |
18 ms |
35560 KB |
Output is correct |
6 |
Correct |
19 ms |
35460 KB |
Output is correct |
7 |
Correct |
19 ms |
35576 KB |
Output is correct |
8 |
Correct |
18 ms |
35572 KB |
Output is correct |
9 |
Correct |
18 ms |
35540 KB |
Output is correct |
10 |
Correct |
19 ms |
35564 KB |
Output is correct |
11 |
Correct |
293 ms |
54680 KB |
Output is correct |
12 |
Correct |
302 ms |
54604 KB |
Output is correct |
13 |
Correct |
253 ms |
54628 KB |
Output is correct |
14 |
Correct |
248 ms |
54676 KB |
Output is correct |
15 |
Correct |
258 ms |
54804 KB |
Output is correct |
16 |
Correct |
257 ms |
54020 KB |
Output is correct |
17 |
Correct |
268 ms |
54204 KB |
Output is correct |
18 |
Correct |
256 ms |
54012 KB |
Output is correct |
19 |
Correct |
257 ms |
54620 KB |
Output is correct |
20 |
Correct |
148 ms |
59836 KB |
Output is correct |
21 |
Correct |
89 ms |
58304 KB |
Output is correct |
22 |
Correct |
98 ms |
56828 KB |
Output is correct |
23 |
Correct |
145 ms |
59840 KB |
Output is correct |
24 |
Correct |
151 ms |
59852 KB |
Output is correct |
25 |
Correct |
165 ms |
59840 KB |
Output is correct |
26 |
Correct |
156 ms |
59840 KB |
Output is correct |
27 |
Correct |
142 ms |
59860 KB |
Output is correct |
28 |
Correct |
146 ms |
59800 KB |
Output is correct |
29 |
Correct |
868 ms |
120016 KB |
Output is correct |
30 |
Correct |
747 ms |
117132 KB |
Output is correct |
31 |
Correct |
706 ms |
117096 KB |
Output is correct |
32 |
Correct |
868 ms |
123136 KB |
Output is correct |
33 |
Correct |
833 ms |
123028 KB |
Output is correct |
34 |
Correct |
859 ms |
120836 KB |
Output is correct |
35 |
Correct |
867 ms |
120468 KB |
Output is correct |
36 |
Correct |
846 ms |
120492 KB |
Output is correct |
37 |
Correct |
882 ms |
121920 KB |
Output is correct |
38 |
Correct |
649 ms |
127944 KB |
Output is correct |
39 |
Correct |
642 ms |
127828 KB |
Output is correct |
40 |
Correct |
642 ms |
124528 KB |
Output is correct |
41 |
Correct |
626 ms |
124040 KB |
Output is correct |
42 |
Correct |
648 ms |
124020 KB |
Output is correct |
43 |
Correct |
629 ms |
125764 KB |
Output is correct |
44 |
Correct |
669 ms |
127240 KB |
Output is correct |
45 |
Correct |
677 ms |
127256 KB |
Output is correct |
46 |
Correct |
660 ms |
124100 KB |
Output is correct |
47 |
Correct |
659 ms |
123692 KB |
Output is correct |
48 |
Correct |
656 ms |
123676 KB |
Output is correct |
49 |
Correct |
673 ms |
125728 KB |
Output is correct |
50 |
Correct |
750 ms |
125520 KB |
Output is correct |
51 |
Correct |
742 ms |
125536 KB |
Output is correct |
52 |
Correct |
754 ms |
122988 KB |
Output is correct |
53 |
Correct |
710 ms |
122744 KB |
Output is correct |
54 |
Correct |
731 ms |
122732 KB |
Output is correct |
55 |
Correct |
743 ms |
124404 KB |
Output is correct |