이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define sz(v) int(std::size(v))
using i64 = long long;
const int N = 2e5, Q = 2e5, INF = 1e9;
const int N_ = 1 << 18;
int n;
int h[N], dl[N], dr[N];
vector<pair<int, bool>> upd[N + 1];
int q;
int ans[Q];
vector<pair<int, int>> qry[N];
struct {
int max_dif[N_ * 2];
int min_active[N_ * 2];
int max_put[N_ * 2];
void bld(int k, int l, int r) {
max_dif[k] = -1;
min_active[k] = INF;
max_put[k] = -INF;
if (l < r) {
int m = (l + r) / 2;
bld(k * 2, l, m), bld(k * 2 + 1, m + 1, r);
}
}
void bld() { bld(1, 0, n - 1); }
void pull(int k) {
min_active[k] = min(min_active[k * 2], min_active[k * 2 + 1]);
max_dif[k] = max(max_dif[k * 2], max_dif[k * 2 + 1]);
}
void put(int k, int x) {
max_put[k] = max(max_put[k], x);
max_dif[k] = max(max_dif[k], x - min_active[k]);
}
void push(int k) {
if (max_put[k] != -INF) {
put(k * 2, max_put[k]), put(k * 2 + 1, max_put[k]);
max_put[k] = -INF;
}
}
void set_point(int k, int l, int r, int i, int x) {
if (l == r) min_active[k] = x;
else {
int m = (l + r) / 2;
push(k);
i <= m ? set_point(k * 2, l, m, i, x) : set_point(k * 2 + 1, m + 1, r, i, x);
pull(k);
}
}
void put_range(int k, int l, int r, int ql, int qr, int x) {
if (ql <= l && qr >= r) put(k, x);
else {
int m = (l + r) / 2;
push(k);
if (ql <= m) put_range(k * 2, l, m, ql, qr, x);
if (qr > m) put_range(k * 2 + 1, m + 1, r, ql, qr, x);
pull(k);
}
}
int qry(int k, int l, int r, int ql, int qr) {
if (ql <= l && qr >= r) return max_dif[k];
int m = (l + r) / 2;
push(k);
return max(ql <= m ? qry(k * 2, l, m, ql, qr) : -1, qr > m ? qry(k * 2 + 1, m + 1, r, ql, qr) : -1);
}
void set_point(int i, int x) { set_point(1, 0, n - 1, i, x); }
void put_range(int l, int r, int x) { put_range(1, 0, n - 1, l, r, x); }
int qry(int l, int r) { return qry(1, 0, n - 1, l, r); }
} seg;
void solve() {
seg.bld(1, 0, n - 1);
for (int i = 0; i < n; i++) {
for (auto [j, type] : upd[i])
seg.set_point(j, type ? h[j] : INF);
if (i - dl[i] >= 0) seg.put_range(max(0, i - dr[i]), i - dl[i], h[i]);
for (auto [h, l] : qry[i]) ans[h] = max(ans[h], seg.qry(l, i));
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> h[i] >> dl[i] >> dr[i];
upd[min(n, i + dl[i])].push_back({i, 1});
upd[min(n, i + dr[i] + 1)].push_back({i, 0});
}
cin >> q;
for (int h = 0; h < q; h++) {
int l, r;
cin >> l >> r, l--, r--;
qry[r].push_back({h, l});
ans[h] = -1;
}
solve();
for (int i = 0; i < n; i++) h[i] *= -1;
solve();
for (int h = 0; h < q; h++) cout << ans[h] << '\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... |