#include <bits/stdc++.h>
using namespace std;
template <class T>
struct RMQ {
vector<vector<T>> rmq;
void Build(const vector<T>& V) {
int n = V.size(), on = 1, depth = 1;
while (on < n) on *= 2, ++depth;
rmq.assign(depth, V);
for (int i = 0; i < depth - 1; ++i)
for (int j = 0; j < n; ++j) {
rmq[i + 1][j] = max(rmq[i][j],
rmq[i][min(n - 1, j + (1 << i))]);
}
}
T Query(int a, int b) {
if (b <= a) return 0;
int dep = 31 - __builtin_clz(b - a); // log(b - a)
return max(rmq[dep][a], rmq[dep][b - (1 << dep)]);
}
};
using PQ = priority_queue<pair<int, int>>;
RMQ<int> rmq;
vector<vector<pair<int, int>>> queries;
vector<int> answer;
vector<PQ> pqs;
vector<int> v;
void Add(PQ& a, pair<int, int> x) {
a.push(x);
//while (a.size() > 20)
// a.pop();
}
void Merge(PQ& a, PQ b) {
while (!b.empty()) {
auto top = b.top();
Add(a, top);
b.pop();
}
}
int Solve(int l, int r, PQ& pq) {
vector<int> vals;
vals.reserve(pq.size());
while (pq.size()) {
vals.push_back(pq.top().second);
pq.pop();
}
int best = 0;
for (auto a : vals) {
for (auto b : vals) {
if (a >= b) continue;
int delta = b - a;
int now = 0;
now = max(now, rmq.Query(a + 1, a + delta / 2 + 1));
now = max(now, rmq.Query(max(l, a - delta), a));
now = max(now, rmq.Query(b + delta, r + 1));
best = max(best, now + v[a] + v[b]);
}
}
return best;
}
void Divide(int b, int e) {
if (b == e) {
return;
}
int m = (b + e) / 2;
Divide(b, m); Divide(m + 1, e);
PQ now;
for (int i = m + 1; i <= e; ++i) {
Add(now, make_pair(-v[i], i));
pqs[i] = now;
}
now = PQ();
for (int i = m; i >= b; --i) {
Add(now, make_pair(-v[i], i));
while (queries[i].size()) {
int j, idx; tie(j, idx) = queries[i].back();
if (j > e) break;
queries[i].pop_back();
PQ now2 = now;
Merge(now2, pqs[j]);
answer[idx] = Solve(i, j, now2);
}
}
}
int main() {
int n; cin >> n;
v.resize(n);
for (int i = 0; i < n; ++i)
cin >> v[i];
rmq.Build(v);
int q; cin >> q;
queries.assign(n, {});
answer.assign(q, -1);
pqs.assign(n, PQ());
for (int i = 0; i < q; ++i) {
int l, r; cin >> l >> r; --l; --r;
queries[l].emplace_back(r, i);
}
for (int i = 0; i < n; ++i) {
sort(queries[i].rbegin(), queries[i].rend());
}
Divide(0, n - 1);
for (int i = 0; i < q; ++i)
cout << answer[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
4 ms |
504 KB |
Output is correct |
4 |
Correct |
5 ms |
364 KB |
Output is correct |
5 |
Correct |
4 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
424 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
4 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
4 ms |
504 KB |
Output is correct |
4 |
Correct |
5 ms |
364 KB |
Output is correct |
5 |
Correct |
4 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
424 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
4 ms |
376 KB |
Output is correct |
11 |
Execution timed out |
4014 ms |
14176 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
676 ms |
524292 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
4 ms |
504 KB |
Output is correct |
4 |
Correct |
5 ms |
364 KB |
Output is correct |
5 |
Correct |
4 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
424 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
4 ms |
376 KB |
Output is correct |
11 |
Execution timed out |
4014 ms |
14176 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |