#include <bits/stdc++.h>
using namespace std;
constexpr int inf = 1e9 + 7;
vector<vector<int>> mst;
vector<int> a;
bool cmp(int i, int j) {
return a[i] > a[j] ? i : j;
}
int sz = 1;
void init(vector<int> b) {
sz = 1;
while (sz < b.size()) sz <<= 1;
mst.resize(sz * 2);
for (int i = 0; i < int(b.size()); ++i) {
mst[i + sz] = {i};
}
for (int i = sz - 1; i > 0; --i) {
mst[i].resize(mst[i << 1].size() + mst[i << 1 | 1].size());
merge(mst[i << 1].begin(), mst[i << 1].end(), mst[i << 1 | 1].begin(), mst[i << 1 | 1].end(), mst[i].begin(), cmp);
}
}
vector<int> d;
void getSorted(int l, int r, int cnt, int x = 1, int lx = 0, int rx = sz) {
if (l >= rx || lx >= r) return;
if (l <= lx && rx <= r) {
d.insert(d.end(), mst[x].begin(), mst[x].begin() + min(cnt, int(mst[x].size())));
return;
}
int mid = (lx + rx) >> 1;
getSorted(l, r, cnt, x << 1, lx, mid);
getSorted(l, r, cnt, x << 1 | 1, mid, rx);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
a.resize(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
auto func = [&](int i, int j) {
return a[i] > a[j] ? i : j;
};
const int max_log = __lg(n) + bool(n & (n - 1));
vector<vector<int>> st(max_log);
st[0].resize(n);
iota(st[0].begin(), st[0].end(), 0);
for (int i = 1; i < max_log; ++i) {
st[i].resize(n - (1 << i) + 1);
for (int j = 0; j < (int) st[i].size(); ++j) {
st[i][j] = func(st[i - 1][j], st[i - 1][j + (1 << (i - 1))]);
}
}
init(a);
vector<int> logn(n + 1);
for (int i = 2; i <= n; ++i) {
logn[i] = logn[i >> 1] + 1;
}
auto query = [&](int l, int r) {
int lg = logn[r - l];
return func(st[lg][l], st[lg][r - (1 << lg)]);
};
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l;
vector<int> p;
d.clear();
getSorted(l, r, 30);
sort(d.begin(), d.end(), cmp);
d.resize(min(int(d.size()), 30));
swap(p, d);
int ans = 0;
auto relax = [&](int i, int j) {
int R = j + (j - i);
if (R < r) {
ans = max(ans, a[i] + a[j] + a[query(R, r)]);
}
int len = j - i - 1;
if (len > 0) {
ans = max(ans, a[i] + a[j] + a[query(i + 1, i + 1 + ((len + 1) / 2))]);
}
int L = max(l, i - (j - i));
if (L < i) {
ans = max(ans, a[i] + a[j] + a[query(L, i)]);
}
};
sort(p.begin(), p.end());
vector<int> stk;
for (int i: p) {
while (!stk.empty() && a[stk.back()] <= a[i]) {
relax(stk.back(), i);
stk.pop_back();
}
if (!stk.empty()) {
relax(stk.back(), i);
}
stk.push_back(i);
}
cout << ans << '\n';
}
return 0;
}
Compilation message
jumps.cpp: In function 'void init(std::vector<int>)':
jumps.cpp:19:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | while (sz < b.size()) sz <<= 1;
| ~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
104 ms |
104444 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |