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>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
void read(T &x)
{
x = 0;
register int c;
while ((c = getchar()) && (c > '9' || c < '0'))
;
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
}
constexpr bool typetest = 0;
constexpr int N = 2e5 + 5;
constexpr int Inf = 1e9 + 7;
// SegmentTree kĩ thuật thượng thừa
// Lả lướt với SegmetTree
struct SegmentTree
{
int st[N * 4], maxn[N * 4], minn[N * 4], n;
SegmentTree()
{
}
void Assign(int n)
{
this->n = n;
fill_n(maxn, N * 4, -Inf);
fill_n(minn, N * 4, Inf);
fill_n(st, N * 4, -Inf);
}
void Push(int id)
{
if (minn[id] != Inf)
{
minn[id << 1] = min(minn[id << 1], minn[id]);
st[id << 1] = max(st[id << 1], maxn[id << 1] - minn[id << 1]);
minn[id << 1 | 1] = min(minn[id << 1 | 1], minn[id]);
st[id << 1 | 1] = max(st[id << 1 | 1], maxn[id << 1 | 1] - minn[id << 1 | 1]);
minn[id] = Inf;
}
}
void UpdateHigh(int id, int l, int r, const int &x, const int &v)
{
if (l > x || r < x)
return;
if (l == x && r == x)
{
st[id] = max(st[id], maxn[id] - minn[id]);
maxn[id] = v;
minn[id] = Inf;
return;
}
Push(id);
UpdateHigh(id << 1, l, (l + r) / 2, x, v);
UpdateHigh(id << 1 | 1, (l + r) / 2 + 1, r, x, v);
st[id] = max({st[id], st[id << 1], st[id << 1 | 1]});
maxn[id] = max(maxn[id << 1], maxn[id << 1 | 1]);
}
void UpdateHigh(int x, int v)
{
UpdateHigh(1, 1, n, x, v);
}
void UpdateLow(int id, int l, int r, const int &a, const int &b, const int &v)
{
if (l > b || r < a)
return;
if (l >= a && r <= b)
{
minn[id] = min(minn[id], v);
st[id] = max(st[id], maxn[id] - minn[id]);
return;
}
Push(id);
UpdateLow(id << 1, l, (l + r) / 2, a, b, v);
UpdateLow(id << 1 | 1, (l + r) / 2 + 1, r, a, b, v);
st[id] = max({st[id], st[id << 1], st[id << 1 | 1]});
}
void UpdateLow(int l, int r, int v)
{
UpdateLow(1, 1, n, l, r, v);
}
int Get(int id, int l, int r, const int &a, const int &b)
{
if (l > b || r < a)
return -Inf;
if (l >= a && r <= b)
return st[id];
Push(id);
return max(Get(id << 1, l, (l + r) / 2, a, b), Get(id << 1 | 1, (l + r) / 2 + 1, r, a, b));
}
int Get(int l, int r)
{
return Get(1, 1, n, l, r);
}
} f;
int n, q, l[N], r[N], x[N], y[N], a[N], ans[N];
vector<int> in[N], out[N], que[N];
void Read()
{
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i] >> x[i] >> y[i];
cin >> q;
for (int i = 1; i <= q; ++i)
{
cin >> l[i] >> r[i];
ans[i] = -1;
}
}
void Cal()
{
for (int i = 1; i <= q; ++i)
que[l[i]].emplace_back(i);
f.Assign(n);
for (int i = n; i; --i)
{
for (auto j : in[i])
f.UpdateHigh(j, a[j]);
for (auto j : out[i])
f.UpdateHigh(j, -Inf);
in[i].clear();
out[i].clear();
if (i + x[i] <= n)
f.UpdateLow(i + x[i], min(n, i + y[i]), a[i]);
for (auto j : que[i])
ans[j] = max(ans[j], f.Get(i, r[j]));
que[i].clear();
if (i - x[i] > 0)
in[i - x[i]].emplace_back(i);
if (i - y[i] - 1 > 0)
out[i - y[i] - 1].emplace_back(i);
}
}
void Solve()
{
Cal();
reverse(a + 1, a + n + 1);
reverse(x + 1, x + n + 1);
reverse(y + 1, y + n + 1);
for (int i = 1; i <= q; ++i)
{
l[i] = n - l[i] + 1;
r[i] = n - r[i] + 1;
swap(l[i], r[i]);
}
Cal();
for (int i = 1; i <= q; ++i)
cout << ans[i] << "\n";
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (fopen("palesta.INP", "r"))
{
freopen("paletsa.inp", "r", stdin);
freopen("palesta.out", "w", stdout);
}
int t(1);
if (typetest)
cin >> t;
for (int _ = 1; _ <= t; ++_)
{
// cout << "Case #" << _ << ": ";
Read();
Solve();
}
// cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
/*
*/
Compilation message (stderr)
antennas.cpp: In function 'void read(T&)':
antennas.cpp:12:18: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
12 | register int c;
| ^
antennas.cpp: In function 'int32_t main()':
antennas.cpp:194:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
194 | freopen("paletsa.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:195:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
195 | freopen("palesta.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... |