This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 500'000 + 10;
int n;
int a[N];
int q;
vector<pair<int, int>> Q[N];
int f[N << 2], T[N << 2], lazy[N << 2];
void build(int s, int l, int r) {
if (l == r) {
T[s] = f[s] = a[l];
return;
}
int mid = l + r >> 1;
build(s << 1, l, mid); build(s << 1 | 1, mid + 1, r);
T[s] = max(T[s << 1], T[s << 1 | 1]);
f[s] = max(f[s << 1], f[s << 1 | 1]);
}
void push(int s) {
f[s << 1] = max(f[s << 1], T[s << 1] + lazy[s]);
f[s << 1 | 1] = max(f[s << 1 | 1], T[s << 1 | 1] + lazy[s]);
lazy[s << 1] = max(lazy[s << 1], lazy[s]);
lazy[s << 1 | 1] = max(lazy[s << 1 | 1], lazy[s]);
}
void update(int s, int l, int r, int u, int v, int x) {
if (v < l || u > r) return;
if (u <= l && r <= v) {
lazy[s] = max(lazy[s], x);
f[s] = max(f[s], T[s] + x);
return;
}
push(s);
int mid = l + r >> 1;
update(s << 1, l, mid, u, v, x); update(s << 1 | 1, mid + 1, r, u, v, x);
f[s] = max(f[s << 1], f[s << 1 | 1]);
}
int query(int s, int l, int r, int u, int v) {
if (v < l || u > r) return 0;
if (u <= l && r <= v) return f[s];
push(s);
int mid = l + r >> 1;
return max(query(s << 1, l, mid, u, v), query(s << 1 | 1, mid + 1, r, u, v));
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
cin >> q;
for (int i = 1; i <= q; ++i) {
int l, r; cin >> l >> r;
Q[l].push_back({r, i});
}
vector<int>answer(q + 1);
build(1, 1, n);
stack<int> st;
for (int i = n; i >= 1; --i) {
while (st.size() && a[st.top()] <= a[i]) {
update(1, 1, n, st.top() * 2 - i, n, a[st.top()] + a[i]);
st.pop();
}
if (st.size()) update(1, 1, n, st.top() * 2 - i, n, a[st.top()] + a[i]);
for (const auto& [r, idx] : Q[i]) answer[idx] = query(1, 1, n, i, r);
st.push(i);
}
for (int i = 1; i <= q; ++i) cout << answer[i] << "\n";
}
Compilation message (stderr)
jumps.cpp: In function 'void build(int, int, int)':
jumps.cpp:17:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
17 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'void update(int, int, int, int, int, int)':
jumps.cpp:38:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid = l + r >> 1;
| ~~^~~
jumps.cpp: In function 'int query(int, int, int, int, int)':
jumps.cpp:46:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | int mid = l + r >> 1;
| ~~^~~
# | 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... |