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<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
#define MAXN 200005
const int INF = 1 << 30;
int N, Q;
int H[MAXN], A[MAXN], B[MAXN], L[MAXN], R[MAXN];
int ans[MAXN];
int minc[4*MAXN], maxd[4*MAXN], lazy[4*MAXN];
vector<int> query[MAXN], on[2*MAXN], off[2*MAXN];
void spreadlazy(int idx) {
lazy[idx * 2] = max(lazy[idx * 2], lazy[idx]);
lazy[idx * 2 + 1] = max(lazy[idx * 2 + 1], lazy[idx]);
maxd[idx * 2] = max(maxd[idx * 2], lazy[idx * 2] - minc[idx * 2]);
maxd[idx * 2 + 1] = max(maxd[idx * 2 + 1], lazy[idx * 2 + 1] - minc[idx * 2 + 1]);
lazy[idx] = -1;
}
void updc(int idx, int l, int r, int x, int c) {
if(l == r) {
minc[idx] = c;
lazy[idx] = -1;
return;
}
spreadlazy(idx);
int m = (l + r) / 2;
if(x <= m) updc(idx * 2, l, m, x, c);
else updc(idx * 2 + 1, m + 1, r, x, c);
minc[idx] = min(minc[idx * 2], minc[idx * 2 + 1]);
maxd[idx] = max(maxd[idx * 2], maxd[idx * 2 + 1]);
}
void updlazy(int idx, int l, int r, int x, int y, int z) {
if(x <= l && r <= y) {
lazy[idx] = max(lazy[idx], z);
maxd[idx] = max(maxd[idx], lazy[idx] - minc[idx]);
return;
}
if(r < x || y < l) return;
spreadlazy(idx);
int m = (l + r) / 2;
updlazy(idx * 2, l, m, x, y, z);
updlazy(idx * 2 + 1, m + 1, r, x, y, z);
maxd[idx] = max(maxd[idx * 2], maxd[idx * 2 + 1]);
}
int gmaxd(int idx, int l, int r, int x, int y) {
if(x <= l && r <= y) return maxd[idx];
if(r < x || y < l) return -1;
int m = (l + r) / 2;
spreadlazy(idx);
return max(gmaxd(idx * 2, l, m, x, y), gmaxd(idx * 2 + 1, m + 1, r, x, y));
}
void solve() {
for(int i = 0; i < N; i++) {
query[i].clear();
on[i].clear();
off[i].clear();
}
for(int i = 0; i < Q; i++) query[R[i]].push_back(i);
for(int i = 0; i < N; i++) {
on[i + A[i]].push_back(i);
off[i + B[i]].push_back(i);
}
for(int i = 1; i < 4 * N; i++) {
minc[i] = INF;
maxd[i] = lazy[i] = -1;
}
for(int i = 0; i < N; i++) {
for(auto a : on[i]) updc(1, 0, N-1, a, H[a]);
updlazy(1, 0, N-1, i - B[i], i - A[i], H[i]);
for(auto a : query[i]) ans[a] = max(ans[a], gmaxd(1, 0, N-1, L[a], i));
for(auto a : off[i]) updc(1, 0, N-1, a, INF);
}
}
int main() {
scanf("%d", &N);
for(int i = 0; i < N; i++) scanf("%d%d%d", H+i, A+i, B+i);
scanf("%d", &Q);
for(int i = 0; i < Q; i++) scanf("%d%d", L+i, R+i);
for(int i = 0; i < Q; i++) {
ans[i] = -1;
L[i]--;
R[i]--;
}
solve();
for(int i = 0; i < N/2; i++){
swap(H[i], H[N - i - 1]);
swap(A[i], A[N - i - 1]);
swap(B[i], B[N - i - 1]);
}
for(int i = 0; i < Q; i++) {
swap(L[i], R[i]);
L[i] = N - L[i] - 1;
R[i] = N - R[i] - 1;
}
solve();
for(int i = 0; i < Q; i++) printf("%d\n", ans[i]);
return 0;
}
Compilation message (stderr)
antennas.cpp: In function 'int main()':
antennas.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%d", &N);
| ~~~~~^~~~~~~~~~
antennas.cpp:87:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | for(int i = 0; i < N; i++) scanf("%d%d%d", H+i, A+i, B+i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:88:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | scanf("%d", &Q);
| ~~~~~^~~~~~~~~~
antennas.cpp:89:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | for(int i = 0; i < Q; i++) scanf("%d%d", L+i, R+i);
| ~~~~~^~~~~~~~~~~~~~~~~~
# | 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... |