#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
int n, q;
cin >> n >> q; // Số lượng sự kiện và số lượng truy vấn
vector<vector<int>> nx(17);
vector<vector<pair<int, int>>> rmq(17);
for (int i = 0; i < 17; i++) {
nx[i].resize(n);
rmq[i].resize(n, {1'000'000'001, -1});
}
vector<pair<int, int>> a(n);
vector<int> b;
for (int i = 0; i < n; i++) {
cin >> a[i].first >> a[i].second;
b.push_back(a[i].second);
}
sort(b.begin(), b.end());
b.erase(unique(b.begin(), b.end()), b.end());
for (int i = 0; i < n; i++) {
int r = lower_bound(b.begin(), b.end(), a[i].second) - b.begin();
rmq[0][r] = min(rmq[0][r], {a[i].first, i});
}
for (int i = 0; i + 1 < 17; i++) {
for (int j = 0; j + (1 << (i + 1)) <= n; j++) {
rmq[i + 1][j] = min(rmq[i][j], rmq[i][j + (1 << i)]);
}
}
for (int i = 0; i < n; i++) {
int l = lower_bound(b.begin(), b.end(), a[i].first) - b.begin();
int r = lower_bound(b.begin(), b.end(), a[i].second) - b.begin();
int h = 31 - __builtin_clz(r - l + 1);
nx[0][i] = min(rmq[h][l], rmq[h][r + 1 - (1 << h)]).second;
}
for (int i = 0; i + 1 < 17; i++) {
for (int j = 0; j < n; j++) {
nx[i + 1][j] = nx[i][nx[i][j]];
}
}
while (q--) {
int x, y, l = 1;
cin >> x >> y;
x--, y--; // Chuyển về 0-based index
if (x == y || (a[y].first <= a[x].second && a[x].second <= a[y].second)) {
cout << (x == y ? 0 : 1) << "\n";
continue;
}
for (int i = 16; i >= 0; i--) {
if (a[x].second < a[nx[i][y]].first) {
l += 1 << i;
y = nx[i][y];
}
}
y = nx[0][y];
if (a[x].second < a[y].first || a[x].second > a[y].second) {
cout << "impossible\n";
} else {
cout << l + (a[x].second >= a[y].first) << "\n";
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
298 ms |
25420 KB |
Output is correct |
3 |
Correct |
274 ms |
25428 KB |
Output is correct |
4 |
Correct |
317 ms |
25788 KB |
Output is correct |
5 |
Correct |
280 ms |
25472 KB |
Output is correct |
6 |
Correct |
278 ms |
25544 KB |
Output is correct |
7 |
Correct |
283 ms |
25524 KB |
Output is correct |
8 |
Correct |
281 ms |
25904 KB |
Output is correct |
9 |
Correct |
297 ms |
25944 KB |
Output is correct |
10 |
Correct |
277 ms |
25824 KB |
Output is correct |
11 |
Correct |
284 ms |
25924 KB |
Output is correct |
12 |
Correct |
229 ms |
25036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
448 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
448 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
668 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
164 ms |
2884 KB |
Output is correct |
20 |
Correct |
162 ms |
2948 KB |
Output is correct |
21 |
Correct |
176 ms |
3216 KB |
Output is correct |
22 |
Correct |
162 ms |
3296 KB |
Output is correct |
23 |
Correct |
158 ms |
3156 KB |
Output is correct |
24 |
Correct |
157 ms |
2952 KB |
Output is correct |
25 |
Correct |
161 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
448 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
3 ms |
448 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
668 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
448 KB |
Output is correct |
19 |
Correct |
102 ms |
23712 KB |
Output is correct |
20 |
Correct |
86 ms |
22744 KB |
Output is correct |
21 |
Correct |
99 ms |
23496 KB |
Output is correct |
22 |
Correct |
104 ms |
23524 KB |
Output is correct |
23 |
Correct |
94 ms |
23492 KB |
Output is correct |
24 |
Correct |
118 ms |
23520 KB |
Output is correct |
25 |
Correct |
57 ms |
22984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
261 ms |
23388 KB |
Output is correct |
2 |
Correct |
277 ms |
25292 KB |
Output is correct |
3 |
Correct |
289 ms |
25324 KB |
Output is correct |
4 |
Correct |
301 ms |
25804 KB |
Output is correct |
5 |
Correct |
293 ms |
25772 KB |
Output is correct |
6 |
Correct |
293 ms |
25588 KB |
Output is correct |
7 |
Correct |
296 ms |
25708 KB |
Output is correct |
8 |
Correct |
286 ms |
25544 KB |
Output is correct |
9 |
Correct |
95 ms |
23644 KB |
Output is correct |
10 |
Correct |
266 ms |
25032 KB |
Output is correct |
11 |
Correct |
266 ms |
24996 KB |
Output is correct |
12 |
Correct |
276 ms |
25300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
298 ms |
25420 KB |
Output is correct |
3 |
Correct |
274 ms |
25428 KB |
Output is correct |
4 |
Correct |
317 ms |
25788 KB |
Output is correct |
5 |
Correct |
280 ms |
25472 KB |
Output is correct |
6 |
Correct |
278 ms |
25544 KB |
Output is correct |
7 |
Correct |
283 ms |
25524 KB |
Output is correct |
8 |
Correct |
281 ms |
25904 KB |
Output is correct |
9 |
Correct |
297 ms |
25944 KB |
Output is correct |
10 |
Correct |
277 ms |
25824 KB |
Output is correct |
11 |
Correct |
284 ms |
25924 KB |
Output is correct |
12 |
Correct |
229 ms |
25036 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
448 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
668 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
164 ms |
2884 KB |
Output is correct |
32 |
Correct |
162 ms |
2948 KB |
Output is correct |
33 |
Correct |
176 ms |
3216 KB |
Output is correct |
34 |
Correct |
162 ms |
3296 KB |
Output is correct |
35 |
Correct |
158 ms |
3156 KB |
Output is correct |
36 |
Correct |
157 ms |
2952 KB |
Output is correct |
37 |
Correct |
161 ms |
2644 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
3 ms |
448 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
440 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
668 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
448 KB |
Output is correct |
47 |
Correct |
102 ms |
23712 KB |
Output is correct |
48 |
Correct |
86 ms |
22744 KB |
Output is correct |
49 |
Correct |
99 ms |
23496 KB |
Output is correct |
50 |
Correct |
104 ms |
23524 KB |
Output is correct |
51 |
Correct |
94 ms |
23492 KB |
Output is correct |
52 |
Correct |
118 ms |
23520 KB |
Output is correct |
53 |
Correct |
57 ms |
22984 KB |
Output is correct |
54 |
Correct |
261 ms |
23388 KB |
Output is correct |
55 |
Correct |
277 ms |
25292 KB |
Output is correct |
56 |
Correct |
289 ms |
25324 KB |
Output is correct |
57 |
Correct |
301 ms |
25804 KB |
Output is correct |
58 |
Correct |
293 ms |
25772 KB |
Output is correct |
59 |
Correct |
293 ms |
25588 KB |
Output is correct |
60 |
Correct |
296 ms |
25708 KB |
Output is correct |
61 |
Correct |
286 ms |
25544 KB |
Output is correct |
62 |
Correct |
95 ms |
23644 KB |
Output is correct |
63 |
Correct |
266 ms |
25032 KB |
Output is correct |
64 |
Correct |
266 ms |
24996 KB |
Output is correct |
65 |
Correct |
276 ms |
25300 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
289 ms |
25380 KB |
Output is correct |
68 |
Correct |
278 ms |
25652 KB |
Output is correct |
69 |
Correct |
285 ms |
25288 KB |
Output is correct |
70 |
Correct |
301 ms |
25396 KB |
Output is correct |
71 |
Correct |
279 ms |
25292 KB |
Output is correct |
72 |
Correct |
292 ms |
25444 KB |
Output is correct |
73 |
Correct |
293 ms |
25804 KB |
Output is correct |
74 |
Correct |
283 ms |
25944 KB |
Output is correct |
75 |
Correct |
277 ms |
25804 KB |
Output is correct |
76 |
Correct |
289 ms |
25728 KB |
Output is correct |
77 |
Correct |
228 ms |
25152 KB |
Output is correct |
78 |
Correct |
0 ms |
344 KB |
Output is correct |
79 |
Correct |
2 ms |
604 KB |
Output is correct |
80 |
Correct |
1 ms |
604 KB |
Output is correct |
81 |
Correct |
1 ms |
604 KB |
Output is correct |
82 |
Correct |
2 ms |
604 KB |
Output is correct |
83 |
Correct |
2 ms |
600 KB |
Output is correct |
84 |
Correct |
2 ms |
604 KB |
Output is correct |
85 |
Correct |
2 ms |
428 KB |
Output is correct |
86 |
Correct |
161 ms |
2844 KB |
Output is correct |
87 |
Correct |
164 ms |
2932 KB |
Output is correct |
88 |
Correct |
179 ms |
3212 KB |
Output is correct |
89 |
Correct |
160 ms |
3176 KB |
Output is correct |
90 |
Correct |
162 ms |
3152 KB |
Output is correct |
91 |
Correct |
156 ms |
2960 KB |
Output is correct |
92 |
Correct |
161 ms |
2828 KB |
Output is correct |
93 |
Correct |
105 ms |
23716 KB |
Output is correct |
94 |
Correct |
88 ms |
22732 KB |
Output is correct |
95 |
Correct |
108 ms |
23752 KB |
Output is correct |
96 |
Correct |
106 ms |
23500 KB |
Output is correct |
97 |
Correct |
95 ms |
23496 KB |
Output is correct |
98 |
Correct |
102 ms |
23504 KB |
Output is correct |
99 |
Correct |
55 ms |
22984 KB |
Output is correct |
100 |
Correct |
286 ms |
25416 KB |
Output is correct |
101 |
Correct |
276 ms |
25548 KB |
Output is correct |
102 |
Correct |
270 ms |
25588 KB |
Output is correct |
103 |
Correct |
302 ms |
25244 KB |
Output is correct |
104 |
Correct |
267 ms |
25320 KB |
Output is correct |
105 |
Correct |
294 ms |
25056 KB |
Output is correct |
106 |
Correct |
287 ms |
25084 KB |
Output is correct |
107 |
Correct |
269 ms |
24776 KB |
Output is correct |
108 |
Correct |
269 ms |
25404 KB |
Output is correct |
109 |
Correct |
283 ms |
25504 KB |
Output is correct |
110 |
Correct |
268 ms |
25376 KB |
Output is correct |
111 |
Correct |
290 ms |
25292 KB |
Output is correct |