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>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
string _reset = "\u001b[0m", _yellow = "\u001b[33m", _bold = "\u001b[1m";
void DBG() { cerr << "]" << _reset << endl; }
template<class H, class...T> void DBG(H h, T ...t) {
cerr << to_string(h);
if(sizeof ...(t)) cerr << ", ";
DBG(t...);
}
#ifdef CHEISSMART
#define debug(...) cerr << _yellow << _bold << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define debug(...)
#endif
const int INF = 1e9 + 7, N = 5e5 + 7;
const ll oo = 1e18;
int a[N], l[N], r[N];
int n;
ll ans[N];
template<class T> void cmax(T& a, T b) {
a = max(a, b);
}
struct node {
ll mxo, ans, lz;
node(ll val = 0) : mxo(val), ans(val), lz(-oo) {}
} t[N * 4];
void apply(int v, ll val) {
cmax(t[v].lz, val);
cmax(t[v].ans, t[v].mxo + val);
}
void push(int v) {
apply(v * 2, t[v].lz);
apply(v * 2 + 1, t[v].lz);
}
void pull(int v) {
t[v].ans = max(t[v * 2].ans, t[v * 2 + 1].ans);
t[v].mxo = max(t[v * 2].mxo, t[v * 2 + 1].mxo);
}
void build(int v = 1, int tl = 0, int tr = n) {
if(tr - tl == 1) {
t[v] = node(ll(a[tl]));
return;
}
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm, tr);
pull(v);
}
void upd(int l, int r, ll val, int v = 1, int tl = 0, int tr = n) {
if(l <= tl && tr <= r)
return apply(v, val), void(0);
push(v);
int tm = (tl + tr) / 2;
if(l < tm)
upd(l, r, val, v * 2, tl, tm);
if(r > tm)
upd(l, r, val, v * 2 + 1, tm, tr);
pull(v);
}
ll qry(int l, int r, int v = 1, int tl = 0, int tr = n) {
if(l <= tl && tr <= r)
return t[v].ans;
push(v);
int tm = (tl + tr) / 2;
ll res = 0;
if(l < tm)
cmax(res, qry(l, r, v * 2, tl, tm));
if(r > tm)
cmax(res, qry(l, r, v * 2 + 1, tm, tr));
return res;
}
signed main()
{
IO_OP;
cin >> n;
for(int i = 0; i < n; i++)
cin >> a[i];
build();
V<pi> ev;
for(int i = 0; i < n; i++) {
l[i] = i - 1;
while(l[i] != -1 && a[l[i]] < a[i])
l[i] = l[l[i]];
if(l[i] != -1)
ev.EB(l[i], i);
}
for(int i = n - 1; i >= 0; i--) {
r[i] = i + 1;
while(r[i] != n && a[r[i]] <= a[i])
r[i] = r[r[i]];
if(r[i] != n)
ev.EB(i, r[i]);
}
sort(ALL(ev));
V<array<int, 3>> qq;
int q;
cin >> q;
for(int i = 0; i < q; i++) {
int l, r;
cin >> l >> r, l--, r--;
qq.PB({l, r, i});
}
sort(ALL(qq), greater<array<int, 3>>());
for(auto e:qq) {
while(ev.size() && ev.back().F >= e[0]) {
auto [l, m] = ev.back();
ev.pop_back();
int r = 2 * m - l;
if(r < n)
upd(r, n, a[l] + a[m]);
}
ans[e[2]] = qry(e[0], e[1] + 1);
}
for(int i = 0; i < q; i++)
cout << ans[i] << '\n';
}
Compilation message (stderr)
jumps.cpp: In function 'int main()':
jumps.cpp:140:9: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
140 | auto [l, m] = ev.back();
| ^
# | 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... |