이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
struct ant {
int h, a, b;
void read() { cin >> h >> a >> b; }
};
const int oo = 1e9 + 213;
struct T {
int d = -1, c = 0, lazy = oo;
void merge(T l, T r)
{
assert(lazy == oo && l.lazy == oo && r.lazy == oo);
d = max(l.d, r.d);
c = max(l.c, r.c);
}
};
const int nax = 2e5 + 213;
struct tree {
#define lc 2 * v
#define rc 2 * v + 1
#define m (l + r) / 2
vector<T> t = vector<T>(4 * nax);
void push(int v, int l, int r)
{
if(t[v].lazy == oo) {
return;
}
if(l != r) {
for(int to : {lc, rc}) {
t[to].lazy = min(t[to].lazy, t[v].lazy);
}
}
t[v].d = max(t[v].d, t[v].c - t[v].lazy);
t[v].lazy = oo;
}
void setC(int v, int l, int r, int p, int x)
{
push(v, l, r);
if(p < l || p > r) {
return;
}
if(p <= l && r <= p) {
t[v].c = x;
return;
}
setC(lc, l, m, p, x);
setC(rc, m + 1, r, p, x);
t[v].merge(t[lc], t[rc]);
}
void putLazy(int v, int l, int r, int ql, int qr, int x)
{
push(v, l, r);
if(qr < l || ql > r) {
return;
}
if(ql <= l && r <= qr) {
t[v].lazy = x;
push(v, l, r);
return;
}
putLazy(lc, l, m, ql, qr, x);
putLazy(rc, m + 1, r, ql, qr, x);
t[v].merge(t[lc], t[rc]);
}
int query(int v, int l, int r, int ql, int qr)
{
push(v, l, r);
if(qr < l || ql > r) {
return -1;
}
if(ql <= l && r <= qr) {
return t[v].d;
}
int ret = -1;
ret = max(ret, query(lc, l, m, ql, qr));
ret = max(ret, query(rc, m + 1, r, ql, qr));
t[v].merge(t[lc], t[rc]);
return ret;
}
};
int32_t main()
{
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
vector<ant> in(n + 1);
for(int i = 1; i <= n; i++) {
in[i].read();
}
int q;
cin >> q;
vector<int> ql(q + 1), qr(q + 1), ans(q + 1, -1);
for(int i = 1; i <= q; i++) {
cin >> ql[i] >> qr[i];
}
auto solve = [&]() {
vector<vector<int>> qs(n + 1), st(n + 1), en(n + 1);
tree seg;
for(int i = 1; i <= q; i++) {
qs[qr[i]].emplace_back(i);
}
for(int i = 1; i <= n; i++) {
if(i + in[i].a <= n) {
st[i + in[i].a].emplace_back(i);
}
if(i + in[i].b <= n) {
en[i + in[i].b].emplace_back(i);
}
for(int j : st[i]) {
seg.setC(1, 1, n, j, in[j].h);
}
if(i - in[i].a >= 1) {
seg.putLazy(1, 1, n, max(1, i - in[i].b), i - in[i].a, in[i].h);
}
for(int j : qs[i]) {
ans[j] = max(ans[j], seg.query(1, 1, n, ql[j], qr[j]));
}
for(int j : en[i]) {
seg.setC(1, 1, n, j, 0);
}
}
};
solve();
reverse(1 + in.begin(), in.end());
for(int i = 1; i <= q; i++) {
ql[i] = n - ql[i] + 1;
qr[i] = n - qr[i] + 1;
swap(ql[i], qr[i]);
}
solve();
for(int i = 1; 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... |