#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 5e5 + 7, INF = 1e18 + 7;
int n, q, a[N], ans[N];
vector <ii> add[N], d[N];
int tree[N << 2], prop[N << 2], mx[N << 2];
void relax(int v) {
tree[v] = max(tree[v * 2 + 1], tree[v * 2 + 2]);
mx[v] = max(mx[v * 2 + 1], mx[v * 2 + 2]);
}
void gap(int v, int x) {
prop[v] = max(prop[v], x);
tree[v] = max(tree[v], mx[v] + x);
}
void push(int v) {
if (prop[v]) {
gap(v * 2 + 1, prop[v]);
gap(v * 2 + 2, prop[v]);
prop[v] = 0;
}
}
int get(int v, int tl, int tr, int l, int r) {
if (tr < l || r < tl)
return 0;
if (l <= tl && tr <= r) {
#ifdef HOME
cout << "get " << tl << ' ' << tr << ' ' << tree[v] << '\n';
#endif
return tree[v];
}
int tm = (tl + tr) >> 1;
push(v);
return max(get(v * 2 + 1, tl, tm, l, r), get(v * 2 + 2, tm + 1, tr, l, r));
}
void upd(int v, int tl, int tr, int i, int x) {
if (tl == tr) {
tree[v] = max(tree[v], x);
mx[v] = max(mx[v], x);
return;
}
int tm = (tl + tr) >> 1;
push(v);
if (i <= tm)
upd(v * 2 + 1, tl, tm, i, x);
else
upd(v * 2 + 2, tm + 1, tr, i, x);
relax(v);
}
void print(int v, int tl, int tr) {
cout << tl << ' ' << tr << " : " << tree[v] << ' ' << prop[v] << ' ' << mx[v] << '\n';
if (tl == tr)
return;
int tm = (tl + tr) >> 1;
push(v);
print(v * 2 + 1, tl, tm);
print(v * 2 + 2, tm + 1, tr);
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
for (int i = 0; i < (N << 2); ++i)
tree[i] = mx[i] = -INF;
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i];
vector <ii> p;
vector <int> s;
for (int i = 0; i < n; ++i) {
while (s.size() && a[s.back()] <= a[i]) {
p.app(mp(s.back(), i));
s.pop_back();
}
s.app(i);
}
s.clear();
for (int i = n - 1; i >= 0; --i) {
while (s.size() && a[s.back()] <= a[i]) {
p.app(mp(i, s.back()));
s.pop_back();
}
s.app(i);
}
for (auto e : p) {
int l = e.s + (e.s - e.f);
if (l < n)
add[l].app(mp(e.f, a[e.f] + a[e.s]));
}
cin >> q;
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
--l; --r;
d[r].app(mp(l, i));
}
for (int i = 0; i < n; ++i) {
for (auto e : add[i]) {
upd(0, 0, n - 1, e.f, e.s);
//cout << "upd " << e.f << ' ' << e.s << '\n';
}
gap(0, a[i]);
//cout << "gap " << a[i] << '\n';
for (auto e : d[i])
ans[e.s] = get(0, 0, n - 1, e.f, n - 1);
}
for (int i = 0; i < q; ++i)
cout << ans[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
55288 KB |
Output is correct |
2 |
Correct |
34 ms |
55160 KB |
Output is correct |
3 |
Correct |
32 ms |
55160 KB |
Output is correct |
4 |
Correct |
34 ms |
55160 KB |
Output is correct |
5 |
Correct |
36 ms |
55160 KB |
Output is correct |
6 |
Correct |
36 ms |
55160 KB |
Output is correct |
7 |
Correct |
35 ms |
55160 KB |
Output is correct |
8 |
Correct |
35 ms |
55160 KB |
Output is correct |
9 |
Correct |
35 ms |
55160 KB |
Output is correct |
10 |
Correct |
33 ms |
55160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
55288 KB |
Output is correct |
2 |
Correct |
34 ms |
55160 KB |
Output is correct |
3 |
Correct |
32 ms |
55160 KB |
Output is correct |
4 |
Correct |
34 ms |
55160 KB |
Output is correct |
5 |
Correct |
36 ms |
55160 KB |
Output is correct |
6 |
Correct |
36 ms |
55160 KB |
Output is correct |
7 |
Correct |
35 ms |
55160 KB |
Output is correct |
8 |
Correct |
35 ms |
55160 KB |
Output is correct |
9 |
Correct |
35 ms |
55160 KB |
Output is correct |
10 |
Correct |
33 ms |
55160 KB |
Output is correct |
11 |
Correct |
317 ms |
77304 KB |
Output is correct |
12 |
Correct |
311 ms |
76920 KB |
Output is correct |
13 |
Correct |
335 ms |
77048 KB |
Output is correct |
14 |
Correct |
322 ms |
77304 KB |
Output is correct |
15 |
Correct |
333 ms |
77452 KB |
Output is correct |
16 |
Correct |
323 ms |
76664 KB |
Output is correct |
17 |
Correct |
329 ms |
76664 KB |
Output is correct |
18 |
Correct |
325 ms |
76680 KB |
Output is correct |
19 |
Correct |
303 ms |
77176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
201 ms |
77956 KB |
Output is correct |
2 |
Correct |
126 ms |
71772 KB |
Output is correct |
3 |
Correct |
122 ms |
71776 KB |
Output is correct |
4 |
Correct |
195 ms |
77908 KB |
Output is correct |
5 |
Correct |
198 ms |
78020 KB |
Output is correct |
6 |
Correct |
191 ms |
78036 KB |
Output is correct |
7 |
Correct |
180 ms |
78032 KB |
Output is correct |
8 |
Correct |
191 ms |
77972 KB |
Output is correct |
9 |
Correct |
191 ms |
78036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
55288 KB |
Output is correct |
2 |
Correct |
34 ms |
55160 KB |
Output is correct |
3 |
Correct |
32 ms |
55160 KB |
Output is correct |
4 |
Correct |
34 ms |
55160 KB |
Output is correct |
5 |
Correct |
36 ms |
55160 KB |
Output is correct |
6 |
Correct |
36 ms |
55160 KB |
Output is correct |
7 |
Correct |
35 ms |
55160 KB |
Output is correct |
8 |
Correct |
35 ms |
55160 KB |
Output is correct |
9 |
Correct |
35 ms |
55160 KB |
Output is correct |
10 |
Correct |
33 ms |
55160 KB |
Output is correct |
11 |
Correct |
317 ms |
77304 KB |
Output is correct |
12 |
Correct |
311 ms |
76920 KB |
Output is correct |
13 |
Correct |
335 ms |
77048 KB |
Output is correct |
14 |
Correct |
322 ms |
77304 KB |
Output is correct |
15 |
Correct |
333 ms |
77452 KB |
Output is correct |
16 |
Correct |
323 ms |
76664 KB |
Output is correct |
17 |
Correct |
329 ms |
76664 KB |
Output is correct |
18 |
Correct |
325 ms |
76680 KB |
Output is correct |
19 |
Correct |
303 ms |
77176 KB |
Output is correct |
20 |
Correct |
201 ms |
77956 KB |
Output is correct |
21 |
Correct |
126 ms |
71772 KB |
Output is correct |
22 |
Correct |
122 ms |
71776 KB |
Output is correct |
23 |
Correct |
195 ms |
77908 KB |
Output is correct |
24 |
Correct |
198 ms |
78020 KB |
Output is correct |
25 |
Correct |
191 ms |
78036 KB |
Output is correct |
26 |
Correct |
180 ms |
78032 KB |
Output is correct |
27 |
Correct |
191 ms |
77972 KB |
Output is correct |
28 |
Correct |
191 ms |
78036 KB |
Output is correct |
29 |
Correct |
1200 ms |
130672 KB |
Output is correct |
30 |
Correct |
1050 ms |
116784 KB |
Output is correct |
31 |
Correct |
1004 ms |
116824 KB |
Output is correct |
32 |
Correct |
1166 ms |
130628 KB |
Output is correct |
33 |
Correct |
1215 ms |
130624 KB |
Output is correct |
34 |
Correct |
1199 ms |
129856 KB |
Output is correct |
35 |
Correct |
1219 ms |
129984 KB |
Output is correct |
36 |
Correct |
1185 ms |
129984 KB |
Output is correct |
37 |
Correct |
1194 ms |
130496 KB |
Output is correct |
38 |
Correct |
783 ms |
133276 KB |
Output is correct |
39 |
Correct |
769 ms |
133316 KB |
Output is correct |
40 |
Correct |
751 ms |
131556 KB |
Output is correct |
41 |
Correct |
716 ms |
131136 KB |
Output is correct |
42 |
Correct |
762 ms |
131132 KB |
Output is correct |
43 |
Correct |
765 ms |
132160 KB |
Output is correct |
44 |
Correct |
781 ms |
133388 KB |
Output is correct |
45 |
Correct |
769 ms |
133440 KB |
Output is correct |
46 |
Correct |
779 ms |
132036 KB |
Output is correct |
47 |
Correct |
763 ms |
131724 KB |
Output is correct |
48 |
Correct |
756 ms |
131884 KB |
Output is correct |
49 |
Correct |
805 ms |
133052 KB |
Output is correct |
50 |
Correct |
929 ms |
133700 KB |
Output is correct |
51 |
Correct |
912 ms |
133700 KB |
Output is correct |
52 |
Correct |
921 ms |
132712 KB |
Output is correct |
53 |
Correct |
950 ms |
132828 KB |
Output is correct |
54 |
Correct |
907 ms |
132644 KB |
Output is correct |
55 |
Correct |
910 ms |
133696 KB |
Output is correct |