//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pil pair<int, long long>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vil vector<pil>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 2e5 + 5;
const double eps = 1e-9;
int h[MAXN], a[MAXN], b[MAXN], ans[MAXN];
pair<pii,int> qr[MAXN];
pii seg1[MAXN*4], lazy[MAXN*4];
int seg2[MAXN*4], n;
vi arr[2][MAXN];
void build(int l, int r, int k) {
seg1[k] = {INF, -INF};
lazy[k] = {INF, -INF};
seg2[k] = -1;
if (l == r)
return;
int m = (l + r) / 2;
build(l, m, k*2);
build(m+1, r, k*2+1);
}
void prop(int l, int r, int k) {
if (l != r) {
lazy[k*2].fi = min(lazy[k*2].fi, lazy[k].fi);
lazy[k*2].se = max(lazy[k*2].se, lazy[k].se);
lazy[k*2+1].fi = min(lazy[k*2+1].fi, lazy[k].fi);
lazy[k*2+1].se = max(lazy[k*2+1].se, lazy[k].se);
}
lazy[k] = {INF, -INF};
}
void upd1(int l, int r, int k, int x, pii val) {
if (r < x || x < l) return;
if (lazy[k] != mp(INF, -INF)) {
seg2[k] = max(seg2[k], max(lazy[k].se - seg1[k].fi, seg1[k].se - lazy[k].fi));
prop(l, r, k);
}
if (x <= l && r <= x) {
seg1[k] = val;
return;
}
int m = (l + r) / 2;
upd1(l, m, k*2, x, val);
upd1(m+1, r, k*2+1, x, val);
seg1[k].fi = min(seg1[k*2].fi, seg1[k*2+1].fi);
seg1[k].se = max(seg1[k*2].se, seg1[k*2+1].se);
}
void upd2(int l, int r, int k, int x, int y, int val) {
if (r < x || y < l || y < x) return;
if (lazy[k] != mp(INF, -INF)) {
seg2[k] = max(seg2[k], max(lazy[k].se - seg1[k].fi, seg1[k].se - lazy[k].fi));
prop(l, r, k);
}
if (x <= l && r <= y) {
seg2[k] = max(seg2[k], max(val - seg1[k].fi, seg1[k].se - val));
lazy[k] = mp(val, val);
prop(l, r, k);
return;
}
int m = (l + r) / 2;
upd2(l, m, k*2, x, y, val);
upd2(m+1, r, k*2+1, x, y, val);
seg2[k] = max(seg2[k*2], seg2[k*2+1]);
}
int qry(int l, int r, int k, int x, int y) {
if (r < x || y < l || y < x) return -1;
if (lazy[k] != mp(INF, -INF)) {
seg2[k] = max(seg2[k], max(lazy[k].se - seg1[k].fi, seg1[k].se - lazy[k].fi));
prop(l, r, k);
}
if (x <= l && r <= y)
return seg2[k];
int m = (l + r) / 2;
return max(qry(l, m, k*2, x, y), qry(m+1, r, k*2+1, x, y));
}
void ins(int ind) {
int lo = max(1, ind - b[ind]), hi = max(0, ind - a[ind]);
for (int nx: arr[0][ind])
upd1(1, n, 1, nx, mp(h[nx], h[nx]));
for (int nx: arr[1][ind])
upd1(1, n, 1, nx, mp(INF, -INF));
upd2(1, n, 1, lo, hi, h[ind]);
lo = min(n + 1, ind + a[ind]);
hi = min(n, ind + b[ind]);
arr[0][lo].pb(ind);
arr[1][hi + 1].pb(ind);
}
int main() {
int q;
ni(n);
build(1, n, 1);
for (int i = 1; i <= n; i++)
scanf("%d %d %d", &h[i], &a[i], &b[i]);
ni(q);
for (int i = 1; i <= q; i++) {
scanf("%d %d", &qr[i].fi.fi, &qr[i].fi.se);
qr[i].se = i;
}
sort(qr+1, qr+1+ q, [](pair<pii,int> l, pair<pii,int> r) {
if (l.fi.se == r.fi.se)
return l.fi.fi < r.fi.fi;
return l.fi.se < r.fi.se;
});
int cur = 1;
for (int i = 1; i <= q; i++) {
while (cur <= qr[i].fi.se)
ins(cur++);
ans[qr[i].se] = qry(1, n, 1, qr[i].fi.fi, qr[i].fi.se);
}
for (int i = 1; i <= q; i++)
pri(ans[i]);
return 0;
}
Compilation message
antennas.cpp: In function 'int main()':
antennas.cpp:8:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define ni(n) scanf("%d", &(n))
~~~~~^~~~~~~~~~~~
antennas.cpp:125:2: note: in expansion of macro 'ni'
ni(n);
^~
antennas.cpp:128:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &h[i], &a[i], &b[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:8:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
#define ni(n) scanf("%d", &(n))
~~~~~^~~~~~~~~~~~
antennas.cpp:129:2: note: in expansion of macro 'ni'
ni(q);
^~
antennas.cpp:131:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &qr[i].fi.fi, &qr[i].fi.se);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
9728 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
9728 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
376 ms |
26968 KB |
Output is correct |
2 |
Correct |
413 ms |
32188 KB |
Output is correct |
3 |
Correct |
296 ms |
28272 KB |
Output is correct |
4 |
Correct |
428 ms |
32124 KB |
Output is correct |
5 |
Incorrect |
146 ms |
20340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
9728 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |