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 endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const ll maxn = 2e5 + 10;
struct query
{
ll l, r, idx;
query(ll _l = 0, ll _r = 0, ll _idx = 0)
{
l = _l;
r = _r;
idx = _idx;
}
} task[maxn];
ll n, q;
ll h[maxn], a[maxn], b[maxn];
vector < query > spot[maxn];
ll ans[maxn];
void input()
{
cin >> n;
for (ll i = 1; i <= n; i ++)
{
cin >> h[i] >> a[i] >> b[i];
}
cin >> q;
for (ll i = 1; i <= q; i ++)
ans[i] = -1;
for (ll i = 1; i <= q; i ++)
{
cin >> task[i].l >> task[i].r;
task[i].idx = i;
spot[task[i].r].push_back(task[i]);
}
}
const ll inf = 2e9 + 10;
ll act[maxn], low[maxn];
vector < ll > upd[2][2 * maxn];
struct node
{
ll max_dif, max_low, sec_low;
ll res;
node()
{
max_dif = -inf;
max_low = inf;
sec_low = -inf;
res = -inf;
}
};
node merge_nodes(node lf, node rf)
{
/// careful for free node
node mf;
mf.max_low = max(lf.max_low, rf.max_low);
if (lf.max_low != mf.max_low && lf.max_low > mf.sec_low)
mf.sec_low = lf.max_low;
if (lf.sec_low != mf.max_low && lf.sec_low > mf.sec_low)
mf.sec_low = lf.sec_low;
if (rf.max_low != mf.max_low && rf.max_low > mf.sec_low)
mf.sec_low = rf.max_low;
if (rf.sec_low != mf.max_low && rf.sec_low > mf.sec_low)
mf.sec_low = rf.sec_low;
if (lf.max_low == mf.max_low && lf.max_dif > mf.max_dif)
mf.max_dif = lf.max_dif;
if (rf.max_low == mf.max_low && rf.max_dif > mf.max_dif)
mf.max_dif = rf.max_dif;
mf.res = max(lf.res, rf.res);
return mf;
}
node tree[4 * maxn];
ll lazy[4 * maxn];
ll tag[4 * maxn];
void build(ll root, ll left, ll right)
{
lazy[root] = tag[root] = 0;
if (left == right)
{
tree[root] = node();
tree[root].max_dif = 0 - inf;
tree[root].max_low = inf;
tree[root].sec_low = -inf;
tree[root].res = tree[root].max_dif;
return;
}
ll mid = (left + right) / 2;
build(root * 2, left, mid);
build(root * 2 + 1, mid + 1, right);
tree[root] = merge_nodes(tree[root * 2], tree[root * 2 + 1]);
}
void push_lazy(ll root, ll left, ll right)
{
if (tag[root] == 0 || lazy[root] > tree[root].max_low)
{
tag[root] = 0;
lazy[root] = 0;
return;
}
///cout << "push " << root << " " << left << " " << right << " " << lazy[root] << endl;
//cout << "push_lazy " << endl;
ll delta = tree[root].max_low - lazy[root];
tree[root].max_dif += delta;
tree[root].res = max(tree[root].res, tree[root].max_dif);
tree[root].max_low = lazy[root];
if (left != right)
{
lazy[root * 2] = lazy[root];
lazy[root * 2 + 1] = lazy[root];
tag[root * 2] = tag[root * 2 + 1] = 1;
}
tag[root] = 0;
lazy[root] = 0;
}
void chmin(ll root, ll left, ll right, ll qleft, ll qright, ll val)
{
//cout << root << " " << tag[16] << endl;
push_lazy(root, left, right);
if (left > qright || right < qleft || tree[root].max_low < val)
return;
if (left >= qleft && right <= qright && tree[root].sec_low < val)
{
//if (left <= 5 && right >= 5)
//cout << "CHMIN " << left << " " << right << " " << val << endl;
lazy[root] = val;
tag[root] = 1;
push_lazy(root, left, right);
return;
}
ll mid = (left + right) / 2;
chmin(root * 2, left, mid, qleft, qright, val);
chmin(root * 2 + 1, mid + 1, right, qleft, qright, val);
tree[root] = merge_nodes(tree[root * 2], tree[root * 2 + 1]);
}
void point_update(ll root, ll left, ll right, ll pivot, ll val)
{
assert( left <= right);
push_lazy(root, left, right);
if (left == right)
{
tree[root] = node();
tree[root].max_dif = val - inf;
tree[root].max_low = inf;
tree[root].sec_low = -inf;
tree[root].res = tree[root].max_dif;
return;
}
ll mid = (left + right) / 2;
if (pivot <= mid)
{
push_lazy(root * 2 + 1, mid + 1, right);
point_update(root * 2, left, mid, pivot, val);
}
else
{
push_lazy(root * 2, left, mid);
point_update(root * 2 + 1, mid + 1, right, pivot, val);
}
tree[root] = merge_nodes(tree[root * 2], tree[root * 2 + 1]);
}
ll point_query(ll root, ll left, ll right, ll pivot)
{
push_lazy(root, left, right);
if (left == right)
return tree[root].res;
ll mid = (left + right) / 2;
if (pivot <= mid)
return point_query(root * 2, left, mid, pivot);
else
return point_query(root * 2 + 1, mid + 1, right, pivot);
}
ll range_query(ll root, ll left, ll right, ll qleft, ll qright)
{
push_lazy(root, left, right);
if (left > qright || right < qleft)
return -inf;
if (left >= qleft && right <= qright)
return tree[root].res;
ll mid = (left + right) / 2;
return max(range_query(root * 2, left, mid, qleft, qright),
range_query(root * 2 + 1, mid + 1, right, qleft, qright));
}
ll values[maxn];
ll temp[4 * maxn];
void update(int root, int left, int right, int pivot, ll val)
{
if (left == right)
{
temp[root] = val;
return;
}
int mid = (left + right) / 2;
if (pivot <= mid)
update(root * 2, left, mid, pivot, val);
else
update(root * 2 + 1, mid + 1, right, pivot, val);
temp[root] = max(temp[root * 2], temp[root * 2 + 1]);
}
void temp_build(int root, int left, int right)
{
if (left == right)
{
temp[root] = -inf;
return;
}
int mid = (left + right) / 2;
temp_build(root * 2, left, mid);
temp_build(root * 2 + 1, mid + 1, right);
temp[root] = max(temp[root * 2], temp[root * 2 + 1]);
}
ll temp_query(int root, int left, int right, int qleft, int qright)
{
if (left > qright || right < qleft)
return -inf;
if (left >= qleft && right <= qright)
return temp[root];
int mid = (left + right) / 2;
return max(temp_query(root * 2, left, mid, qleft, qright),
temp_query(root * 2 + 1, mid + 1, right, qleft, qright));
}
void sweep_line()
{
///cerr << task[49].l << " : " << task[49].r << endl;
for (ll i = 1; i <= n; i ++)
{
low[i] = inf;
act[i] = 0;
values[i] = -inf;
upd[0][i + a[i]].push_back(i);
upd[1][i + b[i]].push_back(i);
}
temp_build(1, 1, n);
build(1, 1, n);
for (ll i = 1; i <= n; i ++)
{
for (ll cur : upd[0][i])
{
//cout << "Activate " << cur << endl;
act[cur] = 1;
point_update(1, 1, n, cur, h[cur]);
}
ll lf = max((ll)1, i - b[i]), rf = max((ll)0, i - a[i]);
chmin(1, 1, n, lf, rf, h[i]);
//if (i == 95)
/**for (int i = 1; i <= n; i ++)
cerr << act[i] << " ";
cerr << endl;
for (int i = 1; i <= n; i ++)
cerr << low[i] << " ";
cerr << endl;*/
//cout << "step " << i << " " << lf << " " << rf << " " << h[i] << endl;
/**for (int j = 5; j <= 5; j ++)
{
ll t1 = point_query(1, 1, n, j);
if (t1 > h[j] - low[j] && h[j] - low[j] >= 0)
{
cout << "STOP!" << endl;
cout << t1 << " " << h[j] - low[j] << " " << j << " " << act[j] << endl;
exit(0);
}
}*/
// cerr << point_query(1 ,1 ,n, j) << " " << h[j] - low[j] << endl;
// cerr << endl;cerr<< "--------------" << endl;}
for (ll cur : upd[1][i])
{
act[cur] = 0;
//cout << "Deactivate " << cur << endl;
update(1, 1, n, cur, point_query(1 ,1, n, cur));
point_update(1, 1, n, cur, 0);
}
for (query cur : spot[i])
{
ans[cur.idx] = max(ans[cur.idx], temp_query(1, 1, n, cur.l, cur.r));
/**for (ll j = cur.l; j <= cur.r; j ++)
{
ans[cur.idx] = max(ans[cur.idx], values[j]);
}*/
ll cs = range_query(1, 1, n, cur.l, cur.r);
/**if (cs == 841326895)
{
cout << "CHANGE HERE" << " " << i << endl;
}*/
ans[cur.idx] = max(ans[cur.idx], cs);
}
}
///cout << "FINISH" << endl;
}
void reverse_data()
{
reverse(h + 1, h + n + 1);
reverse(a + 1, a + n + 1);
reverse(b + 1, b + n + 1);
for (ll i = 1; i <= n; i ++)
{
spot[i].clear();
upd[0][i].clear();
upd[1][i].clear();
}
for (ll i = 1; i <= q; i ++)
{
ll nl = n - task[i].r + 1, nr = n - task[i].l + 1;
///cout << i << " : " << nl << " " << nr << endl;
task[i].l = nl;
task[i].r = nr;
spot[task[i].r].push_back(task[i]);
}
}
void prll()
{
for (ll i = 1; i <= q; i ++)
{
cout << ans[i] << endl;
}
}
void solve()
{
input();
sweep_line();
reverse_data();
sweep_line();
prll();
}
int main()
{
speed();
///freopen("output.txt", "w", stdout);
///freopen("input.txt", "r", stdin);
solve();
return 0;
}
/**
5
10 2 4
1 1 1
2 1 3
1 1 1
100 1 1
1
1 2
111
2342163 25 76
738276997 50 52
1669890 26 40
14902411 56 81
899007094 32 85
422634516 2 71
936109680 79 100
638713463 109 110
119468142 28 104\
713258492 104 107
267306336 1 39
973810399 87 90
835929417 43 86
335127775 12 104
840490095 39 66
459253103 11 104
706538155 4 101
194912428 82 96
949222000 73 96
910118043 85 95
226986709 46 100
827920993 75 83
768390729 46 87
314695927 26 30
230872586 49 82
995156805 60 93
497044719 3 103
827805975 75 83
499889872 95 110
590726845 66 73
607078123 59 99
370425117 26 76
490217622 18 32
431150446 77 89
9267221 13 81
693001694 64 95
866349101 26 93
844742196 6 63
655634427 33 88
829473857 67 92
311419714 15 56
900123935 42 87
665050273 42 59
792230034 12 34
847490718 11 21
177697704 66 81
198679758 24 54
668213379 14 45
45659509 82 91
327099919 21 82
118001173 81 85
738608951 80 101
214209461 38 97
324473845 107 109
879267283 109 109
190386778 43 77
881527758 25 44
18672166 13 21
733749641 73 92
653264399 103 110
870234612 104 106
101340654 33 40
710067113 71 104
616465791 8 14
610725519 6 58
376168562 89 109
93289534 79 107
973278654 55 85
183001646 37 50
70495079 47 89
383449644 76 77
239592436 26 49
561740304 21 27
326305893 94 101
376448990 28 85
558483855 107 110
58797040 8 97
479096372 76 90
435216279 42 87
583841698 92 106
277031377 63 110
56594141 34 38
381128173 40 95
275114013 110 110
716940824 48 85
805968236 44 55
997032121 87 92
958531100 90 104
459788004 39 85
618435405 34 97
833464071 87 99
527989265 92 92
708140049 65 87
707609457 2 78
937680650 71 105
969561745 81 102
960172678 49 65
56883909 7 40
602118268 53 69
980631740 95 101
795277058 19 26
572270576 13 89
440358047 25 79
467566899 16 24
634175510 30 38
673050101 21 60
768461341 54 107
331110483 58 87
218341925 68 77
419836470 30 77
925521790 29 36
1
38 95
*/
# | 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... |