#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 5e5 + 10;
int lazy[N << 2], T[N << 2], mx[N << 2];
int a[N], ans[N];
vector<pair<int, int> > query[N];
void build(int s, int l, int r) {
lazy[s] = -2e9;
if (l == r) {
mx[s] = a[l];
return;
}
int mid = l + r >> 1;
build(s << 1, l, mid);
build(s << 1 | 1, mid + 1, r);
mx[s] = max(mx[s << 1], mx[s << 1 | 1]);
}
void push(int s, int l, int r) {
T[s] = max(T[s], lazy[s] + mx[s]);
if (l != r) {
lazy[s << 1] = max(lazy[s << 1], lazy[s]);
lazy[s << 1 | 1] = max(lazy[s << 1 | 1], lazy[s]);
}
}
void up(int s, int l, int r, int from, int to, int val) {
push(s, l, r);
if (l > to || r < from) return;
if (from <= l && r <= to) {
lazy[s] = max(lazy[s], val);
push(s, l, r);
return;
}
int mid = l + r >> 1;
up(s << 1, l, mid, from, to, val);
up(s << 1 | 1, mid + 1, r, from, to, val);
T[s] = max(T[s << 1], T[s << 1 | 1]);
}
int get(int s, int l, int r, int from, int to) {
push(s, l, r);
if (l > to || r < from) return 0;
if (from <= l && r <= to) return T[s];
int mid = l + r >> 1;
return max(get(s << 1, l, mid, from, to), get(s << 1 | 1, mid + 1, r, from, to));
}
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
int n; cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
build(1, 1, n);
int q; cin >> q;
for(int i = 1; i <= q; i++) {
int l, r; cin >> l >> r;
query[l].emplace_back(r, i);
}
stack<int> st;
for(int i = n; i >= 1; i--) {
while (!st.empty() && a[st.top()] <= a[i]) {
up(1, 1, n, st.top() * 2 - i, n, a[st.top()] + a[i]);
st.pop();
}
if (!st.empty()) up(1, 1, n, st.top() * 2 - i, n, a[st.top()] + a[i]);
for(auto &[r, idx] : query[i]) {
ans[idx] = get(1, 1, n, i, r);
}
st.push(i);
}
for(int i = 1; i <= q; i++) cout << ans[i] << endl;
}
Compilation message
jumps.cpp: In function 'void build(int, int, int)':
jumps.cpp:18:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
18 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'void up(int, int, int, int, int, int)':
jumps.cpp:40:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int get(int, int, int, int, int)':
jumps.cpp:50:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
50 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int main()':
jumps.cpp:58:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | freopen ("task.inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:59:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | freopen ("task.out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:62:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:63:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen (task".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12120 KB |
Output is correct |
3 |
Correct |
7 ms |
12032 KB |
Output is correct |
4 |
Correct |
7 ms |
12116 KB |
Output is correct |
5 |
Correct |
7 ms |
12096 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
8 ms |
11996 KB |
Output is correct |
8 |
Correct |
8 ms |
12116 KB |
Output is correct |
9 |
Correct |
8 ms |
12080 KB |
Output is correct |
10 |
Correct |
8 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12120 KB |
Output is correct |
3 |
Correct |
7 ms |
12032 KB |
Output is correct |
4 |
Correct |
7 ms |
12116 KB |
Output is correct |
5 |
Correct |
7 ms |
12096 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
8 ms |
11996 KB |
Output is correct |
8 |
Correct |
8 ms |
12116 KB |
Output is correct |
9 |
Correct |
8 ms |
12080 KB |
Output is correct |
10 |
Correct |
8 ms |
12116 KB |
Output is correct |
11 |
Correct |
289 ms |
31000 KB |
Output is correct |
12 |
Correct |
294 ms |
30776 KB |
Output is correct |
13 |
Correct |
300 ms |
30796 KB |
Output is correct |
14 |
Correct |
337 ms |
30816 KB |
Output is correct |
15 |
Correct |
307 ms |
30820 KB |
Output is correct |
16 |
Correct |
296 ms |
30368 KB |
Output is correct |
17 |
Correct |
282 ms |
30264 KB |
Output is correct |
18 |
Correct |
296 ms |
30152 KB |
Output is correct |
19 |
Correct |
322 ms |
30784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
20756 KB |
Output is correct |
2 |
Correct |
94 ms |
21572 KB |
Output is correct |
3 |
Correct |
116 ms |
20752 KB |
Output is correct |
4 |
Correct |
148 ms |
20780 KB |
Output is correct |
5 |
Correct |
145 ms |
20768 KB |
Output is correct |
6 |
Correct |
136 ms |
20000 KB |
Output is correct |
7 |
Correct |
141 ms |
20088 KB |
Output is correct |
8 |
Correct |
135 ms |
20012 KB |
Output is correct |
9 |
Correct |
145 ms |
20296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
11988 KB |
Output is correct |
2 |
Correct |
7 ms |
12120 KB |
Output is correct |
3 |
Correct |
7 ms |
12032 KB |
Output is correct |
4 |
Correct |
7 ms |
12116 KB |
Output is correct |
5 |
Correct |
7 ms |
12096 KB |
Output is correct |
6 |
Correct |
7 ms |
12116 KB |
Output is correct |
7 |
Correct |
8 ms |
11996 KB |
Output is correct |
8 |
Correct |
8 ms |
12116 KB |
Output is correct |
9 |
Correct |
8 ms |
12080 KB |
Output is correct |
10 |
Correct |
8 ms |
12116 KB |
Output is correct |
11 |
Correct |
289 ms |
31000 KB |
Output is correct |
12 |
Correct |
294 ms |
30776 KB |
Output is correct |
13 |
Correct |
300 ms |
30796 KB |
Output is correct |
14 |
Correct |
337 ms |
30816 KB |
Output is correct |
15 |
Correct |
307 ms |
30820 KB |
Output is correct |
16 |
Correct |
296 ms |
30368 KB |
Output is correct |
17 |
Correct |
282 ms |
30264 KB |
Output is correct |
18 |
Correct |
296 ms |
30152 KB |
Output is correct |
19 |
Correct |
322 ms |
30784 KB |
Output is correct |
20 |
Correct |
152 ms |
20756 KB |
Output is correct |
21 |
Correct |
94 ms |
21572 KB |
Output is correct |
22 |
Correct |
116 ms |
20752 KB |
Output is correct |
23 |
Correct |
148 ms |
20780 KB |
Output is correct |
24 |
Correct |
145 ms |
20768 KB |
Output is correct |
25 |
Correct |
136 ms |
20000 KB |
Output is correct |
26 |
Correct |
141 ms |
20088 KB |
Output is correct |
27 |
Correct |
135 ms |
20012 KB |
Output is correct |
28 |
Correct |
145 ms |
20296 KB |
Output is correct |
29 |
Correct |
981 ms |
54240 KB |
Output is correct |
30 |
Correct |
863 ms |
56172 KB |
Output is correct |
31 |
Correct |
824 ms |
54196 KB |
Output is correct |
32 |
Correct |
1043 ms |
54236 KB |
Output is correct |
33 |
Correct |
1045 ms |
54296 KB |
Output is correct |
34 |
Correct |
944 ms |
51848 KB |
Output is correct |
35 |
Correct |
948 ms |
51616 KB |
Output is correct |
36 |
Correct |
960 ms |
51656 KB |
Output is correct |
37 |
Correct |
944 ms |
53088 KB |
Output is correct |
38 |
Correct |
694 ms |
59928 KB |
Output is correct |
39 |
Correct |
739 ms |
59980 KB |
Output is correct |
40 |
Correct |
699 ms |
56656 KB |
Output is correct |
41 |
Correct |
763 ms |
55984 KB |
Output is correct |
42 |
Correct |
711 ms |
56032 KB |
Output is correct |
43 |
Correct |
713 ms |
57740 KB |
Output is correct |
44 |
Correct |
805 ms |
59156 KB |
Output is correct |
45 |
Correct |
796 ms |
59356 KB |
Output is correct |
46 |
Correct |
729 ms |
56084 KB |
Output is correct |
47 |
Correct |
731 ms |
55676 KB |
Output is correct |
48 |
Correct |
776 ms |
55696 KB |
Output is correct |
49 |
Correct |
765 ms |
57824 KB |
Output is correct |
50 |
Correct |
825 ms |
57404 KB |
Output is correct |
51 |
Correct |
814 ms |
57400 KB |
Output is correct |
52 |
Correct |
803 ms |
55024 KB |
Output is correct |
53 |
Correct |
869 ms |
54516 KB |
Output is correct |
54 |
Correct |
884 ms |
54516 KB |
Output is correct |
55 |
Correct |
837 ms |
56228 KB |
Output is correct |