#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 5e5 + 5;
struct viet{
ll a, b, v;
};
viet st[N * 4];
ll a[N], ans[N];
vector<pair<ll, ll>> mem[N], qr[N];
void Init(ll k, ll l, ll r){
if (l == r){
st[k].a = a[l];
return;
}
ll mid = (l + r) >> 1;
Init(k << 1, l, mid);
Init(k << 1 | 1, mid + 1, r);
st[k].a = max(st[k << 1].a, st[k << 1 | 1].a);
}
void down(ll k){
st[k << 1].b = max(st[k << 1].b, st[k].b);
st[k << 1].v = max(st[k << 1].v, st[k << 1].a + st[k << 1].b);
st[k << 1 | 1].b = max(st[k << 1 | 1].b, st[k].b);
st[k << 1 | 1].v = max(st[k << 1 | 1].v, st[k << 1 | 1].a + st[k << 1 | 1].b);
}
void upd(ll k, ll l, ll r, ll x, ll y, ll v){
if (x > r || y < l) return;
if (l != r) down(k);
if (x <= l && y >= r){
st[k].b = max(st[k].b, v);
st[k].v = max(st[k].v, st[k].b + st[k].a);
return;
}
ll mid = (l + r) >> 1;
upd(k << 1, l, mid, x, y, v);
upd(k << 1 | 1, mid + 1, r, x, y, v);
st[k].v = max(st[k << 1].v, st[k << 1 | 1].v);
}
ll get(ll k, ll l, ll r, ll x, ll y){
if (x > r || y < l) return 0;
if (l != r) down(k);
if (x <= l && y >= r) return st[k].v;
ll mid = (l + r) >> 1;
return max(get(k << 1, l, mid, x, y), get(k << 1 | 1, mid + 1, r, x, y));
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("balance.in", "r")){
freopen("balance.in", "r", stdin);
freopen("balance.out", "w", stdout);
}
ll n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
stack<ll> s;
for (int i = 1; i <= n; i++){
while (s.size()){
if (a[s.top()] <= a[i]){
ll k = 2 * i - s.top();
if (k <= n) mem[s.top()].push_back({k, a[i] + a[s.top()]});
} else break;
s.pop();
}
if (s.size()){
ll k = 2 * i - s.top();
if (k <= n) mem[s.top()].push_back({k, a[i] + a[s.top()]});
}
s.push(i);
}
ll q;
cin >> q;
for (int i = 1; i <= q; i++){
ll l, r;
cin >> l >> r;
qr[l].push_back({r, i});
}
Init(1, 1, n);
for (int i = n; i >= 1; i--){
for (auto j : mem[i]){
upd(1, 1, n, j.first, n, j.second);
}
for (auto j : qr[i]){
ans[j.second] = get(1, 1, n, i, j.first);
}
}
for (int i = 1; i <= q; i++) cout << ans[i] << endl;
}
/*
*/
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:54:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
54 | freopen("balance.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:55:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
55 | freopen("balance.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23808 KB |
Output is correct |
2 |
Correct |
17 ms |
23808 KB |
Output is correct |
3 |
Correct |
17 ms |
23936 KB |
Output is correct |
4 |
Correct |
16 ms |
23808 KB |
Output is correct |
5 |
Correct |
17 ms |
23808 KB |
Output is correct |
6 |
Correct |
16 ms |
23808 KB |
Output is correct |
7 |
Correct |
17 ms |
23808 KB |
Output is correct |
8 |
Correct |
17 ms |
23808 KB |
Output is correct |
9 |
Correct |
18 ms |
23808 KB |
Output is correct |
10 |
Correct |
18 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23808 KB |
Output is correct |
2 |
Correct |
17 ms |
23808 KB |
Output is correct |
3 |
Correct |
17 ms |
23936 KB |
Output is correct |
4 |
Correct |
16 ms |
23808 KB |
Output is correct |
5 |
Correct |
17 ms |
23808 KB |
Output is correct |
6 |
Correct |
16 ms |
23808 KB |
Output is correct |
7 |
Correct |
17 ms |
23808 KB |
Output is correct |
8 |
Correct |
17 ms |
23808 KB |
Output is correct |
9 |
Correct |
18 ms |
23808 KB |
Output is correct |
10 |
Correct |
18 ms |
23936 KB |
Output is correct |
11 |
Correct |
403 ms |
50808 KB |
Output is correct |
12 |
Correct |
407 ms |
50708 KB |
Output is correct |
13 |
Correct |
403 ms |
50788 KB |
Output is correct |
14 |
Correct |
420 ms |
50836 KB |
Output is correct |
15 |
Correct |
401 ms |
50752 KB |
Output is correct |
16 |
Correct |
408 ms |
50168 KB |
Output is correct |
17 |
Correct |
422 ms |
50168 KB |
Output is correct |
18 |
Correct |
414 ms |
50160 KB |
Output is correct |
19 |
Correct |
417 ms |
50692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
235 ms |
49780 KB |
Output is correct |
2 |
Correct |
135 ms |
45688 KB |
Output is correct |
3 |
Correct |
144 ms |
47352 KB |
Output is correct |
4 |
Correct |
239 ms |
49912 KB |
Output is correct |
5 |
Correct |
238 ms |
49656 KB |
Output is correct |
6 |
Correct |
232 ms |
49180 KB |
Output is correct |
7 |
Correct |
232 ms |
49144 KB |
Output is correct |
8 |
Correct |
227 ms |
49016 KB |
Output is correct |
9 |
Correct |
232 ms |
49400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
23808 KB |
Output is correct |
2 |
Correct |
17 ms |
23808 KB |
Output is correct |
3 |
Correct |
17 ms |
23936 KB |
Output is correct |
4 |
Correct |
16 ms |
23808 KB |
Output is correct |
5 |
Correct |
17 ms |
23808 KB |
Output is correct |
6 |
Correct |
16 ms |
23808 KB |
Output is correct |
7 |
Correct |
17 ms |
23808 KB |
Output is correct |
8 |
Correct |
17 ms |
23808 KB |
Output is correct |
9 |
Correct |
18 ms |
23808 KB |
Output is correct |
10 |
Correct |
18 ms |
23936 KB |
Output is correct |
11 |
Correct |
403 ms |
50808 KB |
Output is correct |
12 |
Correct |
407 ms |
50708 KB |
Output is correct |
13 |
Correct |
403 ms |
50788 KB |
Output is correct |
14 |
Correct |
420 ms |
50836 KB |
Output is correct |
15 |
Correct |
401 ms |
50752 KB |
Output is correct |
16 |
Correct |
408 ms |
50168 KB |
Output is correct |
17 |
Correct |
422 ms |
50168 KB |
Output is correct |
18 |
Correct |
414 ms |
50160 KB |
Output is correct |
19 |
Correct |
417 ms |
50692 KB |
Output is correct |
20 |
Correct |
235 ms |
49780 KB |
Output is correct |
21 |
Correct |
135 ms |
45688 KB |
Output is correct |
22 |
Correct |
144 ms |
47352 KB |
Output is correct |
23 |
Correct |
239 ms |
49912 KB |
Output is correct |
24 |
Correct |
238 ms |
49656 KB |
Output is correct |
25 |
Correct |
232 ms |
49180 KB |
Output is correct |
26 |
Correct |
232 ms |
49144 KB |
Output is correct |
27 |
Correct |
227 ms |
49016 KB |
Output is correct |
28 |
Correct |
232 ms |
49400 KB |
Output is correct |
29 |
Correct |
1488 ms |
111040 KB |
Output is correct |
30 |
Correct |
1249 ms |
100960 KB |
Output is correct |
31 |
Correct |
1275 ms |
105320 KB |
Output is correct |
32 |
Correct |
1556 ms |
111048 KB |
Output is correct |
33 |
Correct |
1505 ms |
111084 KB |
Output is correct |
34 |
Correct |
1481 ms |
108780 KB |
Output is correct |
35 |
Correct |
1518 ms |
108540 KB |
Output is correct |
36 |
Correct |
1591 ms |
108404 KB |
Output is correct |
37 |
Correct |
1551 ms |
109880 KB |
Output is correct |
38 |
Correct |
1050 ms |
113748 KB |
Output is correct |
39 |
Correct |
1068 ms |
113656 KB |
Output is correct |
40 |
Correct |
1020 ms |
110284 KB |
Output is correct |
41 |
Correct |
1043 ms |
109796 KB |
Output is correct |
42 |
Correct |
1027 ms |
109780 KB |
Output is correct |
43 |
Correct |
1026 ms |
111576 KB |
Output is correct |
44 |
Correct |
1139 ms |
113992 KB |
Output is correct |
45 |
Correct |
1148 ms |
113868 KB |
Output is correct |
46 |
Correct |
1117 ms |
110772 KB |
Output is correct |
47 |
Correct |
1090 ms |
110328 KB |
Output is correct |
48 |
Correct |
1081 ms |
110472 KB |
Output is correct |
49 |
Correct |
1068 ms |
112632 KB |
Output is correct |
50 |
Correct |
1217 ms |
114232 KB |
Output is correct |
51 |
Correct |
1189 ms |
114168 KB |
Output is correct |
52 |
Correct |
1186 ms |
111712 KB |
Output is correct |
53 |
Correct |
1177 ms |
111492 KB |
Output is correct |
54 |
Correct |
1198 ms |
111348 KB |
Output is correct |
55 |
Correct |
1197 ms |
113144 KB |
Output is correct |