// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v) memset(h, v, sizeof h)
#define Forv(i, a) for(auto& i : a)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define c_bit(i) __builtin_popcountll(i)
#define Bit(mask, i) ((mask >> i) & 1LL)
#define onbit(mask, i) ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
template <typename T> struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e6 + 5;
const ll mod = 1e9 + 7;
const ll oo = 1e18;
//#define int long long
int n, a[MAXN], q;
vector<ii> qr[MAXN];
ll st[MAXN << 2], mx[MAXN << 2], lazy[MAXN << 2];
void down(int id, int l, int r){
if(!lazy[id] || l == r) return;
for(int i : {id * 2, id * 2 + 1}){
maximize(mx[i], st[i] + lazy[id]);
maximize(lazy[i], lazy[id]);
}
lazy[id] = 0;
}
void build(int id, int l, int r){
if(l == r){
st[id] = a[l];
return;
}
int mid = l + r >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
void update(int id, int l, int r, int u, int v, int val){
if(v < l || r < u) return;
down(id, l, r);
if(u <= l && r <= v){
maximize(mx[id], val + st[id]);
maximize(lazy[id], val);
down(id, l, r);
return;
}
int mid = l + r >> 1;
update(id << 1, l, mid, u, v, val);
update(id << 1 | 1, mid + 1, r, u, v, val);
mx[id] = max(mx[id << 1], mx[id << 1 | 1]);
}
int get(int id, int l, int r, int u, int v){
if (v < l || r < u) return 0;
down(id, l, r);
if (u <= l && r <= v) return mx[id];
int mid = l + r >> 1;
return max(get(id << 1, l, mid, u, v), get(id << 1 | 1, mid + 1, r, u, v));
}
ll res[MAXN];
void Solve() {
cin >> n;
For (i, 1, n) cin >> a[i];
build(1, 1, n);
cin >> q;
For (i, 1, q){
int l, r; cin >> l >> r;
qr[l].push_back({r, i});
}
vector<int> st;
Ford(i, n, 1){
while(st.size()){
int j = st.back();
update(1, 1, n, j * 2 - i, n, a[i] + a[j]);
if(a[i] < a[j]) break;
st.pop_back();
}
st.push_back(i);
for(auto [r, id] : qr[i]) res[id] = get(1, 1, n, i + 2, r);
}
For(i, 1, q) cout << res[i] << '\n';
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(0);
if(fopen("JOI19_jumps.inp", "r")) {
freopen("JOI19_jumps.inp", "r", stdin);
freopen("JOI19_jumps.out", "w", stdout);
}
int t = 1;
// cin >> t;
for (int test = 1; test <= t; test++) {
Solve();
}
return 0;
}
Compilation message
jumps.cpp: In function 'void build(int, int, int)':
jumps.cpp:56:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'void update(int, int, int, int, int, int)':
jumps.cpp:71:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int get(int, int, int, int, int)':
jumps.cpp:81:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
81 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int32_t main()':
jumps.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
113 | freopen("JOI19_jumps.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | freopen("JOI19_jumps.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26960 KB |
Output is correct |
2 |
Correct |
5 ms |
26960 KB |
Output is correct |
3 |
Correct |
6 ms |
26960 KB |
Output is correct |
4 |
Correct |
5 ms |
26960 KB |
Output is correct |
5 |
Correct |
5 ms |
27164 KB |
Output is correct |
6 |
Correct |
5 ms |
26960 KB |
Output is correct |
7 |
Correct |
5 ms |
26960 KB |
Output is correct |
8 |
Correct |
4 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
26972 KB |
Output is correct |
10 |
Correct |
5 ms |
27136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26960 KB |
Output is correct |
2 |
Correct |
5 ms |
26960 KB |
Output is correct |
3 |
Correct |
6 ms |
26960 KB |
Output is correct |
4 |
Correct |
5 ms |
26960 KB |
Output is correct |
5 |
Correct |
5 ms |
27164 KB |
Output is correct |
6 |
Correct |
5 ms |
26960 KB |
Output is correct |
7 |
Correct |
5 ms |
26960 KB |
Output is correct |
8 |
Correct |
4 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
26972 KB |
Output is correct |
10 |
Correct |
5 ms |
27136 KB |
Output is correct |
11 |
Correct |
216 ms |
56660 KB |
Output is correct |
12 |
Correct |
215 ms |
56652 KB |
Output is correct |
13 |
Correct |
192 ms |
56804 KB |
Output is correct |
14 |
Correct |
232 ms |
56648 KB |
Output is correct |
15 |
Correct |
229 ms |
56648 KB |
Output is correct |
16 |
Correct |
241 ms |
56192 KB |
Output is correct |
17 |
Correct |
241 ms |
56144 KB |
Output is correct |
18 |
Correct |
240 ms |
55880 KB |
Output is correct |
19 |
Correct |
243 ms |
56648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
98 ms |
45128 KB |
Output is correct |
2 |
Correct |
59 ms |
46156 KB |
Output is correct |
3 |
Correct |
67 ms |
45036 KB |
Output is correct |
4 |
Correct |
100 ms |
45128 KB |
Output is correct |
5 |
Correct |
98 ms |
45140 KB |
Output is correct |
6 |
Correct |
94 ms |
44492 KB |
Output is correct |
7 |
Correct |
113 ms |
44360 KB |
Output is correct |
8 |
Correct |
100 ms |
44276 KB |
Output is correct |
9 |
Correct |
93 ms |
44780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
26960 KB |
Output is correct |
2 |
Correct |
5 ms |
26960 KB |
Output is correct |
3 |
Correct |
6 ms |
26960 KB |
Output is correct |
4 |
Correct |
5 ms |
26960 KB |
Output is correct |
5 |
Correct |
5 ms |
27164 KB |
Output is correct |
6 |
Correct |
5 ms |
26960 KB |
Output is correct |
7 |
Correct |
5 ms |
26960 KB |
Output is correct |
8 |
Correct |
4 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
26972 KB |
Output is correct |
10 |
Correct |
5 ms |
27136 KB |
Output is correct |
11 |
Correct |
216 ms |
56660 KB |
Output is correct |
12 |
Correct |
215 ms |
56652 KB |
Output is correct |
13 |
Correct |
192 ms |
56804 KB |
Output is correct |
14 |
Correct |
232 ms |
56648 KB |
Output is correct |
15 |
Correct |
229 ms |
56648 KB |
Output is correct |
16 |
Correct |
241 ms |
56192 KB |
Output is correct |
17 |
Correct |
241 ms |
56144 KB |
Output is correct |
18 |
Correct |
240 ms |
55880 KB |
Output is correct |
19 |
Correct |
243 ms |
56648 KB |
Output is correct |
20 |
Correct |
98 ms |
45128 KB |
Output is correct |
21 |
Correct |
59 ms |
46156 KB |
Output is correct |
22 |
Correct |
67 ms |
45036 KB |
Output is correct |
23 |
Correct |
100 ms |
45128 KB |
Output is correct |
24 |
Correct |
98 ms |
45140 KB |
Output is correct |
25 |
Correct |
94 ms |
44492 KB |
Output is correct |
26 |
Correct |
113 ms |
44360 KB |
Output is correct |
27 |
Correct |
100 ms |
44276 KB |
Output is correct |
28 |
Correct |
93 ms |
44780 KB |
Output is correct |
29 |
Correct |
886 ms |
89164 KB |
Output is correct |
30 |
Correct |
694 ms |
91256 KB |
Output is correct |
31 |
Correct |
612 ms |
89132 KB |
Output is correct |
32 |
Correct |
853 ms |
89168 KB |
Output is correct |
33 |
Correct |
820 ms |
89156 KB |
Output is correct |
34 |
Correct |
850 ms |
86856 KB |
Output is correct |
35 |
Correct |
831 ms |
86600 KB |
Output is correct |
36 |
Correct |
786 ms |
86648 KB |
Output is correct |
37 |
Correct |
777 ms |
88048 KB |
Output is correct |
38 |
Correct |
499 ms |
91628 KB |
Output is correct |
39 |
Correct |
501 ms |
91792 KB |
Output is correct |
40 |
Correct |
478 ms |
88424 KB |
Output is correct |
41 |
Correct |
493 ms |
87880 KB |
Output is correct |
42 |
Correct |
484 ms |
88000 KB |
Output is correct |
43 |
Correct |
450 ms |
89564 KB |
Output is correct |
44 |
Correct |
540 ms |
91904 KB |
Output is correct |
45 |
Correct |
550 ms |
91984 KB |
Output is correct |
46 |
Correct |
509 ms |
88912 KB |
Output is correct |
47 |
Correct |
530 ms |
88392 KB |
Output is correct |
48 |
Correct |
521 ms |
88400 KB |
Output is correct |
49 |
Correct |
512 ms |
90552 KB |
Output is correct |
50 |
Correct |
615 ms |
92240 KB |
Output is correct |
51 |
Correct |
617 ms |
92264 KB |
Output is correct |
52 |
Correct |
581 ms |
89672 KB |
Output is correct |
53 |
Correct |
624 ms |
89388 KB |
Output is correct |
54 |
Correct |
594 ms |
89412 KB |
Output is correct |
55 |
Correct |
664 ms |
90980 KB |
Output is correct |