This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// BEGIN BOILERPLATE CODE
#include <bits/stdc++.h>
using namespace std;
#ifdef KAMIRULEZ
const bool kami_loc = true;
const long long kami_seed = 69420;
#else
const bool kami_loc = false;
const long long kami_seed = chrono::steady_clock::now().time_since_epoch().count();
#endif
const string kami_fi = "kamirulez.inp";
const string kami_fo = "kamirulez.out";
mt19937_64 kami_gen(kami_seed);
long long rand_range(long long rmin, long long rmax) {
uniform_int_distribution<long long> rdist(rmin, rmax);
return rdist(kami_gen);
}
long double rand_real(long double rmin, long double rmax) {
uniform_real_distribution<long double> rdist(rmin, rmax);
return rdist(kami_gen);
}
void file_io(string fi, string fo) {
if (fi != "" && (!kami_loc || fi == kami_fi)) {
freopen(fi.c_str(), "r", stdin);
}
if (fo != "" && (!kami_loc || fo == kami_fo)) {
freopen(fo.c_str(), "w", stdout);
}
}
void set_up() {
if (kami_loc) {
file_io(kami_fi, kami_fo);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
}
void just_do_it();
void just_exec_it() {
if (kami_loc) {
auto pstart = chrono::steady_clock::now();
just_do_it();
auto pend = chrono::steady_clock::now();
long long ptime = chrono::duration_cast<chrono::milliseconds>(pend - pstart).count();
string bar(50, '=');
cout << '\n' << bar << '\n';
cout << "Time: " << ptime << " ms" << '\n';
}
else {
just_do_it();
}
}
int main() {
set_up();
just_exec_it();
return 0;
}
// END BOILERPLATE CODE
// BEGIN MAIN CODE
const int ms = 2e5 + 20;
const int inf = 1e9 + 20;
int h[ms];
int a[ms];
int b[ms];
int res[ms];
int n, q;
struct query {
int id, lt, rt;
};
struct node {
int ma, mb, ts;
};
query qs[ms];
node tr[ms * 4];
vector<int> add[ms];
vector<int> rem[ms];
vector<query> qr[ms];
void push(int cc) {
tr[cc * 2].mb = max(tr[cc * 2].mb, tr[cc].mb);
tr[cc * 2].ts = max(tr[cc * 2].ts, tr[cc * 2].mb - tr[cc * 2].ma);
tr[cc * 2 + 1].mb = max(tr[cc * 2 + 1].mb, tr[cc].mb);
tr[cc * 2 + 1].ts = max(tr[cc * 2 + 1].ts, tr[cc * 2 + 1].mb - tr[cc * 2 + 1].ma);
tr[cc].mb = -inf;
}
void update_a(int cc, int lt, int rt, int px, int pp) {
if (lt == rt) {
tr[cc].ma = pp;
tr[cc].mb = -inf;
return;
}
push(cc);
int mt = (lt + rt) / 2;
if (px <= mt) {
update_a(cc * 2, lt, mt, px, pp);
}
else {
update_a(cc * 2 + 1, mt + 1, rt, px, pp);
}
tr[cc].ma = min(tr[cc * 2].ma, tr[cc * 2 + 1].ma);
tr[cc].ts = max(tr[cc * 2].ts, tr[cc * 2 + 1].ts);
}
void update_b(int cc, int lt, int rt, int ql, int qr, int pp) {
if (lt == ql && rt == qr) {
tr[cc].mb = max(tr[cc].mb, pp);
tr[cc].ts = max(tr[cc].ts, tr[cc].mb - tr[cc].ma);
return;
}
push(cc);
int mt = (lt + rt) / 2;
if (qr <= mt) {
update_b(cc * 2, lt, mt, ql, qr, pp);
}
else if (ql >= mt + 1) {
update_b(cc * 2 + 1, mt + 1, rt, ql, qr, pp);
}
else {
update_b(cc * 2, lt, mt, ql, mt, pp);
update_b(cc * 2 + 1, mt + 1, rt, mt + 1, qr, pp);
}
tr[cc].ts = max(tr[cc * 2].ts, tr[cc * 2 + 1].ts);
}
int get(int cc, int lt, int rt, int ql, int qr) {
if (lt == ql && rt == qr) {
return tr[cc].ts;
}
push(cc);
int mt = (lt + rt) / 2;
if (qr <= mt) {
return get(cc * 2, lt, mt, ql, qr);
}
else if (ql >= mt + 1) {
return get(cc * 2 + 1, mt + 1, rt, ql, qr);
}
else {
return max(get(cc * 2, lt, mt, ql, mt), get(cc * 2 + 1, mt + 1, rt, mt + 1, qr));
}
}
void solve() {
fill_n(tr, ms * 4, node({inf, -inf, -inf}));
for (int i = 1; i <= n; i++) {
add[i].clear();
rem[i].clear();
qr[i].clear();
}
for (int i = 1; i <= n; i++) {
int lt = i + a[i];
int rt = i + b[i];
if (lt <= n) {
add[lt].push_back(i);
rem[min(rt, n)].push_back(i);
}
}
for (int i = 1; i <= q; i++) {
qr[qs[i].rt].push_back(qs[i]);
}
for (int i = 1; i <= n; i++) {
for (auto x: add[i]) {
update_a(1, 1, n, x, h[x]);
}
int lt = i - b[i];
int rt = i - a[i];
if (rt >= 1) {
update_b(1, 1, n, max(lt, 1), rt, h[i]);
}
for (auto x: qr[i]) {
res[x.id] = max(res[x.id], get(1, 1, n, x.lt, x.rt));
}
for (auto x: rem[i]) {
update_a(1, 1, n, x, inf);
}
}
}
void just_do_it() {
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 >> qs[i].lt >> qs[i].rt;
qs[i].id = i;
res[i] = -inf;
}
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++) {
qs[i].lt = n + 1 - qs[i].lt;
qs[i].rt = n + 1 - qs[i].rt;
swap(qs[i].lt, qs[i].rt);
}
solve();
for (int i = 1; i <= q; i++) {
cout << max(res[i], -1) << '\n';
}
}
// END MAIN CODE
Compilation message (stderr)
antennas.cpp: In function 'void file_io(std::string, std::string)':
antennas.cpp:30:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen(fi.c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:33:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen(fo.c_str(), "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... |