#include<bits/stdc++.h>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 2e5 + 10;
int h[N], a[N], b[N], T[N], mx[N], mn[N], ans[N];
vector<ii> query[N], event[N];
int n;
void push(int s, int l, int r) {
T[s] = max(T[s], mx[s] - mn[s]);
if (l != r) {
mx[s << 1] = max(mx[s << 1], mx[s]);
mx[s << 1 | 1] = max(mx[s << 1 | 1], mx[s]);
}
mx[s] = 0;
}
void upval (int s, int l, int r, int pos, int val) {
push(s, l, r);
if (l == r) {
mn[s] = val;
return;
}
int mid = l + r >> 1;
if (pos <= mid) upval(s << 1, l, mid, pos, val);
else upval(s << 1 | 1, mid + 1, r, pos, val);
mn[s] = min(mn[s << 1], mn[s << 1 | 1]);
}
void upans (int s, int l, int r, int from, int to, int val) {
push(s, l, r);
if (l > to || r < from) return;
if (from <= l && r <= to) {
mx[s] = val;
push(s, l, r);
return;
}
int mid = l + r >> 1;
upans(s << 1, l, mid, from, to, val);
upans(s << 1 | 1, mid + 1, r, from, to, val);
T[s] = max(T[s << 1], T[s << 1 | 1]);
}
int get(int s, int l, int r, int from, int to) {
push(s, l, r);
if (l > to || r < from) return -1;
if (from <= l && r <= to) return T[s];
int mid = l + r >> 1;
return max(get(s << 1, l, mid, from, to), get(s << 1 | 1, mid + 1, r, from, to));
}
void solve() {
for(int i = 1; i <= 4 * n; i++) T[i] = -1, mx[i] = 0, mn[i] = 1e9;
for(int i = 1; i <= n; i++) {
if (i + a[i] <= n) event[i + a[i]].emplace_back(i, h[i]);
if (i + b[i] + 1 <= n) event[i + b[i] + 1].emplace_back(i, 1e9);
}
for(int r = 1; r <= n; r++) {
for(auto &[pos, val] : event[r]) upval(1, 1, n, pos, val);
upans(1, 1, n, r - b[r], r - a[r], h[r]);
for(auto &[l, idx] : query[r]) ans[idx] = max(ans[idx], get(1, 1, n, l, n));
event[r].clear();
}
}
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
cin >> n;
for(int i = 1; i <= n; i++) cin >> h[i] >> a[i] >> b[i];
int q; cin >> q;
for(int i = 1; i <= q; i++) {
int l, r; cin >> l >> r;
query[r].emplace_back(l, i);
ans[i] = -1;
}
solve();
for(int i = 1; i <= n; i++) h[i] = 1e9 - h[i];
solve();
for(int i = 1; i <= q; i++) cout << ans[i] << endl;
}
Compilation message
antennas.cpp: In function 'void upval(int, int, int, int, int)':
antennas.cpp:25:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | int mid = l + r >> 1;
| ~~^~~
antennas.cpp: In function 'void upans(int, int, int, int, int, int)':
antennas.cpp:38:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid = l + r >> 1;
| ~~^~~
antennas.cpp: In function 'int get(int, int, int, int, int)':
antennas.cpp:47:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int mid = l + r >> 1;
| ~~^~~
antennas.cpp: In function 'int main()':
antennas.cpp:68:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | freopen ("task.inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:69:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | freopen ("task.out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:72:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen (task".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:73:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen (task".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9736 KB |
Output is correct |
2 |
Correct |
8 ms |
9736 KB |
Output is correct |
3 |
Correct |
7 ms |
9672 KB |
Output is correct |
4 |
Correct |
8 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9736 KB |
Output is correct |
7 |
Correct |
8 ms |
9776 KB |
Output is correct |
8 |
Correct |
8 ms |
9732 KB |
Output is correct |
9 |
Correct |
7 ms |
9684 KB |
Output is correct |
10 |
Correct |
7 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
8 ms |
9680 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
8 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
8 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
19 |
Correct |
6 ms |
9728 KB |
Output is correct |
20 |
Correct |
6 ms |
9684 KB |
Output is correct |
21 |
Correct |
7 ms |
9732 KB |
Output is correct |
22 |
Correct |
6 ms |
9728 KB |
Output is correct |
23 |
Correct |
7 ms |
9728 KB |
Output is correct |
24 |
Correct |
6 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9736 KB |
Output is correct |
2 |
Correct |
8 ms |
9736 KB |
Output is correct |
3 |
Correct |
7 ms |
9672 KB |
Output is correct |
4 |
Correct |
8 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9736 KB |
Output is correct |
7 |
Correct |
8 ms |
9776 KB |
Output is correct |
8 |
Correct |
8 ms |
9732 KB |
Output is correct |
9 |
Correct |
7 ms |
9684 KB |
Output is correct |
10 |
Correct |
7 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
8 ms |
9680 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
8 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
8 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
19 |
Correct |
6 ms |
9728 KB |
Output is correct |
20 |
Correct |
6 ms |
9684 KB |
Output is correct |
21 |
Correct |
7 ms |
9732 KB |
Output is correct |
22 |
Correct |
6 ms |
9728 KB |
Output is correct |
23 |
Correct |
7 ms |
9728 KB |
Output is correct |
24 |
Correct |
6 ms |
9684 KB |
Output is correct |
25 |
Correct |
83 ms |
14600 KB |
Output is correct |
26 |
Correct |
20 ms |
10456 KB |
Output is correct |
27 |
Correct |
161 ms |
16600 KB |
Output is correct |
28 |
Correct |
145 ms |
16676 KB |
Output is correct |
29 |
Correct |
87 ms |
14540 KB |
Output is correct |
30 |
Correct |
88 ms |
14400 KB |
Output is correct |
31 |
Correct |
105 ms |
15692 KB |
Output is correct |
32 |
Correct |
127 ms |
16664 KB |
Output is correct |
33 |
Correct |
138 ms |
16152 KB |
Output is correct |
34 |
Correct |
63 ms |
13056 KB |
Output is correct |
35 |
Correct |
127 ms |
16588 KB |
Output is correct |
36 |
Correct |
158 ms |
16664 KB |
Output is correct |
37 |
Correct |
68 ms |
13264 KB |
Output is correct |
38 |
Correct |
116 ms |
15748 KB |
Output is correct |
39 |
Correct |
38 ms |
10656 KB |
Output is correct |
40 |
Correct |
125 ms |
15632 KB |
Output is correct |
41 |
Correct |
91 ms |
14116 KB |
Output is correct |
42 |
Correct |
116 ms |
15884 KB |
Output is correct |
43 |
Correct |
44 ms |
11732 KB |
Output is correct |
44 |
Correct |
120 ms |
15776 KB |
Output is correct |
45 |
Correct |
26 ms |
10828 KB |
Output is correct |
46 |
Correct |
121 ms |
15676 KB |
Output is correct |
47 |
Correct |
54 ms |
11328 KB |
Output is correct |
48 |
Correct |
179 ms |
15776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
318 ms |
24024 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9736 KB |
Output is correct |
2 |
Correct |
8 ms |
9736 KB |
Output is correct |
3 |
Correct |
7 ms |
9672 KB |
Output is correct |
4 |
Correct |
8 ms |
9684 KB |
Output is correct |
5 |
Correct |
6 ms |
9684 KB |
Output is correct |
6 |
Correct |
5 ms |
9736 KB |
Output is correct |
7 |
Correct |
8 ms |
9776 KB |
Output is correct |
8 |
Correct |
8 ms |
9732 KB |
Output is correct |
9 |
Correct |
7 ms |
9684 KB |
Output is correct |
10 |
Correct |
7 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
8 ms |
9680 KB |
Output is correct |
13 |
Correct |
6 ms |
9684 KB |
Output is correct |
14 |
Correct |
8 ms |
9684 KB |
Output is correct |
15 |
Correct |
6 ms |
9684 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
8 ms |
9684 KB |
Output is correct |
18 |
Correct |
6 ms |
9684 KB |
Output is correct |
19 |
Correct |
6 ms |
9728 KB |
Output is correct |
20 |
Correct |
6 ms |
9684 KB |
Output is correct |
21 |
Correct |
7 ms |
9732 KB |
Output is correct |
22 |
Correct |
6 ms |
9728 KB |
Output is correct |
23 |
Correct |
7 ms |
9728 KB |
Output is correct |
24 |
Correct |
6 ms |
9684 KB |
Output is correct |
25 |
Correct |
83 ms |
14600 KB |
Output is correct |
26 |
Correct |
20 ms |
10456 KB |
Output is correct |
27 |
Correct |
161 ms |
16600 KB |
Output is correct |
28 |
Correct |
145 ms |
16676 KB |
Output is correct |
29 |
Correct |
87 ms |
14540 KB |
Output is correct |
30 |
Correct |
88 ms |
14400 KB |
Output is correct |
31 |
Correct |
105 ms |
15692 KB |
Output is correct |
32 |
Correct |
127 ms |
16664 KB |
Output is correct |
33 |
Correct |
138 ms |
16152 KB |
Output is correct |
34 |
Correct |
63 ms |
13056 KB |
Output is correct |
35 |
Correct |
127 ms |
16588 KB |
Output is correct |
36 |
Correct |
158 ms |
16664 KB |
Output is correct |
37 |
Correct |
68 ms |
13264 KB |
Output is correct |
38 |
Correct |
116 ms |
15748 KB |
Output is correct |
39 |
Correct |
38 ms |
10656 KB |
Output is correct |
40 |
Correct |
125 ms |
15632 KB |
Output is correct |
41 |
Correct |
91 ms |
14116 KB |
Output is correct |
42 |
Correct |
116 ms |
15884 KB |
Output is correct |
43 |
Correct |
44 ms |
11732 KB |
Output is correct |
44 |
Correct |
120 ms |
15776 KB |
Output is correct |
45 |
Correct |
26 ms |
10828 KB |
Output is correct |
46 |
Correct |
121 ms |
15676 KB |
Output is correct |
47 |
Correct |
54 ms |
11328 KB |
Output is correct |
48 |
Correct |
179 ms |
15776 KB |
Output is correct |
49 |
Incorrect |
318 ms |
24024 KB |
Output isn't correct |
50 |
Halted |
0 ms |
0 KB |
- |