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;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcount
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 5e5 + 7, INF = 1e18 + 7;
int n, q, a[N], ans[N];
vector <ii> add[N], d[N];
int tree[N << 2], prop[N << 2], mx[N << 2];
void relax(int v) {
tree[v] = max(tree[v * 2 + 1], tree[v * 2 + 2]);
mx[v] = max(mx[v * 2 + 1], mx[v * 2 + 2]);
}
void gap(int v, int x) {
prop[v] = max(prop[v], x);
tree[v] = max(tree[v], mx[v] + x);
}
void push(int v) {
if (prop[v]) {
gap(v * 2 + 1, prop[v]);
gap(v * 2 + 2, prop[v]);
prop[v] = 0;
}
}
int get(int v, int tl, int tr, int l, int r) {
if (tr < l || r < tl)
return 0;
if (l <= tl && tr <= r) {
#ifdef HOME
cout << "get " << tl << ' ' << tr << ' ' << tree[v] << '\n';
#endif
return tree[v];
}
int tm = (tl + tr) >> 1;
push(v);
return max(get(v * 2 + 1, tl, tm, l, r), get(v * 2 + 2, tm + 1, tr, l, r));
}
void upd(int v, int tl, int tr, int i, int x) {
if (tl == tr) {
tree[v] = max(tree[v], x);
mx[v] = max(mx[v], x);
return;
}
int tm = (tl + tr) >> 1;
push(v);
if (i <= tm)
upd(v * 2 + 1, tl, tm, i, x);
else
upd(v * 2 + 2, tm + 1, tr, i, x);
relax(v);
}
void print(int v, int tl, int tr) {
cout << tl << ' ' << tr << " : " << tree[v] << ' ' << prop[v] << ' ' << mx[v] << '\n';
if (tl == tr)
return;
int tm = (tl + tr) >> 1;
push(v);
print(v * 2 + 1, tl, tm);
print(v * 2 + 2, tm + 1, tr);
}
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
for (int i = 0; i < (N << 2); ++i)
tree[i] = mx[i] = -INF;
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i];
vector <ii> p;
vector <int> s;
for (int i = 0; i < n; ++i) {
while (s.size() && a[s.back()] <= a[i]) {
p.app(mp(s.back(), i));
s.pop_back();
}
s.app(i);
}
s.clear();
for (int i = n - 1; i >= 0; --i) {
while (s.size() && a[s.back()] <= a[i]) {
p.app(mp(i, s.back()));
s.pop_back();
}
s.app(i);
}
for (auto e : p) {
int l = e.s + (e.s - e.f);
if (l < n)
add[l].app(mp(e.f, a[e.f] + a[e.s]));
}
cin >> q;
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
--l; --r;
d[r].app(mp(l, i));
}
for (int i = 0; i < n; ++i) {
for (auto e : add[i]) {
upd(0, 0, n - 1, e.f, e.s);
//cout << "upd " << e.f << ' ' << e.s << '\n';
}
gap(0, a[i]);
//cout << "gap " << a[i] << '\n';
for (auto e : d[i])
ans[e.s] = get(0, 0, n - 1, e.f, n - 1);
}
for (int i = 0; i < q; ++i)
cout << ans[i] << '\n';
}
# | 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... |