#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
struct Query {
int l, r, idx; };
struct Nod {
int maxval, maxans, enforce; };
int v[N], ant[N];
vector<int> nxt[N];
Nod pom[N * 4];
vector<Query> qs;
int n, q, ql, qr, qval;
static void get_nxt() {
vector<int> stk;
for (int i = 1; i <= n; ++i) {
while (!stk.empty() && v[stk.back()] <= v[i]) {
nxt[stk.back()].push_back(i);
stk.pop_back(); }
if (!stk.empty())
nxt[stk.back()].push_back(i);
stk.push_back(i); }
for (int i = 0; i + 1 < stk.size(); ++i) {
for (int j = stk[i] + 1; j <= stk[i + 1]; ++j)
nxt[stk[i]].push_back(j); } }
static void prop(int nod) {
if (pom[nod].enforce == 0)
return;
else {
pom[2 * nod + 1].enforce = max(pom[2 * nod + 1].enforce, pom[nod].enforce);
pom[2 * nod].enforce = max(pom[2 * nod].enforce, pom[nod].enforce);
pom[nod].maxans = max(pom[nod].maxans, pom[nod].maxval + pom[nod].enforce);
pom[nod].enforce = 0; } }
static int eval(Nod nod) {
return max(nod.maxans, nod.maxval + nod.enforce); }
static Nod join(Nod a, Nod b) {
return (Nod {max(a.maxval, b.maxval), max(eval(a), eval(b)), 0}); }
static void build(int nod, int st, int dr) {
if (st == dr) {
pom[nod] = {v[st], v[dr], 0}; // OCD right here
return; }
int mid = (st + dr) / 2;
build(2 * nod, st, mid);
build(2 * nod + 1, mid + 1, dr);
pom[nod] = join(pom[2 * nod], pom[2 * nod + 1]); }
static void update(int nod, int st, int dr) {
if (ql <= st && dr <= qr) {
pom[nod].enforce = max(pom[nod].enforce, qval);
pom[nod].maxans = max(pom[nod].maxans, pom[nod].maxval + pom[nod].enforce);
return; }
int mid = (st + dr) / 2;
prop(nod);
if (ql <= mid)
update(2 * nod, st, mid);
if (mid < qr)
update(2 * nod + 1, mid + 1, dr);
pom[nod] = join(pom[2 * nod], pom[2 * nod + 1]); }
static int query(int nod, int st, int dr) {
if (ql <= st && dr <= qr)
return eval(pom[nod]);
int ans = 0, mid = (st + dr) / 2;
prop(nod);
if (ql <= mid)
ans = max(ans, query(2 * nod, st, mid));
if (mid < qr)
ans = max(ans, query(2 * nod + 1, mid + 1, dr));
return ans; }
static void baga(int pos) {
for (auto nex: nxt[pos]) {
qval = v[pos] + v[nex];
ql = 2 * nex - pos;
qr = n;
if (ql <= qr)
update(1, 1, n); } }
int main() {
#ifdef HOME
freopen("triple_jump.in", "r", stdin);
freopen("triple_jump.out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int ptr;
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> v[i];
cin >> q;
qs.resize(q);
for (int i = 0; i < q; ++i) {
cin >> qs[i].l >> qs[i].r;
qs[i].idx = i; }
sort(begin(qs), end(qs), [&](const Query &a, const Query &b) { return a.l > b.l; });
build(1, 1, n);
get_nxt();
ptr = n;
for (auto myq: qs) {
while (myq.l <= ptr)
baga(ptr--);
ql = myq.l + 2, qr = myq.r;
ant[myq.idx] = query(1, 1, n); }
for (int i = 0; i < q; ++i)
cout << ant[i] << '\n';
return 0; }
Compilation message
jumps.cpp: In function 'void get_nxt()':
jumps.cpp:30:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i + 1 < stk.size(); ++i) {
~~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
12024 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
3 |
Correct |
13 ms |
12024 KB |
Output is correct |
4 |
Correct |
13 ms |
12024 KB |
Output is correct |
5 |
Correct |
12 ms |
12152 KB |
Output is correct |
6 |
Correct |
13 ms |
12024 KB |
Output is correct |
7 |
Correct |
14 ms |
12280 KB |
Output is correct |
8 |
Correct |
13 ms |
12024 KB |
Output is correct |
9 |
Correct |
12 ms |
12152 KB |
Output is correct |
10 |
Correct |
12 ms |
12152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
12024 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
3 |
Correct |
13 ms |
12024 KB |
Output is correct |
4 |
Correct |
13 ms |
12024 KB |
Output is correct |
5 |
Correct |
12 ms |
12152 KB |
Output is correct |
6 |
Correct |
13 ms |
12024 KB |
Output is correct |
7 |
Correct |
14 ms |
12280 KB |
Output is correct |
8 |
Correct |
13 ms |
12024 KB |
Output is correct |
9 |
Correct |
12 ms |
12152 KB |
Output is correct |
10 |
Correct |
12 ms |
12152 KB |
Output is correct |
11 |
Correct |
367 ms |
30088 KB |
Output is correct |
12 |
Correct |
366 ms |
29984 KB |
Output is correct |
13 |
Correct |
375 ms |
29972 KB |
Output is correct |
14 |
Correct |
362 ms |
30200 KB |
Output is correct |
15 |
Correct |
359 ms |
30072 KB |
Output is correct |
16 |
Correct |
390 ms |
29532 KB |
Output is correct |
17 |
Correct |
395 ms |
29336 KB |
Output is correct |
18 |
Correct |
543 ms |
29584 KB |
Output is correct |
19 |
Correct |
412 ms |
30072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
325 ms |
28100 KB |
Output is correct |
2 |
Correct |
140 ms |
27000 KB |
Output is correct |
3 |
Correct |
223 ms |
27756 KB |
Output is correct |
4 |
Correct |
235 ms |
27380 KB |
Output is correct |
5 |
Correct |
234 ms |
27256 KB |
Output is correct |
6 |
Correct |
253 ms |
27000 KB |
Output is correct |
7 |
Correct |
261 ms |
27260 KB |
Output is correct |
8 |
Correct |
264 ms |
27000 KB |
Output is correct |
9 |
Correct |
241 ms |
26996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
12024 KB |
Output is correct |
2 |
Correct |
13 ms |
12152 KB |
Output is correct |
3 |
Correct |
13 ms |
12024 KB |
Output is correct |
4 |
Correct |
13 ms |
12024 KB |
Output is correct |
5 |
Correct |
12 ms |
12152 KB |
Output is correct |
6 |
Correct |
13 ms |
12024 KB |
Output is correct |
7 |
Correct |
14 ms |
12280 KB |
Output is correct |
8 |
Correct |
13 ms |
12024 KB |
Output is correct |
9 |
Correct |
12 ms |
12152 KB |
Output is correct |
10 |
Correct |
12 ms |
12152 KB |
Output is correct |
11 |
Correct |
367 ms |
30088 KB |
Output is correct |
12 |
Correct |
366 ms |
29984 KB |
Output is correct |
13 |
Correct |
375 ms |
29972 KB |
Output is correct |
14 |
Correct |
362 ms |
30200 KB |
Output is correct |
15 |
Correct |
359 ms |
30072 KB |
Output is correct |
16 |
Correct |
390 ms |
29532 KB |
Output is correct |
17 |
Correct |
395 ms |
29336 KB |
Output is correct |
18 |
Correct |
543 ms |
29584 KB |
Output is correct |
19 |
Correct |
412 ms |
30072 KB |
Output is correct |
20 |
Correct |
325 ms |
28100 KB |
Output is correct |
21 |
Correct |
140 ms |
27000 KB |
Output is correct |
22 |
Correct |
223 ms |
27756 KB |
Output is correct |
23 |
Correct |
235 ms |
27380 KB |
Output is correct |
24 |
Correct |
234 ms |
27256 KB |
Output is correct |
25 |
Correct |
253 ms |
27000 KB |
Output is correct |
26 |
Correct |
261 ms |
27260 KB |
Output is correct |
27 |
Correct |
264 ms |
27000 KB |
Output is correct |
28 |
Correct |
241 ms |
26996 KB |
Output is correct |
29 |
Correct |
1315 ms |
68004 KB |
Output is correct |
30 |
Correct |
925 ms |
65836 KB |
Output is correct |
31 |
Correct |
1150 ms |
65940 KB |
Output is correct |
32 |
Correct |
1197 ms |
66552 KB |
Output is correct |
33 |
Correct |
1203 ms |
66704 KB |
Output is correct |
34 |
Correct |
1261 ms |
65008 KB |
Output is correct |
35 |
Correct |
1277 ms |
65288 KB |
Output is correct |
36 |
Correct |
1254 ms |
65512 KB |
Output is correct |
37 |
Correct |
1214 ms |
65652 KB |
Output is correct |
38 |
Correct |
982 ms |
66552 KB |
Output is correct |
39 |
Correct |
987 ms |
66520 KB |
Output is correct |
40 |
Correct |
1018 ms |
63956 KB |
Output is correct |
41 |
Correct |
1069 ms |
64416 KB |
Output is correct |
42 |
Correct |
1051 ms |
64288 KB |
Output is correct |
43 |
Correct |
992 ms |
64736 KB |
Output is correct |
44 |
Correct |
1014 ms |
66440 KB |
Output is correct |
45 |
Correct |
1012 ms |
66552 KB |
Output is correct |
46 |
Correct |
1120 ms |
64436 KB |
Output is correct |
47 |
Correct |
1078 ms |
64260 KB |
Output is correct |
48 |
Correct |
1116 ms |
64532 KB |
Output is correct |
49 |
Correct |
1039 ms |
65268 KB |
Output is correct |
50 |
Correct |
1066 ms |
66632 KB |
Output is correct |
51 |
Correct |
1108 ms |
66632 KB |
Output is correct |
52 |
Correct |
1171 ms |
65060 KB |
Output is correct |
53 |
Correct |
1153 ms |
64884 KB |
Output is correct |
54 |
Correct |
1151 ms |
65096 KB |
Output is correct |
55 |
Correct |
1081 ms |
65820 KB |
Output is correct |