#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
void setIO() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen("input.inp", "r")) {
freopen("input.inp", "r", stdin);
freopen("input.out", "w", stdout);
}
}
const int N = 5e5+5;
int n, q, a[N], ans[N];
// segment tree
int nax[N << 2], cand[N << 2], lazy[N << 2];
void apply(int id) {
if (lazy[id] == 0) return;
int k = id << 1;
lazy[k] = max(lazy[k], lazy[id]);
cand[k] = max(cand[k], nax[k] + lazy[k]);
lazy[k+1] = max(lazy[k+1], lazy[id]);
cand[k+1] = max(cand[k+1], nax[k+1] + lazy[k+1]);
lazy[id] = 0;
}
void build(int id = 1, int lb = 1, int rb = n) {
if (lb ^ rb) {
int k = id << 1, mb = (lb + rb) >> 1;
build(k, lb, mb);
build(k+1, mb+1, rb);
nax[id] = max(nax[k], nax[k+1]);
}
else {
nax[id] = a[lb];
}
}
void update(int l, int v, int id = 1, int lb = 1, int rb = n) {
if (l <= lb) {
lazy[id] = max(lazy[id], v);
cand[id] = max(cand[id], v + nax[id]);
return;
}
else if (rb < l) {
return;
}
int k = id << 1, mb = (lb + rb) >> 1;
apply(id);
update(l, v, k, lb, mb);
update(l, v, k+1, mb+1, rb);
cand[id] = max(cand[k], cand[k+1]);
}
int question(int l, int r, int id = 1, int lb = 1, int rb = n) {
if (l <= lb && rb <= r) {
return cand[id];
}
else if (rb < l || lb > r) {
return 0;
}
int k = id << 1, mb = (lb + rb) >> 1;
apply(id);
return max(question(l, r, k, lb, mb), question(l, r, k+1, mb+1, rb));
}
// querys
vector<pair<ii, int>> querys;
int main() {
setIO();
cin >> n;
foru(i, 1, n) cin >> a[i];
build();
cin >> q;
foru(i, 1, q) {
int l, r;
cin >> l >> r;
querys.push_back({{l, r}, i});
}
sort(all(querys), greater<pair<ii, int>>());
stack<int> st;
int j = 0;
ford(i, n, 1) {
while (!st.empty()) {
int j = st.top();
if (2*j-i <= n) {
// cout << "upd " << i << " " << j << " at " << 2*j-i << " value " << a[i] + a[j] << "\n";
update(2*j-i, a[i] + a[j]);
}
if (a[j] <= a[i]) st.pop();
else break;
}
st.push(i);
while (j < q && querys[j].first.first == i) {
auto ask = querys[j];
int l = ask.first.first, r = ask.first.second, id = ask.second;
ans[id] = question(l, r);
// cout << "ques " << l << " " << r << " " << ans[id] << "\n";
++j;
}
}
foru(i, 1, q) {
cout << ans[i] << "\n";
}
}
Compilation message
jumps.cpp: In function 'void setIO()':
jumps.cpp:17:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen("input.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:18:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | freopen("input.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6612 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6604 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6612 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6604 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
252 ms |
20408 KB |
Output is correct |
12 |
Correct |
243 ms |
20736 KB |
Output is correct |
13 |
Correct |
236 ms |
20532 KB |
Output is correct |
14 |
Correct |
245 ms |
20560 KB |
Output is correct |
15 |
Correct |
237 ms |
20404 KB |
Output is correct |
16 |
Correct |
241 ms |
19888 KB |
Output is correct |
17 |
Correct |
239 ms |
19896 KB |
Output is correct |
18 |
Correct |
246 ms |
19776 KB |
Output is correct |
19 |
Correct |
270 ms |
20404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
14164 KB |
Output is correct |
2 |
Correct |
55 ms |
15016 KB |
Output is correct |
3 |
Correct |
47 ms |
14232 KB |
Output is correct |
4 |
Correct |
83 ms |
14164 KB |
Output is correct |
5 |
Correct |
83 ms |
14160 KB |
Output is correct |
6 |
Correct |
80 ms |
14160 KB |
Output is correct |
7 |
Correct |
89 ms |
14056 KB |
Output is correct |
8 |
Correct |
94 ms |
14168 KB |
Output is correct |
9 |
Correct |
81 ms |
14020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6612 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6604 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
252 ms |
20408 KB |
Output is correct |
12 |
Correct |
243 ms |
20736 KB |
Output is correct |
13 |
Correct |
236 ms |
20532 KB |
Output is correct |
14 |
Correct |
245 ms |
20560 KB |
Output is correct |
15 |
Correct |
237 ms |
20404 KB |
Output is correct |
16 |
Correct |
241 ms |
19888 KB |
Output is correct |
17 |
Correct |
239 ms |
19896 KB |
Output is correct |
18 |
Correct |
246 ms |
19776 KB |
Output is correct |
19 |
Correct |
270 ms |
20404 KB |
Output is correct |
20 |
Correct |
86 ms |
14164 KB |
Output is correct |
21 |
Correct |
55 ms |
15016 KB |
Output is correct |
22 |
Correct |
47 ms |
14232 KB |
Output is correct |
23 |
Correct |
83 ms |
14164 KB |
Output is correct |
24 |
Correct |
83 ms |
14160 KB |
Output is correct |
25 |
Correct |
80 ms |
14160 KB |
Output is correct |
26 |
Correct |
89 ms |
14056 KB |
Output is correct |
27 |
Correct |
94 ms |
14168 KB |
Output is correct |
28 |
Correct |
81 ms |
14020 KB |
Output is correct |
29 |
Correct |
590 ms |
38132 KB |
Output is correct |
30 |
Correct |
461 ms |
39384 KB |
Output is correct |
31 |
Correct |
502 ms |
37480 KB |
Output is correct |
32 |
Correct |
577 ms |
37472 KB |
Output is correct |
33 |
Correct |
580 ms |
37496 KB |
Output is correct |
34 |
Correct |
590 ms |
36120 KB |
Output is correct |
35 |
Correct |
569 ms |
35996 KB |
Output is correct |
36 |
Correct |
608 ms |
36244 KB |
Output is correct |
37 |
Correct |
555 ms |
36920 KB |
Output is correct |
38 |
Correct |
468 ms |
37544 KB |
Output is correct |
39 |
Correct |
480 ms |
37424 KB |
Output is correct |
40 |
Correct |
494 ms |
35128 KB |
Output is correct |
41 |
Correct |
493 ms |
34900 KB |
Output is correct |
42 |
Correct |
466 ms |
34692 KB |
Output is correct |
43 |
Correct |
468 ms |
35912 KB |
Output is correct |
44 |
Correct |
482 ms |
37608 KB |
Output is correct |
45 |
Correct |
480 ms |
37688 KB |
Output is correct |
46 |
Correct |
480 ms |
35292 KB |
Output is correct |
47 |
Correct |
475 ms |
34972 KB |
Output is correct |
48 |
Correct |
495 ms |
35196 KB |
Output is correct |
49 |
Correct |
492 ms |
36600 KB |
Output is correct |
50 |
Correct |
507 ms |
37840 KB |
Output is correct |
51 |
Correct |
528 ms |
37952 KB |
Output is correct |
52 |
Correct |
513 ms |
35828 KB |
Output is correct |
53 |
Correct |
511 ms |
35984 KB |
Output is correct |
54 |
Correct |
519 ms |
35824 KB |
Output is correct |
55 |
Correct |
509 ms |
36904 KB |
Output is correct |