// Author: caption_mingle
#include "bits/stdc++.h"
using namespace std;
#define ln "\n"
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define ll long long
const int mod = 1e9 + 7;
const int inf = 2e9;
const int N = 5e5 + 7;
int n, q, a[N], ans[N];
vector<pair<int, int>> ev[N];
struct SEGTREE {
int n;
vector<int> st, lazy, ori;
SEGTREE(int n) : n(n) {
st.resize(n * 4 + 4, 0);
lazy.resize(n * 4 + 4, 0);
ori.resize(n * 4 + 4, 0);
}
void down(int i) {
if(lazy[i] == 0) return;
int x = lazy[i];
lazy[i] = 0;
st[i * 2] = max(st[i * 2], ori[i * 2] + x);
st[i * 2 + 1] = max(st[i * 2 + 1], ori[i * 2 + 1] + x);
lazy[i * 2] = max(lazy[i * 2], x);
lazy[i * 2 + 1] = max(lazy[i * 2 + 1], x);
}
void init(int i, int l, int r) {
if(l == r) {
ori[i] = a[l];
return;
}
int m = (l + r) >> 1;
init(i * 2, l, m);
init(i * 2 + 1, m + 1, r);
ori[i] = max(ori[i * 2], ori[i * 2 + 1]);
}
void update(int i, int l, int r, int u, int v, int x) {
if(l > v or r < u) return;
if(u <= l and r <= v) {
st[i] = max(st[i], ori[i] + x);
lazy[i] = max(lazy[i], x);
return;
}
down(i);
int m = (l + r) >> 1;
update(i * 2, l, m, u, v, x);
update(i * 2 + 1, m + 1, r, u, v, x);
st[i] = max(st[i * 2], st[i * 2 + 1]);
}
int get(int i, int l, int r, int u, int v) {
if(l > v or r < u) return 0;
if(u <= l and r <= v) return st[i];
down(i);
int m = (l + r) >> 1;
return max(get(i * 2, l, m, u, v), get(i * 2 + 1, m + 1, r, u, v));
}
};
signed main() {
cin.tie(0) -> sync_with_stdio(0);
#define task ""
if(fopen(task ".INP", "r")) {
freopen(task ".INP", "r", stdin);
freopen(task ".OUT", "w", stdout);
}
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;
ev[l].pb({i, r});
}
SEGTREE st(n);
st.init(1, 1, n);
vector<int> stt;
for(int i = n; i > 0; i--) {
while(sz(stt) and a[stt.back()] < a[i]) {
int j = stt.back();
stt.pop_back();
int l = 2 * j - i;
st.update(1, 1, n, l, n, a[i] + a[j]);
}
if(sz(stt)) {
int j = stt.back();
int l = 2 * j - i;
st.update(1, 1, n, l, n, a[i] + a[j]);
}
stt.pb(i);
for(auto [id, r] : ev[i]) {
ans[id] = st.get(1, 1, n, i, r);
}
}
for(int i = 1; i <= q; i++) cout << ans[i] << ln;
cerr << "\nTime: " << clock() * 1000 / CLOCKS_PER_SEC;
}
Compilation message (stderr)
jumps.cpp: In function 'int main()':
jumps.cpp:73:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(task ".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:74:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | freopen(task ".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |