#include <bits/stdc++.h>
using namespace std;
using lli = int64_t;
#define INFILE "tjump.inp"
#define OUTFILE "tjump.out"
int maximise(int& x, int y) {
if (x < y) return x = y, true;
return false;
}
namespace SUBTASK_1 {
const int MAX_N = 5000;
int value[MAX_N + 10];
int dp[MAX_N + 10][MAX_N + 10];
int max_value[MAX_N + 10];
void main(int N) {
for (int i = 1; i <= N; ++i) {
cin >> value[i];
}
for (int i = N; i >= 1; --i) {
for (int j = i + 1; j <= N; ++j) {
max_value[j] = max(max_value[j - 1], value[j]);
if (j - i >= 2) {
dp[i][j] = value[i] + value[j] + max_value[i + (j - i) / 2];
}
maximise(dp[i][j], max(dp[i][j - 1], dp[i + 1][j]));
}
}
int Q; cin >> Q;
for (int qu = 1; qu <= Q; ++qu) {
int l, r; cin >> l >> r;
cout << dp[l][r] << '\n';
}
}
}
const int MAX_N = (int) 5e5;
int value[MAX_N + 10];
int debugging = false;
struct SegmentTree {
vector <int> max_value, max_lazy, max_all;
int N;
void build(int v, int tl, int tr) {
if (tl == tr) {
max_value[v] = value[tl];
return;
}
int tm = (tl + tr) >> 1;
build(v << 1, tl, tm);
build(v << 1 | 1, tm + 1, tr);
max_value[v] = max(max_value[v << 1], max_value[v << 1 | 1]);
}
SegmentTree(int N): N(N), max_value(N << 2), max_lazy(N << 2), max_all(N << 2) {
build(1, 1, N);
}
void Apply(int v, int delta) {
maximise(max_lazy[v], delta);
maximise(max_all[v], max_lazy[v] + max_value[v]);
}
void Push(int v) {
for (int child : {v << 1, v << 1 | 1}) {
Apply(child, max_lazy[v]);
}
}
void update_range(int v, int tl, int tr, int l, int r, int delta) {
if (l > r) return;
if (l == tl and r == tr) {
Apply(v, delta);
return;
}
Push(v);
int tm = (tl + tr) >> 1;
update_range(v << 1, tl, tm, l, min(r, tm), delta);
update_range(v << 1 | 1, tm + 1, tr, max(l, tm + 1), r, delta);
//max_lazy[v] = max(max_lazy[v << 1], max_lazy[v << 1 | 1]);
max_all[v] = max(max_all[v << 1], max_all[v << 1 | 1]);
}
void update_range(int l, int r, int delta) {
update_range(1, 1, N, l, r, delta);
}
int get_range(int v, int tl, int tr, int l, int r) {
if (l > r) return 0;
if (l == tl and r == tr) return max_all[v];
Push(v);
int tm = (tl + tr) >> 1;
return max(
get_range(v << 1, tl, tm, l, min(r, tm)),
get_range(v << 1 | 1, tm + 1, tr, max(l, tm + 1), r)
);
}
int get_range(int l, int r) {
return get_range(1, 1, N, l, r);
}
};
int main() {
ios::sync_with_stdio(0); cin.tie(0);
if (fopen(INFILE, "r")) {
freopen(INFILE, "r", stdin);
freopen(OUTFILE, "w", stdout);
}
int N; cin >> N;
// if (N <= 5000) {
// SUBTASK_1::main(N);
// return 0;
// }
for (int i = 1; i <= N; ++i) cin >> value[i];
SegmentTree tree(N);
vector <vector <int>> query_line(N + 1);
vector <int> stack;
int Q; cin >> Q;
vector <int> start(Q), dest(Q);
for (int qu = 0; qu < Q; ++qu) {
cin >> start[qu] >> dest[qu];
query_line[start[qu]].push_back(qu);
}
vector <int> finale(Q);
for (int i = N; i >= 1; --i) {
while (not stack.empty()) {
int j = stack.back();
// debugging = i == 11 and j == 12;
tree.update_range(2 * j - i, N, value[i] + value[j]);
if (value[i] < value[j]) break;
stack.pop_back();
}
stack.push_back(i);
for (int id : query_line[i]) {
finale[id] = tree.get_range(i + 2, dest[id]);
}
}
for (int qu = 0; qu < Q; ++qu) cout << finale[qu] << '\n';
return 0;
}
Compilation message
jumps.cpp: In constructor 'SegmentTree::SegmentTree(int)':
jumps.cpp:50:9: warning: 'SegmentTree::N' will be initialized after [-Wreorder]
50 | int N;
| ^
jumps.cpp:49:18: warning: 'std::vector<int> SegmentTree::max_value' [-Wreorder]
49 | vector <int> max_value, max_lazy, max_all;
| ^~~~~~~~~
jumps.cpp:63:5: warning: when initialized here [-Wreorder]
63 | SegmentTree(int N): N(N), max_value(N << 2), max_lazy(N << 2), max_all(N << 2) {
| ^~~~~~~~~~~
jumps.cpp: In function 'int main()':
jumps.cpp:117:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | freopen(INFILE, "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
jumps.cpp:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | freopen(OUTFILE, "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
201 ms |
19784 KB |
Output is correct |
12 |
Correct |
198 ms |
18760 KB |
Output is correct |
13 |
Correct |
204 ms |
19272 KB |
Output is correct |
14 |
Correct |
193 ms |
19636 KB |
Output is correct |
15 |
Correct |
202 ms |
19020 KB |
Output is correct |
16 |
Correct |
191 ms |
18504 KB |
Output is correct |
17 |
Correct |
189 ms |
19016 KB |
Output is correct |
18 |
Correct |
195 ms |
19160 KB |
Output is correct |
19 |
Correct |
190 ms |
19700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
113 ms |
17736 KB |
Output is correct |
2 |
Correct |
67 ms |
19428 KB |
Output is correct |
3 |
Correct |
64 ms |
18436 KB |
Output is correct |
4 |
Correct |
112 ms |
18524 KB |
Output is correct |
5 |
Correct |
113 ms |
18504 KB |
Output is correct |
6 |
Correct |
109 ms |
17744 KB |
Output is correct |
7 |
Correct |
109 ms |
17736 KB |
Output is correct |
8 |
Correct |
111 ms |
17744 KB |
Output is correct |
9 |
Correct |
112 ms |
18000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
201 ms |
19784 KB |
Output is correct |
12 |
Correct |
198 ms |
18760 KB |
Output is correct |
13 |
Correct |
204 ms |
19272 KB |
Output is correct |
14 |
Correct |
193 ms |
19636 KB |
Output is correct |
15 |
Correct |
202 ms |
19020 KB |
Output is correct |
16 |
Correct |
191 ms |
18504 KB |
Output is correct |
17 |
Correct |
189 ms |
19016 KB |
Output is correct |
18 |
Correct |
195 ms |
19160 KB |
Output is correct |
19 |
Correct |
190 ms |
19700 KB |
Output is correct |
20 |
Correct |
113 ms |
17736 KB |
Output is correct |
21 |
Correct |
67 ms |
19428 KB |
Output is correct |
22 |
Correct |
64 ms |
18436 KB |
Output is correct |
23 |
Correct |
112 ms |
18524 KB |
Output is correct |
24 |
Correct |
113 ms |
18504 KB |
Output is correct |
25 |
Correct |
109 ms |
17744 KB |
Output is correct |
26 |
Correct |
109 ms |
17736 KB |
Output is correct |
27 |
Correct |
111 ms |
17744 KB |
Output is correct |
28 |
Correct |
112 ms |
18000 KB |
Output is correct |
29 |
Correct |
853 ms |
68584 KB |
Output is correct |
30 |
Correct |
683 ms |
70848 KB |
Output is correct |
31 |
Correct |
624 ms |
68592 KB |
Output is correct |
32 |
Correct |
844 ms |
68580 KB |
Output is correct |
33 |
Correct |
779 ms |
68584 KB |
Output is correct |
34 |
Correct |
796 ms |
66272 KB |
Output is correct |
35 |
Correct |
716 ms |
65928 KB |
Output is correct |
36 |
Correct |
679 ms |
66120 KB |
Output is correct |
37 |
Correct |
736 ms |
67400 KB |
Output is correct |
38 |
Correct |
564 ms |
75200 KB |
Output is correct |
39 |
Correct |
565 ms |
75316 KB |
Output is correct |
40 |
Correct |
575 ms |
72008 KB |
Output is correct |
41 |
Correct |
552 ms |
71528 KB |
Output is correct |
42 |
Correct |
532 ms |
71516 KB |
Output is correct |
43 |
Correct |
554 ms |
73120 KB |
Output is correct |
44 |
Correct |
567 ms |
74568 KB |
Output is correct |
45 |
Correct |
568 ms |
74568 KB |
Output is correct |
46 |
Correct |
545 ms |
71432 KB |
Output is correct |
47 |
Correct |
545 ms |
71132 KB |
Output is correct |
48 |
Correct |
612 ms |
71120 KB |
Output is correct |
49 |
Correct |
610 ms |
73032 KB |
Output is correct |
50 |
Correct |
688 ms |
72336 KB |
Output is correct |
51 |
Correct |
713 ms |
72340 KB |
Output is correct |
52 |
Correct |
679 ms |
69960 KB |
Output is correct |
53 |
Correct |
646 ms |
69448 KB |
Output is correct |
54 |
Correct |
712 ms |
69448 KB |
Output is correct |
55 |
Correct |
630 ms |
71240 KB |
Output is correct |