This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
#include<bits/stdc++.h>
using namespace std;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) (int)a.size()
#define s second
#define f first
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<pii> rid = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
vector<pii> dir = {{-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
const int N = 5e5 + 1, mod = 1e9 + 7;
const ll inf = 1e9;
double eps = 1e-15;
bool flg = 0;
array<int, 4> t[4 * N];
int add[4 * N], a[N], n;
void push(int u, int tl, int tr) {
if (!add[u]) return;
t[u][0] = t[u][3] = add[u];
t[u][2] = t[u][1] + t[u][0];
if (tl < tr) {
add[u * 2] = add[u];
add[u * 2 + 1] = add[u];
}
add[u] = 0;
}
void bld(int u = 1, int tl = 1, int tr = n) {
if (tl == tr) {
t[u][1] = a[tl];
return;
}
int m = (tl + tr) / 2;
bld(u * 2, tl, m);
bld(u * 2 + 1, m + 1, tr);
t[u][1] = max(t[u * 2][1], t[u * 2 + 1][1]);
}
void upt(int l, int r, int v, int u = 1, int tl = 1, int tr = n) {
push(u, tl, tr);
if (tl > r || tr < l || t[u][3] >= v) return;
if (tl >= l && tr <= r && t[u][0] <= v) {
add[u] = v;
push(u, tl, tr);
return;
}
int m = (tl + tr) / 2;
upt(l, r, v, u * 2, tl, m);
upt(l, r, v, u * 2 + 1, m + 1, tr);
t[u][0] = max(t[u * 2][0], t[u * 2 + 1][0]);
t[u][3] = min(t[u * 2][3], t[u * 2 + 1][3]);
t[u][2] = max(t[u * 2][2], t[u * 2 + 1][2]);
}
int get(int l, int r, int u = 1, int tl = 1, int tr = n) {
push(u, tl, tr);
if (tl > r || tr < l) return 0;
if (tl >= l && tr <= r) return t[u][2];
int m = (tl + tr) / 2;
return max(get(l, r, u * 2, tl, m), get(l, r, u * 2 + 1, m + 1, tr));
}
void slv() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
bld();
int q;
cin >> q;
vector<pii> qr[n + 1];
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
qr[l].push_back({r, i});
}
vector<pii> h[4 * n];
stack<int> s;
for (int i = n; i >= 1; i--) {
while (sz(s) && a[s.top()] < a[i]) s.pop();
if (sz(s)) {
int x = s.top() - i;
if (s.top() + x <= n) {
h[i].push_back({s.top() + x, a[i] + a[s.top()]});
}
}
s.push(i);
}
while (sz(s)) s.pop();
for (int i = 1; i <= n; i++) {
while (sz(s) && a[s.top()] < a[i]) s.pop();
if (sz(s)) {
int x = i - s.top();
if (i + x <= n) {
h[s.top()].push_back({i + x, a[i] + a[s.top()]});
}
}
s.push(i);
}
int ans[q + 1];
for (int i = n; i >= 1; i--) {
for (auto [r, v]: h[i]) upt(r, n, v);
for (auto [r, id]: qr[i]) ans[id] = get(i + 2, r);
}
cout << '\n';
for (int i = 1; i <= q; i++) cout << ans[i] << ' ';
}
main() {
//freopen("rsq.in", "r", stdin);
//freopen("rsq.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int tp = 1;
if (flg) cin >> tp;
while (tp--) {
slv();
}
}
//wenomechainsama
Compilation message (stderr)
jumps.cpp:124:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
124 | main() {
| ^~~~
# | 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... |