#include "bits/stdc++.h"
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
#define endl '\n'
namespace speedy_io {
int read_nonneg() {
int c;
while ((c = getchar_unlocked()) < '0' || c > '9');
int v = c - '0';
while ((c = getchar_unlocked()) >= '0' && c <= '9') {
v = (v << 3) + (v << 1) + (c - '0');
}
return v;
}
const int max_ll_size = 20;
void put_nonneg(ll n) {
int i = max_ll_size;
char output_buffer[max_ll_size];
do {
output_buffer[--i] = (n % 10) + '0';
n /= 10;
} while (n);
do {
putchar_unlocked(output_buffer[i]);
} while (++i < max_ll_size);
}
}
using namespace speedy_io;
const ll INF = 1e15;
const int MXN = 2e5;
struct Node {
ll ans = -INF, mn = INF;
};
Node tree[4*MXN];
class SegmentTree {
public:
int n;
vector<ll> lazy;
void push(int &p, int &c1, int &c2) {
lazy[c1] = max(lazy[c1], lazy[p]);
lazy[c2] = max(lazy[c2], lazy[p]);
tree[c1].ans = max(tree[c1].ans, lazy[c1]-tree[c1].mn);
tree[c2].ans = max(tree[c2].ans, lazy[c2]-tree[c2].mn);
lazy[p] = -INF;
}
void updateMin(int i, ll val, int l, int r, int p) {
if (l > i or r < i) return;
if (l == r) {
lazy[p] = -INF;
tree[p].mn = val;
return;
}
int mid = (l + r) / 2;
int c1 = 2*p+1, c2 = 2*p+2;
if (lazy[p] != -INF) push(p, c1, c2);
updateMin(i, val, l, mid, c1); updateMin(i, val, mid+1, r, c2);
tree[p] = {max(tree[c1].ans, tree[c2].ans), min(tree[c1].mn, tree[c2].mn)};
}
void updateRangeMax(int i, int j, ll val, int l, int r, int p) {
if (r < i or l > j) return;
if (l >= i and r <= j) {
lazy[p] = max(lazy[p], val);
tree[p].ans = max(tree[p].ans, lazy[p]-tree[p].mn);
return;
}
int mid = (l + r) / 2;
int c1 = 2*p+1, c2 = 2*p+2;
if (lazy[p] != -INF) push(p, c1, c2);
updateRangeMax(i, j, val, l, mid, c1); updateRangeMax(i, j, val, mid+1, r, c2);
tree[p] = {max(tree[c1].ans, tree[c2].ans), min(tree[c1].mn, tree[c2].mn)};
}
ll ans(int i, int j, int l, int r, int p) {
if (l > j or r < i) return -INF;
if (l >= i and r <= j) return tree[p].ans;
int mid = (l + r) / 2;
int c1 = 2*p+1, c2 = 2*p+2;
if (lazy[p] != -INF) push(p, c1, c2);
return max(ans(i, j, l, mid, c1), ans(i, j, mid+1, r, c2));
}
SegmentTree(int N) {
n = N;
for_(i, 0, 4*n) tree[i] = {-INF, INF};
lazy.resize(4*n, -INF);
}
};
int n, q;
vector<array<int, 3>> nums; // {height, a, b}
vector<ii> qrs;
vector<ll> ans;
void solve() {
SegmentTree tree(n);
vector<vector<array<int, 2>>> qrLeft(n);
for_(i, 0, q) qrLeft[qrs[i][1]].push_back({qrs[i][0], i});
vector<vi> enter(n), bye(n);
for_(i, 0, n) {
if (i+nums[i][1] < n) enter[i+nums[i][1]].push_back(i);
if (i+nums[i][1] < n-1) bye[min(i+nums[i][2], n-1)].push_back(i);
}
for_(i, 0, n) {
for (auto &e: enter[i]) tree.updateMin(e, nums[e][0], 0, n-1, 0);
if (i-nums[i][1] >= 0) tree.updateRangeMax(max(i-nums[i][2], 0), i-nums[i][1], nums[i][0], 0, n-1, 0);
for (auto &l: qrLeft[i]) ans[l[1]] = max(ans[l[1]], tree.ans(l[0], i, 0, n-1, 0));
for (auto &e: bye[i]) tree.updateMin(e, INF, 0, n-1, 0);
}
}
int main() {
#ifdef mlocal
freopen("test.in", "r", stdin);
#endif
// ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
nums.resize(n);
for_(i, 0, n) for_(j, 0, 3) nums[i][j] = read_nonneg();
cin >> q;
qrs.resize(q); ans.resize(q, -1);
for_(i, 0, q) {
cin >> qrs[i][0] >> qrs[i][1];
qrs[i][0] -= 1; qrs[i][1] -= 1;
}
solve();
reverse(nums.begin(), nums.end());
for_(i, 0, q) {
qrs[i][0] = n-1-qrs[i][0];
qrs[i][1] = n-1-qrs[i][1];
swap(qrs[i][0], qrs[i][1]);
}
solve();
for (auto i: ans) {
if (i == -1) cout << -1;
else put_nonneg(i);
cout << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
134 ms |
4716 KB |
Output is correct |
26 |
Correct |
19 ms |
1388 KB |
Output is correct |
27 |
Correct |
199 ms |
6856 KB |
Output is correct |
28 |
Correct |
204 ms |
7652 KB |
Output is correct |
29 |
Correct |
133 ms |
4988 KB |
Output is correct |
30 |
Correct |
139 ms |
5856 KB |
Output is correct |
31 |
Correct |
157 ms |
5904 KB |
Output is correct |
32 |
Correct |
204 ms |
7864 KB |
Output is correct |
33 |
Correct |
184 ms |
6240 KB |
Output is correct |
34 |
Correct |
104 ms |
4372 KB |
Output is correct |
35 |
Correct |
195 ms |
6772 KB |
Output is correct |
36 |
Correct |
202 ms |
7776 KB |
Output is correct |
37 |
Correct |
118 ms |
4480 KB |
Output is correct |
38 |
Correct |
207 ms |
6508 KB |
Output is correct |
39 |
Correct |
30 ms |
1644 KB |
Output is correct |
40 |
Correct |
205 ms |
6252 KB |
Output is correct |
41 |
Correct |
148 ms |
5264 KB |
Output is correct |
42 |
Correct |
206 ms |
6252 KB |
Output is correct |
43 |
Correct |
69 ms |
2976 KB |
Output is correct |
44 |
Correct |
201 ms |
6252 KB |
Output is correct |
45 |
Correct |
37 ms |
1892 KB |
Output is correct |
46 |
Correct |
203 ms |
6252 KB |
Output is correct |
47 |
Correct |
54 ms |
2412 KB |
Output is correct |
48 |
Correct |
207 ms |
6380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
501 ms |
35552 KB |
Output is correct |
2 |
Correct |
570 ms |
39336 KB |
Output is correct |
3 |
Correct |
380 ms |
27796 KB |
Output is correct |
4 |
Correct |
558 ms |
39352 KB |
Output is correct |
5 |
Correct |
215 ms |
18284 KB |
Output is correct |
6 |
Correct |
558 ms |
39356 KB |
Output is correct |
7 |
Correct |
472 ms |
34152 KB |
Output is correct |
8 |
Correct |
574 ms |
39280 KB |
Output is correct |
9 |
Correct |
62 ms |
6508 KB |
Output is correct |
10 |
Correct |
560 ms |
39340 KB |
Output is correct |
11 |
Correct |
313 ms |
24752 KB |
Output is correct |
12 |
Correct |
561 ms |
39272 KB |
Output is correct |
13 |
Correct |
309 ms |
38308 KB |
Output is correct |
14 |
Correct |
290 ms |
38012 KB |
Output is correct |
15 |
Correct |
273 ms |
37056 KB |
Output is correct |
16 |
Correct |
264 ms |
37356 KB |
Output is correct |
17 |
Correct |
321 ms |
38692 KB |
Output is correct |
18 |
Correct |
296 ms |
37404 KB |
Output is correct |
19 |
Correct |
292 ms |
37744 KB |
Output is correct |
20 |
Correct |
292 ms |
37836 KB |
Output is correct |
21 |
Correct |
286 ms |
38192 KB |
Output is correct |
22 |
Correct |
294 ms |
37896 KB |
Output is correct |
23 |
Correct |
291 ms |
38040 KB |
Output is correct |
24 |
Correct |
248 ms |
36860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
134 ms |
4716 KB |
Output is correct |
26 |
Correct |
19 ms |
1388 KB |
Output is correct |
27 |
Correct |
199 ms |
6856 KB |
Output is correct |
28 |
Correct |
204 ms |
7652 KB |
Output is correct |
29 |
Correct |
133 ms |
4988 KB |
Output is correct |
30 |
Correct |
139 ms |
5856 KB |
Output is correct |
31 |
Correct |
157 ms |
5904 KB |
Output is correct |
32 |
Correct |
204 ms |
7864 KB |
Output is correct |
33 |
Correct |
184 ms |
6240 KB |
Output is correct |
34 |
Correct |
104 ms |
4372 KB |
Output is correct |
35 |
Correct |
195 ms |
6772 KB |
Output is correct |
36 |
Correct |
202 ms |
7776 KB |
Output is correct |
37 |
Correct |
118 ms |
4480 KB |
Output is correct |
38 |
Correct |
207 ms |
6508 KB |
Output is correct |
39 |
Correct |
30 ms |
1644 KB |
Output is correct |
40 |
Correct |
205 ms |
6252 KB |
Output is correct |
41 |
Correct |
148 ms |
5264 KB |
Output is correct |
42 |
Correct |
206 ms |
6252 KB |
Output is correct |
43 |
Correct |
69 ms |
2976 KB |
Output is correct |
44 |
Correct |
201 ms |
6252 KB |
Output is correct |
45 |
Correct |
37 ms |
1892 KB |
Output is correct |
46 |
Correct |
203 ms |
6252 KB |
Output is correct |
47 |
Correct |
54 ms |
2412 KB |
Output is correct |
48 |
Correct |
207 ms |
6380 KB |
Output is correct |
49 |
Correct |
501 ms |
35552 KB |
Output is correct |
50 |
Correct |
570 ms |
39336 KB |
Output is correct |
51 |
Correct |
380 ms |
27796 KB |
Output is correct |
52 |
Correct |
558 ms |
39352 KB |
Output is correct |
53 |
Correct |
215 ms |
18284 KB |
Output is correct |
54 |
Correct |
558 ms |
39356 KB |
Output is correct |
55 |
Correct |
472 ms |
34152 KB |
Output is correct |
56 |
Correct |
574 ms |
39280 KB |
Output is correct |
57 |
Correct |
62 ms |
6508 KB |
Output is correct |
58 |
Correct |
560 ms |
39340 KB |
Output is correct |
59 |
Correct |
313 ms |
24752 KB |
Output is correct |
60 |
Correct |
561 ms |
39272 KB |
Output is correct |
61 |
Correct |
309 ms |
38308 KB |
Output is correct |
62 |
Correct |
290 ms |
38012 KB |
Output is correct |
63 |
Correct |
273 ms |
37056 KB |
Output is correct |
64 |
Correct |
264 ms |
37356 KB |
Output is correct |
65 |
Correct |
321 ms |
38692 KB |
Output is correct |
66 |
Correct |
296 ms |
37404 KB |
Output is correct |
67 |
Correct |
292 ms |
37744 KB |
Output is correct |
68 |
Correct |
292 ms |
37836 KB |
Output is correct |
69 |
Correct |
286 ms |
38192 KB |
Output is correct |
70 |
Correct |
294 ms |
37896 KB |
Output is correct |
71 |
Correct |
291 ms |
38040 KB |
Output is correct |
72 |
Correct |
248 ms |
36860 KB |
Output is correct |
73 |
Correct |
852 ms |
42676 KB |
Output is correct |
74 |
Correct |
616 ms |
40224 KB |
Output is correct |
75 |
Correct |
805 ms |
36856 KB |
Output is correct |
76 |
Correct |
1063 ms |
48800 KB |
Output is correct |
77 |
Correct |
515 ms |
24784 KB |
Output is correct |
78 |
Correct |
898 ms |
45820 KB |
Output is correct |
79 |
Correct |
952 ms |
43324 KB |
Output is correct |
80 |
Correct |
1071 ms |
48544 KB |
Output is correct |
81 |
Correct |
353 ms |
12232 KB |
Output is correct |
82 |
Correct |
802 ms |
44216 KB |
Output is correct |
83 |
Correct |
739 ms |
33564 KB |
Output is correct |
84 |
Correct |
1058 ms |
48672 KB |
Output is correct |
85 |
Correct |
600 ms |
43604 KB |
Output is correct |
86 |
Correct |
769 ms |
46180 KB |
Output is correct |
87 |
Correct |
374 ms |
38552 KB |
Output is correct |
88 |
Correct |
787 ms |
44984 KB |
Output is correct |
89 |
Correct |
730 ms |
44700 KB |
Output is correct |
90 |
Correct |
806 ms |
45464 KB |
Output is correct |
91 |
Correct |
477 ms |
40756 KB |
Output is correct |
92 |
Correct |
826 ms |
45864 KB |
Output is correct |
93 |
Correct |
382 ms |
39856 KB |
Output is correct |
94 |
Correct |
835 ms |
45940 KB |
Output is correct |
95 |
Correct |
437 ms |
40420 KB |
Output is correct |
96 |
Correct |
746 ms |
45012 KB |
Output is correct |