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 fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 2e5 + 10;
int h[N], a[N], b[N], T[N << 2], mx[N << 2], mn[N << 2], ans[N];
vector<ii> query[N], event[N];
int n;
void push(int s, int l, int r) {
T[s] = max(T[s], mx[s] - mn[s]);
if (l != r) {
mx[s << 1] = max(mx[s << 1], mx[s]);
mx[s << 1 | 1] = max(mx[s << 1 | 1], mx[s]);
}
mx[s] = 0;
}
void upval (int s, int l, int r, int pos, int val) {
push(s, l, r);
if (l == r) {
mn[s] = val;
return;
}
int mid = l + r >> 1;
if (pos <= mid) upval(s << 1, l, mid, pos, val);
else upval(s << 1 | 1, mid + 1, r, pos, val);
mn[s] = min(mn[s << 1], mn[s << 1 | 1]);
}
void upans (int s, int l, int r, int from, int to, int val) {
push(s, l, r);
if (l > to || r < from) return;
if (from <= l && r <= to) {
mx[s] = val;
push(s, l, r);
return;
}
int mid = l + r >> 1;
upans(s << 1, l, mid, from, to, val);
upans(s << 1 | 1, mid + 1, r, from, to, val);
T[s] = max(T[s << 1], T[s << 1 | 1]);
}
int get(int s, int l, int r, int from, int to) {
push(s, l, r);
if (l > to || r < from) return -1;
if (from <= l && r <= to) return T[s];
int mid = l + r >> 1;
return max(get(s << 1, l, mid, from, to), get(s << 1 | 1, mid + 1, r, from, to));
}
void solve() {
for(int i = 1; i <= 4 * n; i++) T[i] = -1, mx[i] = 0, mn[i] = 1e9;
for(int i = 1; i <= n; i++) {
if (i + a[i] <= n) event[i + a[i]].emplace_back(i, h[i]);
if (i + b[i] + 1 <= n) event[i + b[i] + 1].emplace_back(i, 1e9);
}
for(int r = 1; r <= n; r++) {
for(auto &[pos, val] : event[r]) upval(1, 1, n, pos, val);
upans(1, 1, n, r - b[r], r - a[r], h[r]);
for(auto &[l, idx] : query[r]) ans[idx] = max(ans[idx], get(1, 1, n, l, n));
event[r].clear();
}
}
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
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;
query[r].emplace_back(l, i);
ans[i] = -1;
}
solve();
for(int i = 1; i <= n; i++) h[i] = 1e9 - h[i];
solve();
for(int i = 1; i <= q; i++) cout << ans[i] << endl;
}
Compilation message (stderr)
antennas.cpp: In function 'void upval(int, int, int, int, int)':
antennas.cpp:25:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | int mid = l + r >> 1;
| ~~^~~
antennas.cpp: In function 'void upans(int, int, int, int, int, int)':
antennas.cpp:38:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid = l + r >> 1;
| ~~^~~
antennas.cpp: In function 'int get(int, int, int, int, int)':
antennas.cpp:47:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int mid = l + r >> 1;
| ~~^~~
antennas.cpp: In function 'int main()':
antennas.cpp:68:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | freopen ("task.inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:69:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | freopen ("task.out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:72:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:73:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen (task".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |