// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h>
using namespace std;
#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define RREP(i, s, e) for (int i = (s); i >= (e); i--)
template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 500005;
int n;
int a[MAXN];
int q;
vii qry[MAXN];
vi ev[MAXN];
ll ans[MAXN];
#define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
struct Node {
ll mxa, res, lz;
} st[MAXN * 4];
void init(int u = 1, int lo = 1, int hi = n) {
st[u].res = 0;
st[u].lz = -LINF;
if (lo == hi) {
st[u].mxa = a[lo];
return;
}
MLR;
init(lc, lo, mid);
init(rc, mid + 1, hi);
st[u].mxa = max(st[lc].mxa, st[rc].mxa);
}
void apply(ll x, int u, int lo, int hi) {
mxto(st[u].res, x + st[u].mxa);
mxto(st[u].lz, x);
}
void propo(int u, int lo, int hi) {
if (st[u].lz == -LINF) {
return;
}
MLR;
apply(st[u].lz, lc, lo, mid);
apply(st[u].lz, rc, mid + 1, hi);
st[u].lz = -LINF;
}
void updmx(int s, int e, ll x, int u = 1, int lo = 1, int hi = n) {
if (s > e) {
return;
}
if (lo >= s && hi <= e) {
apply(x, u, lo, hi);
return;
}
assert(lo != hi);
propo(u, lo, hi);
MLR;
if (s <= mid) {
updmx(s, e, x, lc, lo, mid);
}
if (e > mid) {
updmx(s, e, x, rc, mid + 1, hi);
}
st[u].res = max(st[lc].res, st[rc].res);
}
ll qmx(int s, int e, int u = 1, int lo = 1, int hi = n) {
if (lo >= s && hi <= e) {
return st[u].res;
}
MLR;
propo(u, lo, hi);
ll res = 0;
if (s <= mid) {
mxto(res, qmx(s, e, lc, lo, mid));
}
if (e > mid) {
mxto(res, qmx(s, e, rc, mid + 1, hi));
}
return res;
}
/*
void updmx(int s, int e, ll x) {
REP (i, s, e + 1) {
mxto(mx[i], x);
}
}
ll qmx(int s, int e) {
ll res = 0;
REP (i, s, e + 1) {
mxto(res, a[i] + mx[i]);
}
return res;
}
*/
int main() {
#ifndef DEBUG
ios::sync_with_stdio(0), cin.tie(0);
#endif
cin >> n;
REP (i, 1, n + 1) {
cin >> a[i];
}
vi stk;
REP (j, 1, n + 1) {
while (!stk.empty() && a[stk.back()] < a[j]) {
ev[stk.back()].pb(j);
stk.pop_back();
}
if (!stk.empty()) {
ev[stk.back()].pb(j);
}
stk.pb(j);
}
stk.clear();
RREP (j, n, 1) {
while (!stk.empty() && a[stk.back()] < a[j]) {
ev[j].pb(stk.back());
stk.pop_back();
}
if (!stk.empty()) {
ev[j].pb(stk.back());
}
stk.pb(j);
}
cin >> q;
REP (i, 1, q + 1) {
int l, r; cin >> l >> r;
qry[l].pb({r, i});
}
init();
RREP (l, n, 1) {
for (int r : ev[l]) {
// c - r = r - l
// c = 2 * r - l
updmx(2 * r - l, n, a[l] + a[r]);
}
for (auto [r, i] : qry[l]) {
ans[i] = qmx(l, r);
}
}
REP (i, 1, q + 1) {
cout << ans[i] << '\n';
}
return 0;
}
Compilation message
jumps.cpp: In function 'void init(int, int, int)':
jumps.cpp:47:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
| ~~~^~~~
jumps.cpp:58:5: note: in expansion of macro 'MLR'
58 | MLR;
| ^~~
jumps.cpp: In function 'void propo(int, int, int)':
jumps.cpp:47:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
| ~~~^~~~
jumps.cpp:71:5: note: in expansion of macro 'MLR'
71 | MLR;
| ^~~
jumps.cpp: In function 'void updmx(int, int, ll, int, int, int)':
jumps.cpp:47:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
| ~~~^~~~
jumps.cpp:86:5: note: in expansion of macro 'MLR'
86 | MLR;
| ^~~
jumps.cpp: In function 'll qmx(int, int, int, int, int)':
jumps.cpp:47:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | #define MLR int mid = lo + hi >> 1, lc = u << 1, rc = u << 1 ^ 1
| ~~~^~~~
jumps.cpp:99:5: note: in expansion of macro 'MLR'
99 | MLR;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23732 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23844 KB |
Output is correct |
6 |
Correct |
11 ms |
23768 KB |
Output is correct |
7 |
Correct |
12 ms |
23824 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23768 KB |
Output is correct |
10 |
Correct |
15 ms |
23824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23732 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23844 KB |
Output is correct |
6 |
Correct |
11 ms |
23768 KB |
Output is correct |
7 |
Correct |
12 ms |
23824 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23768 KB |
Output is correct |
10 |
Correct |
15 ms |
23824 KB |
Output is correct |
11 |
Correct |
237 ms |
40520 KB |
Output is correct |
12 |
Correct |
218 ms |
40224 KB |
Output is correct |
13 |
Correct |
226 ms |
40284 KB |
Output is correct |
14 |
Correct |
239 ms |
40288 KB |
Output is correct |
15 |
Correct |
227 ms |
40396 KB |
Output is correct |
16 |
Correct |
228 ms |
39768 KB |
Output is correct |
17 |
Correct |
229 ms |
39712 KB |
Output is correct |
18 |
Correct |
233 ms |
39696 KB |
Output is correct |
19 |
Correct |
226 ms |
40160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
202 ms |
45332 KB |
Output is correct |
2 |
Correct |
111 ms |
44220 KB |
Output is correct |
3 |
Correct |
107 ms |
43968 KB |
Output is correct |
4 |
Correct |
214 ms |
45324 KB |
Output is correct |
5 |
Correct |
206 ms |
45332 KB |
Output is correct |
6 |
Correct |
209 ms |
45328 KB |
Output is correct |
7 |
Correct |
203 ms |
45372 KB |
Output is correct |
8 |
Correct |
204 ms |
45320 KB |
Output is correct |
9 |
Correct |
204 ms |
45324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
11 ms |
23732 KB |
Output is correct |
3 |
Correct |
12 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23844 KB |
Output is correct |
6 |
Correct |
11 ms |
23768 KB |
Output is correct |
7 |
Correct |
12 ms |
23824 KB |
Output is correct |
8 |
Correct |
12 ms |
23764 KB |
Output is correct |
9 |
Correct |
12 ms |
23768 KB |
Output is correct |
10 |
Correct |
15 ms |
23824 KB |
Output is correct |
11 |
Correct |
237 ms |
40520 KB |
Output is correct |
12 |
Correct |
218 ms |
40224 KB |
Output is correct |
13 |
Correct |
226 ms |
40284 KB |
Output is correct |
14 |
Correct |
239 ms |
40288 KB |
Output is correct |
15 |
Correct |
227 ms |
40396 KB |
Output is correct |
16 |
Correct |
228 ms |
39768 KB |
Output is correct |
17 |
Correct |
229 ms |
39712 KB |
Output is correct |
18 |
Correct |
233 ms |
39696 KB |
Output is correct |
19 |
Correct |
226 ms |
40160 KB |
Output is correct |
20 |
Correct |
202 ms |
45332 KB |
Output is correct |
21 |
Correct |
111 ms |
44220 KB |
Output is correct |
22 |
Correct |
107 ms |
43968 KB |
Output is correct |
23 |
Correct |
214 ms |
45324 KB |
Output is correct |
24 |
Correct |
206 ms |
45332 KB |
Output is correct |
25 |
Correct |
209 ms |
45328 KB |
Output is correct |
26 |
Correct |
203 ms |
45372 KB |
Output is correct |
27 |
Correct |
204 ms |
45320 KB |
Output is correct |
28 |
Correct |
204 ms |
45324 KB |
Output is correct |
29 |
Correct |
1101 ms |
89368 KB |
Output is correct |
30 |
Correct |
788 ms |
87924 KB |
Output is correct |
31 |
Correct |
726 ms |
87912 KB |
Output is correct |
32 |
Correct |
1067 ms |
89332 KB |
Output is correct |
33 |
Correct |
1078 ms |
89440 KB |
Output is correct |
34 |
Correct |
1104 ms |
88724 KB |
Output is correct |
35 |
Correct |
1075 ms |
88696 KB |
Output is correct |
36 |
Correct |
1042 ms |
88616 KB |
Output is correct |
37 |
Correct |
1093 ms |
89248 KB |
Output is correct |
38 |
Correct |
772 ms |
94984 KB |
Output is correct |
39 |
Correct |
768 ms |
94940 KB |
Output is correct |
40 |
Correct |
753 ms |
93304 KB |
Output is correct |
41 |
Correct |
794 ms |
93160 KB |
Output is correct |
42 |
Correct |
772 ms |
93044 KB |
Output is correct |
43 |
Correct |
782 ms |
94020 KB |
Output is correct |
44 |
Correct |
809 ms |
94308 KB |
Output is correct |
45 |
Correct |
811 ms |
94332 KB |
Output is correct |
46 |
Correct |
797 ms |
92944 KB |
Output is correct |
47 |
Correct |
790 ms |
92664 KB |
Output is correct |
48 |
Correct |
799 ms |
92708 KB |
Output is correct |
49 |
Correct |
801 ms |
94028 KB |
Output is correct |
50 |
Correct |
898 ms |
92492 KB |
Output is correct |
51 |
Correct |
887 ms |
92412 KB |
Output is correct |
52 |
Correct |
882 ms |
91812 KB |
Output is correct |
53 |
Correct |
879 ms |
91540 KB |
Output is correct |
54 |
Correct |
878 ms |
91652 KB |
Output is correct |
55 |
Correct |
946 ms |
92404 KB |
Output is correct |