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>
typedef long long ll;
using namespace std;
const int oo = 1e9;
int n, a[200001], b[200001], h[200001], mx[200001], ans[200001];
vector<int> ins[400001], rem[400001];
int segtree[800001];
vector<pair<int, int>> Q[200001];
void build(int node = 1, int l = 1, int r = n) {
segtree[node] = -oo;
if (l != r) {
int mid = (l + r) / 2;
build(node * 2, l, mid);
build(node * 2 + 1, mid + 1, r);
}
}
void update(int pos, int val, int node = 1, int l = 1, int r = n) {
if (l == r) segtree[node] = val;
else {
int mid = (l + r) / 2;
if (pos > mid) update(pos, val, node * 2 + 1, mid + 1, r);
else update(pos, val, node * 2, l, mid);
segtree[node] = max(segtree[node * 2], segtree[node * 2 + 1]);
}
}
int query(int x, int y, int node = 1, int l = 1, int r = n) {
if (l > y || r < x) return -oo;
if (l >= x && r <= y) return segtree[node];
int mid = (l + r) / 2;
return max(query(x, y, node * 2, l, mid),
query(x, y, node * 2 + 1, mid + 1, r));
}
void solve(int l, int r) {
build();
for (int i = l; i <= r; i++) {
for (int j : ins[i]) update(j, h[j]);
mx[i] = max(mx[i], query(i - b[i], i - a[i]) - h[i]);
for (int j : rem[i]) update(j, -oo);
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> h[i] >> a[i] >> b[i];
int q;
cin >> q;
for (int i = 1; i <= q; i++) {
int l, r;
cin >> l >> r;
Q[l].push_back({r, i});
}
for (int i = n; i; i--) {
ins[i + a[i]].push_back(i);
rem[i + b[i]].push_back(i);
if (Q[i].size()) {
sort(Q[i].begin(), Q[i].end());
memset(mx, -1, sizeof mx);
solve(i, n);
for (int j = i; j <= n; j++) h[j] *= -1;
solve(i, n);
for (int j = i; j <= n; j++) h[j] *= -1;
for (int j = 0; j < Q[i].size(); j++) {
if (!j) ans[Q[i][j].second] = *max_element(mx + i, mx + Q[i][j].first + 1);
else ans[Q[i][j].second] = max(ans[Q[i][j - 1].second], *max_element(mx + Q[i][j - 1].first, mx + Q[i][j].first + 1));
}
}
}
for (int i = 1; i <= q; i++) cout << ans[i] << '\n';
return 0;
}
Compilation message (stderr)
antennas.cpp: In function 'int main()':
antennas.cpp:72:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int j = 0; j < Q[i].size(); j++) {
| ~~^~~~~~~~~~~~~
# | 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... |