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 int long long
const int Inf = 1e15;
using namespace std;
const int maxn = 2e5 + 7;
int h[maxn], a[maxn], b[maxn], l[maxn], r[maxn], ans[maxn];
int d[4 * maxn], c[maxn], q, n;
vector<int> add[maxn], era[maxn], chk[maxn];
int D[4 * maxn], lz[4 * maxn], C[maxn * 4];
void Push_Down(int id, int l, int r)
{
if(lz[id] != Inf && l < r)
{
lz[id * 2] = min(lz[id * 2], lz[id]);
lz[id * 2 + 1] = min(lz[id * 2 + 1], lz[id]);
D[id * 2] = max(D[id * 2], C[id * 2] - lz[id * 2]);
D[id * 2 + 1] = max(D[id * 2 + 1], C[id * 2 + 1] - lz[id * 2 + 1]);
}
lz[id] = Inf;
}
void Set(int id, int l, int r, int pos, int val)
{
Push_Down(id, l, r);
if(l == r)
{
C[id] = val;
return;
}
Push_Down(id, l, r);
int mid = l + r >> 1;
if(pos <= mid) Set(id * 2, l, mid, pos, val);
else Set(id * 2 + 1, mid + 1, r, pos, val);
D[id] = max(D[id * 2], D[id * 2 + 1]);
C[id] = max(C[id * 2], C[id * 2 + 1]);
}
void Update(int id, int l, int r, int u, int v, int val)
{
Push_Down(id, l, r);
if(l > v || r < u) return;
if(u <= l && r <= v)
{
lz[id] = val;
D[id] = max(D[id], C[id] - lz[id]);
return;
}
int mid = l + r >> 1;
Update(id * 2, l, mid, u, v, val);
Update(id * 2 + 1, mid + 1, r, u, v, val);
D[id] = max(D[id * 2], D[id * 2 + 1]);
C[id] = max(C[id * 2], C[id * 2 + 1]);
}
int Get(int id, int l, int r, int u, int v)
{
Push_Down(id, l, r);
if(l > v || r < u) return -1;
if(u <= l && r <= v) return D[id];
int mid = l + r >> 1;
return max(Get(id * 2, l, mid, u, v), Get(id * 2 + 1, mid + 1, r, u, v));
}
void Solve()
{
for(int i = 0; i < 4 * maxn; i++) lz[i] = Inf, D[i] = C[i] = -Inf;
for(int i = 1; i <= n + 1; i++) add[i] = era[i] = chk[i] = {};
for(int i = 1; i <= q; i++) chk[r[i]].push_back(i);
for(int i = 1; i <= n; i++)
{
if(i + a[i] <= n) add[i + a[i]].push_back(i);
if(i + b[i] + 1 <= n + 1) era[i + b[i] + 1].push_back(i);
}
for(int i = 1; i <= n + 1; i++)
{
for(auto j: chk[i - 1])
{
ans[j] = max(ans[j], Get(1, 1, n, l[j], r[j]));
}
if(i == n + 1) return;
for(auto j: add[i]) Set(1, 1, n, j, h[j]);
for(auto j: era[i]) Set(1, 1, n, j, -Inf);
if(i - a[i] >= 1)
Update(1, 1, n, max(i - b[i], 1ll), i - a[i], h[i]);
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
if(fopen("test.inp", "r")) freopen("test.inp", "r", stdin), freopen("test.out", "w", stdout);
fill_n(ans, maxn, -1);
cin >> n;
for(int i = 1; i <= n; i++) cin >> h[i] >> a[i] >> b[i];
cin >> q;
for(int i = 1; i <= q; i++) cin >> l[i] >> r[i];
Solve();
reverse(h + 1, h + n + 1);
reverse(a + 1, a + n + 1);
reverse(b + 1, b + n + 1);
for(int i = 1; i <= q; i++)
{
l[i] = n + 1 - l[i];
r[i] = n + 1 - r[i];
swap(l[i], r[i]);
}
Solve();
for(int i = 1; i <= q; i++) cout << ans[i] << '\n';
}
Compilation message (stderr)
antennas.cpp: In function 'void Set(long long int, long long int, long long int, long long int, long long int)':
antennas.cpp:34:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
antennas.cpp: In function 'void Update(long long int, long long int, long long int, long long int, long long int, long long int)':
antennas.cpp:51:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
antennas.cpp: In function 'long long int Get(long long int, long long int, long long int, long long int, long long int)':
antennas.cpp:63:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
antennas.cpp: In function 'int32_t main()':
antennas.cpp:95:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
if(fopen("test.inp", "r")) freopen("test.inp", "r", stdin), freopen("test.out", "w", stdout);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:95:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
# | 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... |