이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int kN = 5e5;
int a[1 + kN], s1[1 + kN], s2[1 + kN], dr1[1 + kN], dr2[1 + kN], sol[kN];
vector<pair<int, int>> q[1 + kN];
void maxSelf(int &x, int y) {
if (x < y) {
x = y;
}
}
struct ST {
int n;
vector<int> maxSum, tmax, best;
ST(int N) : n(N) {
int dim = 1;
while (dim < n) {
dim *= 2;
}
dim *= 2;
maxSum.resize(dim, -INF);
tmax.resize(dim);
best.resize(dim, -INF);
}
void build(int x, int lx, int rx) {
if (lx == rx) {
tmax[x] = a[lx];
return;
}
int mid = (lx + rx) / 2;
build(x * 2, lx, mid);
build(x * 2 + 1, mid + 1, rx);
tmax[x] = max(tmax[x * 2], tmax[x * 2 + 1]);
}
void push(int x, int lx, int rx) {
if (lx == rx) {
return;
}
for (int i = 0; i < 2; ++i) {
maxSelf(maxSum[x * 2 + i], maxSum[x]);
maxSelf(best[x * 2 + i], maxSum[x * 2 + i] + tmax[x * 2 + i]);
}
maxSelf(best[x], max(best[x * 2], best[x * 2 + 1]));
}
void update(int x, int lx, int rx, int st, int dr, int sum) {
if (st <= lx && rx <= dr) {
maxSelf(maxSum[x], sum);
maxSelf(best[x], maxSum[x] + tmax[x]);
push(x, lx, rx);
return;
}
push(x, lx, rx);
int mid = (lx + rx) / 2;
if (st <= mid) {
update(x * 2, lx, mid, st, dr, sum);
}
if (mid < dr) {
update(x * 2 + 1, mid + 1, rx, st, dr, sum);
}
maxSelf(best[x], max(best[x * 2], best[x * 2 + 1]));
}
void update(int st, int dr, int sum) {
if (dr < st) {
return;
}
update(1, 1, n, st, dr, sum);
}
int query(int x, int lx, int rx, int st, int dr) {
push(x, lx, rx);
if (st <= lx && rx <= dr) {
return best[x];
}
int mid = (lx + rx) / 2, ans = 0;
if (st <= mid) {
maxSelf(ans, query(x * 2, lx, mid, st, dr));
}
if (mid < dr) {
maxSelf(ans, query(x * 2 + 1, mid + 1, rx, st, dr));
}
return ans;
}
int query(int st, int dr) {
return query(1, 1, n, st, dr);
}
};
void testCase() {
int n;
cin >> n;
int vf1 = 0, vf2 = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
while (vf1 && a[s1[vf1]] <= a[i]) {
dr1[s1[vf1]] = i;
vf1 -= 1;
}
while (vf2 && a[s2[vf2]] < a[i]) {
dr2[s2[vf2]] = i;
vf2 -= 1;
}
s1[++vf1] = i;
s2[++vf2] = i;
dr1[i] = dr2[i] = n + 1;
}
ST t(n);
t.build(1, 1, n);
int m;
cin >> m;
for (int i = 0; i < m; ++i) {
int l, r;
cin >> l >> r;
q[l].emplace_back(r, i);
}
for (int i = n - 2; i > 0; --i) {
int j = i + 1;
while (j <= n && j <= dr1[i]) {
t.update(2 * j - i, n, a[i] + a[j]);
j = dr2[j];
}
for (auto it : q[i]) {
int k, index;
tie(k, index) = it;
sol[index] = t.query(i + 2, k);
}
}
for (int i = 0; i < m; ++i) {
cout << sol[i] << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |