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;
mt19937 rng(time(0));
int random(int l, int r) { return rng() % (r - l + 1) + l; }
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);
// while(true) {
cin >> n;
// n = 10;
for(int i = 1; i <= n; i++) cin >> a[i];
// for(int i = 1; i <= n; i++) a[i] = random(1, 10);
cin >> q;
// q = 10;
for(int i = 1; i <= q; i++) {
cin >> Q[i].l >> Q[i].r;
// Q[i].l = random(1, 8);
// Q[i].r = random(Q[i].l + 2, 10);
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);
}
}
for(int i = 1; i <= q; i++) cout << Q[i].ans << "\n";
// for(int i = 1; i <= q; i++) {
// int l = Q[i].l, r = Q[i].r;
// int mx = 0;
// for(int x = l; x <= r; x++) {
// for(int y = x + 1; y <= r; y++) {
// for(int z = y + (y - x); z <= r; z++) {
// mx = max(mx, a[x] + a[y] + a[z]);
// }
// }
// }
// if(mx != Q[i].ans) {
// cout << n << "\n";
// for(int i = 1; i <= n; i++) cout << a[i] << " ";
// cout << "\n";
// cout << q << "\n";
// for(int i = 1; i <= q; i++) cout << Q[i].l << " " << Q[i].r << "\n";
// return 0;
// }
// }
// }
return 0;
}
/*
10
2 4 5 2 10 2 9 9 10 8
10
8 10
5 7
7 9
5 10
3 10
8 10
5 9
3 5
8 10
7 9
*/
# | 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... |