// 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> st1, st2;
vector<int> st3, st4;
Ford(i, n, 1){
while(st1.size() && a[st1.back()] <= a[i]){
int j = st1.back();
update(1, 1, n, j * 2 - i, n, a[i] + a[j]);
st1.pop_back();
}
if(st1.size()){
int j = st1.back();
update(1, 1, n, j * 2 - i, n, a[i] + a[j]);
}
st1.push_back(i);
while(st2.size() && a[st2.back()] >= a[i]){
int j = st2.back();
update(1, 1, n, j * 2 - i, n, a[i] + a[j]);
st2.pop_back();
}
if(st2.size()){
int j = st2.back();
update(1, 1, n, j * 2 - i, n, a[i] + a[j]);
}
st2.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:130:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
130 | freopen("JOI19_jumps.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:131:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | freopen("JOI19_jumps.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
26960 KB |
Output is correct |
2 |
Correct |
5 ms |
27088 KB |
Output is correct |
3 |
Correct |
5 ms |
27128 KB |
Output is correct |
4 |
Correct |
6 ms |
26960 KB |
Output is correct |
5 |
Correct |
5 ms |
26960 KB |
Output is correct |
6 |
Correct |
5 ms |
26960 KB |
Output is correct |
7 |
Correct |
4 ms |
26960 KB |
Output is correct |
8 |
Correct |
6 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
26960 KB |
Output is correct |
10 |
Correct |
4 ms |
26960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
26960 KB |
Output is correct |
2 |
Correct |
5 ms |
27088 KB |
Output is correct |
3 |
Correct |
5 ms |
27128 KB |
Output is correct |
4 |
Correct |
6 ms |
26960 KB |
Output is correct |
5 |
Correct |
5 ms |
26960 KB |
Output is correct |
6 |
Correct |
5 ms |
26960 KB |
Output is correct |
7 |
Correct |
4 ms |
26960 KB |
Output is correct |
8 |
Correct |
6 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
26960 KB |
Output is correct |
10 |
Correct |
4 ms |
26960 KB |
Output is correct |
11 |
Correct |
228 ms |
54508 KB |
Output is correct |
12 |
Correct |
223 ms |
54604 KB |
Output is correct |
13 |
Correct |
212 ms |
54740 KB |
Output is correct |
14 |
Correct |
221 ms |
54600 KB |
Output is correct |
15 |
Correct |
220 ms |
54600 KB |
Output is correct |
16 |
Correct |
227 ms |
51528 KB |
Output is correct |
17 |
Correct |
243 ms |
54088 KB |
Output is correct |
18 |
Correct |
215 ms |
53952 KB |
Output is correct |
19 |
Correct |
214 ms |
51900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
150 ms |
43336 KB |
Output is correct |
2 |
Correct |
76 ms |
44228 KB |
Output is correct |
3 |
Correct |
76 ms |
44228 KB |
Output is correct |
4 |
Correct |
161 ms |
43336 KB |
Output is correct |
5 |
Correct |
152 ms |
43340 KB |
Output is correct |
6 |
Correct |
148 ms |
43476 KB |
Output is correct |
7 |
Correct |
153 ms |
43336 KB |
Output is correct |
8 |
Correct |
158 ms |
44104 KB |
Output is correct |
9 |
Correct |
147 ms |
43336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
26960 KB |
Output is correct |
2 |
Correct |
5 ms |
27088 KB |
Output is correct |
3 |
Correct |
5 ms |
27128 KB |
Output is correct |
4 |
Correct |
6 ms |
26960 KB |
Output is correct |
5 |
Correct |
5 ms |
26960 KB |
Output is correct |
6 |
Correct |
5 ms |
26960 KB |
Output is correct |
7 |
Correct |
4 ms |
26960 KB |
Output is correct |
8 |
Correct |
6 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
26960 KB |
Output is correct |
10 |
Correct |
4 ms |
26960 KB |
Output is correct |
11 |
Correct |
228 ms |
54508 KB |
Output is correct |
12 |
Correct |
223 ms |
54604 KB |
Output is correct |
13 |
Correct |
212 ms |
54740 KB |
Output is correct |
14 |
Correct |
221 ms |
54600 KB |
Output is correct |
15 |
Correct |
220 ms |
54600 KB |
Output is correct |
16 |
Correct |
227 ms |
51528 KB |
Output is correct |
17 |
Correct |
243 ms |
54088 KB |
Output is correct |
18 |
Correct |
215 ms |
53952 KB |
Output is correct |
19 |
Correct |
214 ms |
51900 KB |
Output is correct |
20 |
Correct |
150 ms |
43336 KB |
Output is correct |
21 |
Correct |
76 ms |
44228 KB |
Output is correct |
22 |
Correct |
76 ms |
44228 KB |
Output is correct |
23 |
Correct |
161 ms |
43336 KB |
Output is correct |
24 |
Correct |
152 ms |
43340 KB |
Output is correct |
25 |
Correct |
148 ms |
43476 KB |
Output is correct |
26 |
Correct |
153 ms |
43336 KB |
Output is correct |
27 |
Correct |
158 ms |
44104 KB |
Output is correct |
28 |
Correct |
147 ms |
43336 KB |
Output is correct |
29 |
Correct |
907 ms |
78148 KB |
Output is correct |
30 |
Correct |
727 ms |
85796 KB |
Output is correct |
31 |
Correct |
687 ms |
85948 KB |
Output is correct |
32 |
Correct |
950 ms |
83824 KB |
Output is correct |
33 |
Correct |
967 ms |
83772 KB |
Output is correct |
34 |
Correct |
958 ms |
77496 KB |
Output is correct |
35 |
Correct |
962 ms |
82248 KB |
Output is correct |
36 |
Correct |
924 ms |
77384 KB |
Output is correct |
37 |
Correct |
950 ms |
78032 KB |
Output is correct |
38 |
Correct |
654 ms |
86344 KB |
Output is correct |
39 |
Correct |
624 ms |
80712 KB |
Output is correct |
40 |
Correct |
611 ms |
78920 KB |
Output is correct |
41 |
Correct |
636 ms |
78988 KB |
Output is correct |
42 |
Correct |
666 ms |
78664 KB |
Output is correct |
43 |
Correct |
649 ms |
79688 KB |
Output is correct |
44 |
Correct |
749 ms |
86544 KB |
Output is correct |
45 |
Correct |
699 ms |
86600 KB |
Output is correct |
46 |
Correct |
687 ms |
79412 KB |
Output is correct |
47 |
Correct |
732 ms |
79432 KB |
Output is correct |
48 |
Correct |
704 ms |
84080 KB |
Output is correct |
49 |
Correct |
689 ms |
85576 KB |
Output is correct |
50 |
Correct |
831 ms |
81232 KB |
Output is correct |
51 |
Correct |
854 ms |
87000 KB |
Output is correct |
52 |
Correct |
750 ms |
85084 KB |
Output is correct |
53 |
Correct |
754 ms |
85064 KB |
Output is correct |
54 |
Correct |
788 ms |
80316 KB |
Output is correct |
55 |
Correct |
769 ms |
80968 KB |
Output is correct |