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;
struct query {
int l, r;
int ans;
};
struct node {
int mx;
int ans;
int lzy;
};
const int N = 5e5 + 10;
int n, q;
int a[N];
query Q[N];
node st[4 * N];
vector<int> qs[N];
vector<int> upd[N];
stack<int> stk;
void build(int index, int l, int r) {
if(l > r) return;
if(l == r) {
st[index].mx = a[l];
st[index].ans = a[l];
st[index].lzy = 0;
return;
}
int mid = (l + r) / 2;
build(2 * index, l, mid);
build(2 * index + 1, mid + 1, r);
st[index].mx = max(st[2 * index].mx, st[2 * index + 1].mx);
st[index].ans = max(st[2 * index].ans, st[2 * index + 1].ans);
st[index].lzy = 0;
}
void propagate(int index) {
if(2 * index + 1 < 4 * N && st[index].lzy > 0) {
st[2 * index].ans = max(st[2 * index].ans, st[2 * index].mx + st[index].lzy);
st[2 * index].lzy = max(st[2 * index].lzy, st[index].lzy);
st[2 * index + 1].ans = max(st[2 * index + 1].ans, st[2 * index + 1].mx + st[index].lzy);
st[2 * index + 1].lzy = max(st[2 * index + 1].lzy, st[index].lzy);
st[index].lzy = 0;
}
}
void update(int index, int l, int r, int L, int R, int val) {
if(l > r || r < L || R < l) return;
if(L <= l && r <= R) {
st[index].ans = max(st[index].ans, val + st[index].mx);
st[index].lzy = max(st[index].lzy, val);
return;
}
propagate(index);
int mid = (l + r) / 2;
update(2 * index, l, mid, L, R, val);
update(2 * index + 1, mid + 1, r, L, R, val);
st[index].ans = max(st[2 * index].ans, st[2 * index + 1].ans);
}
int get(int index, int l, int r, int L, int R) {
if(l > r || r < L || R < l) return 0;
if(L <= l && r <= R) return st[index].ans;
propagate(index);
int mid = (l + r) / 2;
return max(get(2 * index, l, mid, L, R), get(2 * index + 1, mid + 1, r, L, R));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
cin >> q;
for(int i = 1; i <= q; i++) {
cin >> Q[i].l >> Q[i].r;
qs[Q[i].l].push_back(i);
}
for(int i = 1; i <= n; i++) {
while(!stk.empty() && a[stk.top()] <= a[i]) stk.pop();
if(!stk.empty()) {
int x = stk.top(), y = i;
if(y + (y - x) <= n) upd[x].push_back(y);
}
stk.push(i);
}
while(!stk.empty()) stk.pop();
for(int i = n; i >= 1; i--) {
while(!stk.empty() && a[stk.top()] <= a[i]) stk.pop();
if(!stk.empty()) {
int x = i, y = stk.top();
if(y + (y - x) <= n) upd[x].push_back(y);
}
stk.push(i);
}
build(1, 1, n);
for(int l = n; l >= 1; l--) {
for(int y : upd[l]) {
int x = l;
int z = y + (y - x);
update(1, 1, n, z, n, a[x] + a[y]);
}
for(int ind : qs[l]) {
int r = Q[ind].r;
Q[ind].ans = get(1, 1, n, l, r);
assert(Q[ind].ans >= 3);
}
}
for(int i = 1; i <= q; i++) cout << Q[i].ans << "\n";
return 0;
}
/*
6
6 1 2 3 1 1
1
1 6
*/
# | 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... |