// 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;
vector<int> st2;
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);
while(st2.size()){
int j = st2.back();
update(1, 1, n, j * 2 - i, n, a[i] + a[j]);
if(a[i] > a[j]) break;
st2.pop_back();
}
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:121:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | freopen("JOI19_jumps.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:122:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
122 | freopen("JOI19_jumps.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
26960 KB |
Output is correct |
2 |
Correct |
6 ms |
26960 KB |
Output is correct |
3 |
Correct |
5 ms |
27060 KB |
Output is correct |
4 |
Correct |
5 ms |
26960 KB |
Output is correct |
5 |
Correct |
7 ms |
27188 KB |
Output is correct |
6 |
Correct |
5 ms |
27148 KB |
Output is correct |
7 |
Correct |
4 ms |
27160 KB |
Output is correct |
8 |
Correct |
5 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
26960 KB |
Output is correct |
10 |
Correct |
5 ms |
26960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
26960 KB |
Output is correct |
2 |
Correct |
6 ms |
26960 KB |
Output is correct |
3 |
Correct |
5 ms |
27060 KB |
Output is correct |
4 |
Correct |
5 ms |
26960 KB |
Output is correct |
5 |
Correct |
7 ms |
27188 KB |
Output is correct |
6 |
Correct |
5 ms |
27148 KB |
Output is correct |
7 |
Correct |
4 ms |
27160 KB |
Output is correct |
8 |
Correct |
5 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
26960 KB |
Output is correct |
10 |
Correct |
5 ms |
26960 KB |
Output is correct |
11 |
Correct |
241 ms |
56572 KB |
Output is correct |
12 |
Correct |
217 ms |
56656 KB |
Output is correct |
13 |
Correct |
227 ms |
56700 KB |
Output is correct |
14 |
Correct |
224 ms |
56648 KB |
Output is correct |
15 |
Correct |
237 ms |
56668 KB |
Output is correct |
16 |
Correct |
219 ms |
56136 KB |
Output is correct |
17 |
Correct |
230 ms |
56392 KB |
Output is correct |
18 |
Correct |
221 ms |
56060 KB |
Output is correct |
19 |
Correct |
232 ms |
56600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
43368 KB |
Output is correct |
2 |
Correct |
82 ms |
44208 KB |
Output is correct |
3 |
Correct |
82 ms |
44412 KB |
Output is correct |
4 |
Correct |
165 ms |
43336 KB |
Output is correct |
5 |
Correct |
176 ms |
44356 KB |
Output is correct |
6 |
Correct |
153 ms |
44292 KB |
Output is correct |
7 |
Correct |
159 ms |
43892 KB |
Output is correct |
8 |
Correct |
152 ms |
44432 KB |
Output is correct |
9 |
Correct |
159 ms |
44668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
26960 KB |
Output is correct |
2 |
Correct |
6 ms |
26960 KB |
Output is correct |
3 |
Correct |
5 ms |
27060 KB |
Output is correct |
4 |
Correct |
5 ms |
26960 KB |
Output is correct |
5 |
Correct |
7 ms |
27188 KB |
Output is correct |
6 |
Correct |
5 ms |
27148 KB |
Output is correct |
7 |
Correct |
4 ms |
27160 KB |
Output is correct |
8 |
Correct |
5 ms |
26960 KB |
Output is correct |
9 |
Correct |
5 ms |
26960 KB |
Output is correct |
10 |
Correct |
5 ms |
26960 KB |
Output is correct |
11 |
Correct |
241 ms |
56572 KB |
Output is correct |
12 |
Correct |
217 ms |
56656 KB |
Output is correct |
13 |
Correct |
227 ms |
56700 KB |
Output is correct |
14 |
Correct |
224 ms |
56648 KB |
Output is correct |
15 |
Correct |
237 ms |
56668 KB |
Output is correct |
16 |
Correct |
219 ms |
56136 KB |
Output is correct |
17 |
Correct |
230 ms |
56392 KB |
Output is correct |
18 |
Correct |
221 ms |
56060 KB |
Output is correct |
19 |
Correct |
232 ms |
56600 KB |
Output is correct |
20 |
Correct |
154 ms |
43368 KB |
Output is correct |
21 |
Correct |
82 ms |
44208 KB |
Output is correct |
22 |
Correct |
82 ms |
44412 KB |
Output is correct |
23 |
Correct |
165 ms |
43336 KB |
Output is correct |
24 |
Correct |
176 ms |
44356 KB |
Output is correct |
25 |
Correct |
153 ms |
44292 KB |
Output is correct |
26 |
Correct |
159 ms |
43892 KB |
Output is correct |
27 |
Correct |
152 ms |
44432 KB |
Output is correct |
28 |
Correct |
159 ms |
44668 KB |
Output is correct |
29 |
Correct |
1020 ms |
89148 KB |
Output is correct |
30 |
Correct |
713 ms |
91328 KB |
Output is correct |
31 |
Correct |
667 ms |
91276 KB |
Output is correct |
32 |
Correct |
977 ms |
89168 KB |
Output is correct |
33 |
Correct |
917 ms |
89056 KB |
Output is correct |
34 |
Correct |
886 ms |
86856 KB |
Output is correct |
35 |
Correct |
969 ms |
86492 KB |
Output is correct |
36 |
Correct |
931 ms |
86584 KB |
Output is correct |
37 |
Correct |
1041 ms |
87880 KB |
Output is correct |
38 |
Correct |
685 ms |
91608 KB |
Output is correct |
39 |
Correct |
718 ms |
91720 KB |
Output is correct |
40 |
Correct |
689 ms |
88392 KB |
Output is correct |
41 |
Correct |
686 ms |
87880 KB |
Output is correct |
42 |
Correct |
750 ms |
87880 KB |
Output is correct |
43 |
Correct |
682 ms |
89708 KB |
Output is correct |
44 |
Correct |
731 ms |
92104 KB |
Output is correct |
45 |
Correct |
776 ms |
92036 KB |
Output is correct |
46 |
Correct |
661 ms |
88860 KB |
Output is correct |
47 |
Correct |
658 ms |
88408 KB |
Output is correct |
48 |
Correct |
655 ms |
88512 KB |
Output is correct |
49 |
Correct |
652 ms |
90476 KB |
Output is correct |
50 |
Correct |
760 ms |
92096 KB |
Output is correct |
51 |
Correct |
836 ms |
92156 KB |
Output is correct |
52 |
Correct |
827 ms |
89708 KB |
Output is correct |
53 |
Correct |
793 ms |
89496 KB |
Output is correct |
54 |
Correct |
772 ms |
89412 KB |
Output is correct |
55 |
Correct |
817 ms |
90952 KB |
Output is correct |