#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class SegmentTree {
public:
SegmentTree(const vector<int> &A): m_size(A.size()), m_data(m_size * 4) {
build(1, 1, m_size, A);
}
void update(int from, int to, int add) {
update(1, 1, m_size, from, to, add);
}
int query(int from, int to) {
return query(1, 1, m_size, from, to, 0);
}
private:
struct Node {
int max = 0;
int maxadd = 0;
int best = 0;
};
void build(int node, int begin, int end, const vector<int> &A) {
if (end - begin == 1) {
m_data[node] = Node{A[begin], 0, A[begin]};
return;
}
int mid = (begin + end) / 2;
build(node * 2, begin, mid, A);
build(node * 2 + 1, mid, end, A);
m_data[node] = Node{max(m_data[node * 2].max, m_data[node * 2 + 1].max), 0, max(m_data[node * 2].best, m_data[node * 2 + 1].best)};
}
void update(int node, int begin, int end, int from, int to, int add) {
if (from <= begin && end <= to) {
m_data[node].maxadd = max(m_data[node].maxadd, add);
m_data[node].best = max(m_data[node].best, m_data[node].maxadd + m_data[node].max);
return;
}
int mid = (begin + end) / 2;
if (from < mid)
update(node * 2, begin, mid, from, to, add);
if (mid < to)
update(node * 2 + 1, mid, end, from, to, add);
m_data[node].best = max({m_data[node].best, m_data[node * 2].best, m_data[node * 2 + 1].best});
}
int query(int node, int begin, int end, int from, int to, int maxadd) {
maxadd = max(maxadd, m_data[node].maxadd);
if (from <= begin && end <= to)
return max(m_data[node].best, m_data[node].max + maxadd);
int mid = (begin + end) / 2;
int answer = 0;
if (from < mid)
answer = max(answer, query(node * 2, begin, mid, from, to, maxadd));
if (mid < to)
answer = max(answer, query(node * 2 + 1, mid, end, from, to, maxadd));
return answer;
}
int m_size;
vector<Node> m_data;
};
int main() {
ios::sync_with_stdio(false);
int N; cin >> N;
vector<int> A(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
vector< vector<int> > mids(N);
vector<int> stack;
for (int i = 0; i < N; ++i) {
while (!stack.empty() && A[stack.back()] <= A[i]) {
mids[stack.back()].push_back(i);
stack.pop_back();
}
if (!stack.empty())
mids[stack.back()].push_back(i);
stack.push_back(i);
}
int Q; cin >> Q;
vector< vector< pair<int, int> > > queries(N);
for (int i = 0; i < Q; ++i) {
int l, r; cin >> l >> r;
queries[l - 1].emplace_back(r, i);
}
SegmentTree S(A);
vector<int> answer(Q, 0);
for (int i = N - 1; i >= 0; --i) {
for (auto &mid : mids[i]) {
int from = 2 * mid - i;
if (from < N)
S.update(from, N, A[i] + A[mid]);
}
for (auto &[r, index] : queries[i])
answer[index] = S.query(i, r);
}
for (int i = 0; i < Q; ++i)
cout << answer[i] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
281 ms |
19064 KB |
Output is correct |
12 |
Correct |
273 ms |
19160 KB |
Output is correct |
13 |
Correct |
273 ms |
19064 KB |
Output is correct |
14 |
Correct |
274 ms |
19064 KB |
Output is correct |
15 |
Correct |
277 ms |
19192 KB |
Output is correct |
16 |
Correct |
276 ms |
18424 KB |
Output is correct |
17 |
Correct |
272 ms |
18424 KB |
Output is correct |
18 |
Correct |
278 ms |
18424 KB |
Output is correct |
19 |
Correct |
278 ms |
18936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
156 ms |
28152 KB |
Output is correct |
2 |
Correct |
96 ms |
27896 KB |
Output is correct |
3 |
Correct |
106 ms |
28652 KB |
Output is correct |
4 |
Correct |
160 ms |
28152 KB |
Output is correct |
5 |
Correct |
157 ms |
28152 KB |
Output is correct |
6 |
Correct |
152 ms |
27512 KB |
Output is correct |
7 |
Correct |
152 ms |
27512 KB |
Output is correct |
8 |
Correct |
153 ms |
27384 KB |
Output is correct |
9 |
Correct |
157 ms |
27896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
281 ms |
19064 KB |
Output is correct |
12 |
Correct |
273 ms |
19160 KB |
Output is correct |
13 |
Correct |
273 ms |
19064 KB |
Output is correct |
14 |
Correct |
274 ms |
19064 KB |
Output is correct |
15 |
Correct |
277 ms |
19192 KB |
Output is correct |
16 |
Correct |
276 ms |
18424 KB |
Output is correct |
17 |
Correct |
272 ms |
18424 KB |
Output is correct |
18 |
Correct |
278 ms |
18424 KB |
Output is correct |
19 |
Correct |
278 ms |
18936 KB |
Output is correct |
20 |
Correct |
156 ms |
28152 KB |
Output is correct |
21 |
Correct |
96 ms |
27896 KB |
Output is correct |
22 |
Correct |
106 ms |
28652 KB |
Output is correct |
23 |
Correct |
160 ms |
28152 KB |
Output is correct |
24 |
Correct |
157 ms |
28152 KB |
Output is correct |
25 |
Correct |
152 ms |
27512 KB |
Output is correct |
26 |
Correct |
152 ms |
27512 KB |
Output is correct |
27 |
Correct |
153 ms |
27384 KB |
Output is correct |
28 |
Correct |
157 ms |
27896 KB |
Output is correct |
29 |
Correct |
1085 ms |
93588 KB |
Output is correct |
30 |
Correct |
922 ms |
92920 KB |
Output is correct |
31 |
Correct |
909 ms |
94696 KB |
Output is correct |
32 |
Correct |
1077 ms |
93560 KB |
Output is correct |
33 |
Correct |
1073 ms |
93432 KB |
Output is correct |
34 |
Correct |
1050 ms |
91184 KB |
Output is correct |
35 |
Correct |
1070 ms |
90872 KB |
Output is correct |
36 |
Correct |
1063 ms |
90744 KB |
Output is correct |
37 |
Correct |
1058 ms |
92280 KB |
Output is correct |
38 |
Correct |
755 ms |
99192 KB |
Output is correct |
39 |
Correct |
770 ms |
99064 KB |
Output is correct |
40 |
Correct |
738 ms |
95864 KB |
Output is correct |
41 |
Correct |
736 ms |
95352 KB |
Output is correct |
42 |
Correct |
736 ms |
95352 KB |
Output is correct |
43 |
Correct |
740 ms |
97016 KB |
Output is correct |
44 |
Correct |
814 ms |
98552 KB |
Output is correct |
45 |
Correct |
809 ms |
98680 KB |
Output is correct |
46 |
Correct |
787 ms |
95392 KB |
Output is correct |
47 |
Correct |
785 ms |
94968 KB |
Output is correct |
48 |
Correct |
786 ms |
94968 KB |
Output is correct |
49 |
Correct |
790 ms |
97016 KB |
Output is correct |
50 |
Correct |
883 ms |
96632 KB |
Output is correct |
51 |
Correct |
888 ms |
96636 KB |
Output is correct |
52 |
Correct |
864 ms |
94200 KB |
Output is correct |
53 |
Correct |
861 ms |
93816 KB |
Output is correct |
54 |
Correct |
864 ms |
93944 KB |
Output is correct |
55 |
Correct |
870 ms |
95480 KB |
Output is correct |