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>
#define ll long long
#define ar array
#define db double
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rint(l, r) uniform_int_distribution<int>(l, r)(rng)
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
const int INF = int(1e9) + 5;
struct seg {
int N; vector<int> tree, tree2, lazy;
seg(int n) : N(1<<(__lg(n)+1)), tree(2*N, -1), tree2(2*N, 1e9), lazy(2*N, -INF) {}
void push(int node) {
if (lazy[node] == -INF) return;
ckmax(tree[node], lazy[node] - tree2[node]);
if (node < N) {
ckmax(lazy[node*2], lazy[node]);
ckmax(lazy[node*2+1], lazy[node]);
}
lazy[node] = -INF;
}
void update(int node, int nl, int nr, int pos, int x) {
push(node);
if (pos > nr || pos < nl) return;
if (nl == nr && nl == pos) {
tree2[node] = x; return;
}
int mid = (nl+nr)/2;
update(node*2, nl, mid, pos, x);
update(node*2+1, mid+1, nr, pos, x);
tree[node] = max(tree[node*2], tree[node*2+1]);
tree2[node] = min(tree2[node*2], tree2[node*2+1]);
}
void update2(int node, int nl, int nr, int ql, int qr, int x) {
push(node);
if (ql > nr || qr < nl) return;
if (ql <= nl && nr <= qr) {
ckmax(lazy[node], x); push(node); return;
}
int mid = (nl+nr)/2;
update2(node*2, nl, mid, ql, qr, x);
update2(node*2+1, mid+1, nr, ql, qr, x);
tree[node] = max(tree[node*2], tree[node*2+1]);
}
int query(int node, int nl, int nr, int ql, int qr) {
push(node);
if (ql > nr || qr < nl) return -1;
if (ql <= nl && nr <= qr) {
return tree[node];
}
int mid = (nl+nr)/2;
return max(query(node*2, nl, mid, ql, qr), query(node*2+1, mid+1, nr, ql, qr));
}
void update(int pos, int x) {
update(1, 0, N-1, pos, x);
}
void update2(int l, int r, int x) {
update2(1, 0, N-1, l, r, x);
}
int query(int l, int r) {
return query(1, 0, N-1, l, r);
}
};
void test_case() {
int n; cin >> n;
vector<int> h(n), a(n), b(n);
for (int i = 0; i < n; i++) cin >> h[i] >> a[i] >> b[i];
int q; cin >> q;
vector<int> ans(q, -1);
vector<vector<ar<int, 2>>> queries(n);
for (int i = 0; i < q; i++) {
int l, r; cin >> l >> r;
l--, r--;
queries[r].push_back({l, i});
}
for (int t = 0; t < 2; t++) {
seg st(n);
vector<vector<int>> in(n), out(n);
for (int i = 0; i < n; i++) {
if (i + a[i] < n) in[i + a[i]].emplace_back(i);
if (i + b[i] + 1 < n) out[i + b[i] + 1].emplace_back(i);
}
for (int i = 0; i < n; i++) {
for (int p : in[i]) st.update(p, h[p]);
for (int p : out[i]) st.update(p, 1e9);
int l = max(0, i - b[i]), r = i - a[i];
if (l <= r) {
st.update2(l, r, h[i]);
}
for (auto [from, ans_idx] : queries[i]) {
ckmax(ans[ans_idx], st.query(from, i));
}
}
for (int &x : h) x *= -1;
}
for (int i = 0; i < q; i++) cout << ans[i] << '\n';
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
// cin >> t;
while (t--) test_case();
}
# | 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... |