#include "bits/stdc++.h"
#define ll long long
using namespace std;
template <typename T, typename Lambda>
struct segment_tree {
vector<T> tree;
T neutral;
Lambda join;
int size, real_size;
segment_tree() {}
template <typename L>
segment_tree(int _size, const T &_neutral, const L& f)
: join (f) {
real_size = _size;
size = nxt(_size);
tree = vector<T>(2*size+1);
neutral = _neutral;
}
int nxt(int x) {
int i = 1;
while(i < x) {
i <<= 1;
}
return i;
}
void init(const vector<T> &v) {
for(int i = 0; i < real_size; ++i) {
tree[i + size] = v[i];
}
for(int i = size - 1; i >= 0; --i) {
tree[i] = join(tree[i << 1], tree[i << 1 | 1]);
}
}
void point_update(int id, const T &val) {
// point_update(1, size, id, val, 1);
point_update_iterative(id, val);
}
void point_update_iterative(int id, const T &val) {
id += size - 1;
tree[id] = val;
id >>= 1;
while(id > 0) {
tree[id] = join(tree[id << 1], tree[id << 1 | 1]);
id >>= 1;
}
}
void point_update(int l, int r, int x, const T &val, int p) {
if(l > x || r < x) {
return;
}
if(l == r) {
tree[p] = val;
return;
}
int middle = l + r >> 1;
point_update(l, middle, x, val, p << 1);
point_update(middle+1, r, x, val, p << 1 | 1);
tree[p] = join(tree[p << 1], tree[p << 1 | 1]);
}
T get(int id) {
return query(id, id);
}
T query(int L, int R) {
return query(1, size, L, R, 1);
}
T query_iterative(int L, int R) {
L += size - 1;
R += size - 1;
T ans = neutral;
while(L <= R) {
if(L & 1) {
ans = join(ans, tree[L++]);
}
if(!(R & 1)) {
ans = join(ans, tree[R--]);
}
L >>= 1;
R >>= 1;
}
return ans;
}
T query(int l, int r, int L, int R, int p) {
if(L > r || l > R) return neutral;
if(L <= l && R >= r) return tree[p];
int middle = l + r >> 1;
T lft = query(l, middle, L, R, p << 1);
T rgh = query(middle+1, r, L, R, p << 1 | 1);
return join(lft, rgh);
}
};
const int N = 5e5+10;
ll n, q, a[N], ans[N];
vector<int> candidates[N];
vector<pair<int, int>> queries[N];
struct node {
ll a, b, c;
node() {
a = b = c = 0;
}
node(ll _a, ll _b, ll _c) : a(_a), b(_b), c(_c) {}
};
int main() {
auto join = [](const node &x, const node &y) {
node ans;
ans.a = max(x.a, y.a);
ans.b = max(x.b, y.b);
ans.c = max({x.c, y.c, x.a + y.b});
return ans;
};
cin >> n;
segment_tree<node, decltype(join)> t(n, node(), join);
for(int i = 1; i <= n; ++i) {
cin >> a[i];
node x(0, a[i], a[i]);
t.point_update(i, x);
}
stack<int> st;
for(int i = 1; i <= n; ++i) {
while(!st.empty() && a[st.top()] <= a[i]) {
candidates[st.top()].push_back(i);
st.pop();
}
if(!st.empty()) {
candidates[st.top()].push_back(i);
}
st.push(i);
}
cin >> q;
for(int i = 1; i <= q; ++i) {
int l, r; cin >> l >> r;
queries[l].push_back({r, i});
}
for(int l = n; l >= 1; --l) {
for(int i : candidates[l]) {
int id = 2*i - l;
if(id <= n) {
node x = t.get(id);
x.a = max(x.a, a[i] + a[l]);
x.c = x.a + x.b;
t.point_update(id, x);
}
}
for(pair<int, int> p : queries[l]) {
ans[p.second] = t.query(l, p.first).c;
}
}
for(int i = 1; i <= q; ++i) {
cout << ans[i] << " \n"[i == n];
}
}
Compilation message
jumps.cpp: In instantiation of 'T segment_tree<T, Lambda>::query(int, int, int, int, int) [with T = node; Lambda = main()::<lambda(const node&, const node&)>]':
jumps.cpp:72:32: required from 'T segment_tree<T, Lambda>::query(int, int) [with T = node; Lambda = main()::<lambda(const node&, const node&)>]'
jumps.cpp:154:38: required from here
jumps.cpp:95:24: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
95 | int middle = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23812 KB |
Output is correct |
3 |
Correct |
14 ms |
23756 KB |
Output is correct |
4 |
Correct |
14 ms |
23756 KB |
Output is correct |
5 |
Correct |
15 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
7 |
Correct |
14 ms |
23712 KB |
Output is correct |
8 |
Correct |
14 ms |
23780 KB |
Output is correct |
9 |
Correct |
14 ms |
23756 KB |
Output is correct |
10 |
Correct |
14 ms |
23736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23812 KB |
Output is correct |
3 |
Correct |
14 ms |
23756 KB |
Output is correct |
4 |
Correct |
14 ms |
23756 KB |
Output is correct |
5 |
Correct |
15 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
7 |
Correct |
14 ms |
23712 KB |
Output is correct |
8 |
Correct |
14 ms |
23780 KB |
Output is correct |
9 |
Correct |
14 ms |
23756 KB |
Output is correct |
10 |
Correct |
14 ms |
23736 KB |
Output is correct |
11 |
Correct |
554 ms |
40244 KB |
Output is correct |
12 |
Correct |
549 ms |
44952 KB |
Output is correct |
13 |
Correct |
538 ms |
44868 KB |
Output is correct |
14 |
Correct |
561 ms |
44912 KB |
Output is correct |
15 |
Correct |
556 ms |
44964 KB |
Output is correct |
16 |
Correct |
565 ms |
44272 KB |
Output is correct |
17 |
Correct |
546 ms |
44204 KB |
Output is correct |
18 |
Correct |
550 ms |
44304 KB |
Output is correct |
19 |
Correct |
553 ms |
44724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
289 ms |
44100 KB |
Output is correct |
2 |
Correct |
211 ms |
43932 KB |
Output is correct |
3 |
Correct |
202 ms |
44692 KB |
Output is correct |
4 |
Correct |
309 ms |
44152 KB |
Output is correct |
5 |
Correct |
289 ms |
44228 KB |
Output is correct |
6 |
Correct |
268 ms |
44044 KB |
Output is correct |
7 |
Correct |
268 ms |
44152 KB |
Output is correct |
8 |
Correct |
265 ms |
44180 KB |
Output is correct |
9 |
Correct |
277 ms |
44100 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
23756 KB |
Output is correct |
2 |
Correct |
14 ms |
23812 KB |
Output is correct |
3 |
Correct |
14 ms |
23756 KB |
Output is correct |
4 |
Correct |
14 ms |
23756 KB |
Output is correct |
5 |
Correct |
15 ms |
23756 KB |
Output is correct |
6 |
Correct |
14 ms |
23756 KB |
Output is correct |
7 |
Correct |
14 ms |
23712 KB |
Output is correct |
8 |
Correct |
14 ms |
23780 KB |
Output is correct |
9 |
Correct |
14 ms |
23756 KB |
Output is correct |
10 |
Correct |
14 ms |
23736 KB |
Output is correct |
11 |
Correct |
554 ms |
40244 KB |
Output is correct |
12 |
Correct |
549 ms |
44952 KB |
Output is correct |
13 |
Correct |
538 ms |
44868 KB |
Output is correct |
14 |
Correct |
561 ms |
44912 KB |
Output is correct |
15 |
Correct |
556 ms |
44964 KB |
Output is correct |
16 |
Correct |
565 ms |
44272 KB |
Output is correct |
17 |
Correct |
546 ms |
44204 KB |
Output is correct |
18 |
Correct |
550 ms |
44304 KB |
Output is correct |
19 |
Correct |
553 ms |
44724 KB |
Output is correct |
20 |
Correct |
289 ms |
44100 KB |
Output is correct |
21 |
Correct |
211 ms |
43932 KB |
Output is correct |
22 |
Correct |
202 ms |
44692 KB |
Output is correct |
23 |
Correct |
309 ms |
44152 KB |
Output is correct |
24 |
Correct |
289 ms |
44228 KB |
Output is correct |
25 |
Correct |
268 ms |
44044 KB |
Output is correct |
26 |
Correct |
268 ms |
44152 KB |
Output is correct |
27 |
Correct |
265 ms |
44180 KB |
Output is correct |
28 |
Correct |
277 ms |
44100 KB |
Output is correct |
29 |
Correct |
1721 ms |
98480 KB |
Output is correct |
30 |
Correct |
1489 ms |
97652 KB |
Output is correct |
31 |
Correct |
1487 ms |
99780 KB |
Output is correct |
32 |
Correct |
1758 ms |
98232 KB |
Output is correct |
33 |
Correct |
1742 ms |
98264 KB |
Output is correct |
34 |
Correct |
1712 ms |
96052 KB |
Output is correct |
35 |
Correct |
1688 ms |
95672 KB |
Output is correct |
36 |
Correct |
1754 ms |
95672 KB |
Output is correct |
37 |
Correct |
1916 ms |
97188 KB |
Output is correct |
38 |
Correct |
1454 ms |
103956 KB |
Output is correct |
39 |
Correct |
1409 ms |
103976 KB |
Output is correct |
40 |
Correct |
1352 ms |
100700 KB |
Output is correct |
41 |
Correct |
1329 ms |
100144 KB |
Output is correct |
42 |
Correct |
1340 ms |
100176 KB |
Output is correct |
43 |
Correct |
1372 ms |
101968 KB |
Output is correct |
44 |
Correct |
1465 ms |
103540 KB |
Output is correct |
45 |
Correct |
1462 ms |
103640 KB |
Output is correct |
46 |
Correct |
1375 ms |
100188 KB |
Output is correct |
47 |
Correct |
1394 ms |
99820 KB |
Output is correct |
48 |
Correct |
1395 ms |
99872 KB |
Output is correct |
49 |
Correct |
1411 ms |
101884 KB |
Output is correct |
50 |
Correct |
1503 ms |
101508 KB |
Output is correct |
51 |
Correct |
1506 ms |
101484 KB |
Output is correct |
52 |
Correct |
1476 ms |
98996 KB |
Output is correct |
53 |
Correct |
1493 ms |
98712 KB |
Output is correct |
54 |
Correct |
1451 ms |
98644 KB |
Output is correct |
55 |
Correct |
1516 ms |
100356 KB |
Output is correct |