#include <bits/stdc++.h>
#define st first
#define nd second
using namespace std;
const int LG = 17;
int main() {
int n, q; cin >> n >> q;
vector<vector<int>> nx(LG);
vector<vector<pair<int, int>>> rmq(LG);
for (int i = 0; i < LG; 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].st >> a[i].nd;
b.push_back(a[i].nd);
}
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].nd) - b.begin();
rmq[0][r] = min(rmq[0][r], {a[i].st, i});
}
for (int i = 0; i + 1 < LG; 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].st) - b.begin();
int r = lower_bound(b.begin(), b.end(), a[i].nd) - b.begin();
int h = 31 - __builtin_clz(r - l + 1);
nx[0][i] = min(rmq[h][l], rmq[h][r + 1 - (1 << h)]).nd;
}
for (int i = 0; i + 1 < LG; 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--;
if (x == y || (a[y].st <= a[x].nd && a[x].nd <= a[y].nd)) {
cout << (x == y ? 0 : 1) << "\n";
continue;
}
for (int i = LG - 1; ~i; i--) {
if (a[x].nd < a[nx[i][y]].st)
l += 1 << i, y = nx[i][y];
}
y = nx[0][y];
if (a[x].nd < a[y].st || a[x].nd > a[y].nd)
cout << "impossible\n";
else
cout << l + 1 << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
260 ms |
22308 KB |
Output is correct |
3 |
Correct |
263 ms |
22140 KB |
Output is correct |
4 |
Correct |
288 ms |
22220 KB |
Output is correct |
5 |
Correct |
279 ms |
22212 KB |
Output is correct |
6 |
Correct |
276 ms |
22220 KB |
Output is correct |
7 |
Correct |
281 ms |
22296 KB |
Output is correct |
8 |
Correct |
276 ms |
22732 KB |
Output is correct |
9 |
Correct |
282 ms |
22820 KB |
Output is correct |
10 |
Correct |
277 ms |
22476 KB |
Output is correct |
11 |
Correct |
284 ms |
22660 KB |
Output is correct |
12 |
Correct |
226 ms |
22856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
600 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 |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
600 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 |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
600 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 |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
161 ms |
1964 KB |
Output is correct |
20 |
Correct |
163 ms |
1872 KB |
Output is correct |
21 |
Correct |
167 ms |
2084 KB |
Output is correct |
22 |
Correct |
158 ms |
2132 KB |
Output is correct |
23 |
Correct |
158 ms |
2108 KB |
Output is correct |
24 |
Correct |
157 ms |
2088 KB |
Output is correct |
25 |
Correct |
159 ms |
2092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
600 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 |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 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 |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
101 ms |
21776 KB |
Output is correct |
20 |
Correct |
84 ms |
21744 KB |
Output is correct |
21 |
Correct |
95 ms |
21712 KB |
Output is correct |
22 |
Correct |
109 ms |
22484 KB |
Output is correct |
23 |
Correct |
92 ms |
21860 KB |
Output is correct |
24 |
Correct |
102 ms |
21828 KB |
Output is correct |
25 |
Correct |
53 ms |
21720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
270 ms |
22536 KB |
Output is correct |
2 |
Correct |
271 ms |
22320 KB |
Output is correct |
3 |
Correct |
309 ms |
22364 KB |
Output is correct |
4 |
Correct |
274 ms |
22980 KB |
Output is correct |
5 |
Correct |
283 ms |
22732 KB |
Output is correct |
6 |
Correct |
276 ms |
22388 KB |
Output is correct |
7 |
Correct |
279 ms |
22476 KB |
Output is correct |
8 |
Correct |
277 ms |
22476 KB |
Output is correct |
9 |
Correct |
94 ms |
21860 KB |
Output is correct |
10 |
Correct |
287 ms |
21964 KB |
Output is correct |
11 |
Correct |
259 ms |
21944 KB |
Output is correct |
12 |
Correct |
273 ms |
21948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
260 ms |
22308 KB |
Output is correct |
3 |
Correct |
263 ms |
22140 KB |
Output is correct |
4 |
Correct |
288 ms |
22220 KB |
Output is correct |
5 |
Correct |
279 ms |
22212 KB |
Output is correct |
6 |
Correct |
276 ms |
22220 KB |
Output is correct |
7 |
Correct |
281 ms |
22296 KB |
Output is correct |
8 |
Correct |
276 ms |
22732 KB |
Output is correct |
9 |
Correct |
282 ms |
22820 KB |
Output is correct |
10 |
Correct |
277 ms |
22476 KB |
Output is correct |
11 |
Correct |
284 ms |
22660 KB |
Output is correct |
12 |
Correct |
226 ms |
22856 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 |
600 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 |
604 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
600 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 |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
161 ms |
1964 KB |
Output is correct |
32 |
Correct |
163 ms |
1872 KB |
Output is correct |
33 |
Correct |
167 ms |
2084 KB |
Output is correct |
34 |
Correct |
158 ms |
2132 KB |
Output is correct |
35 |
Correct |
158 ms |
2108 KB |
Output is correct |
36 |
Correct |
157 ms |
2088 KB |
Output is correct |
37 |
Correct |
159 ms |
2092 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
101 ms |
21776 KB |
Output is correct |
48 |
Correct |
84 ms |
21744 KB |
Output is correct |
49 |
Correct |
95 ms |
21712 KB |
Output is correct |
50 |
Correct |
109 ms |
22484 KB |
Output is correct |
51 |
Correct |
92 ms |
21860 KB |
Output is correct |
52 |
Correct |
102 ms |
21828 KB |
Output is correct |
53 |
Correct |
53 ms |
21720 KB |
Output is correct |
54 |
Correct |
270 ms |
22536 KB |
Output is correct |
55 |
Correct |
271 ms |
22320 KB |
Output is correct |
56 |
Correct |
309 ms |
22364 KB |
Output is correct |
57 |
Correct |
274 ms |
22980 KB |
Output is correct |
58 |
Correct |
283 ms |
22732 KB |
Output is correct |
59 |
Correct |
276 ms |
22388 KB |
Output is correct |
60 |
Correct |
279 ms |
22476 KB |
Output is correct |
61 |
Correct |
277 ms |
22476 KB |
Output is correct |
62 |
Correct |
94 ms |
21860 KB |
Output is correct |
63 |
Correct |
287 ms |
21964 KB |
Output is correct |
64 |
Correct |
259 ms |
21944 KB |
Output is correct |
65 |
Correct |
273 ms |
21948 KB |
Output is correct |
66 |
Correct |
0 ms |
348 KB |
Output is correct |
67 |
Correct |
263 ms |
22504 KB |
Output is correct |
68 |
Correct |
270 ms |
22348 KB |
Output is correct |
69 |
Correct |
296 ms |
22556 KB |
Output is correct |
70 |
Correct |
276 ms |
22216 KB |
Output is correct |
71 |
Correct |
275 ms |
22372 KB |
Output is correct |
72 |
Correct |
272 ms |
22232 KB |
Output is correct |
73 |
Correct |
285 ms |
22732 KB |
Output is correct |
74 |
Correct |
298 ms |
22960 KB |
Output is correct |
75 |
Correct |
273 ms |
22688 KB |
Output is correct |
76 |
Correct |
309 ms |
22728 KB |
Output is correct |
77 |
Correct |
223 ms |
22732 KB |
Output is correct |
78 |
Correct |
0 ms |
600 KB |
Output is correct |
79 |
Correct |
1 ms |
424 KB |
Output is correct |
80 |
Correct |
2 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 |
1 ms |
604 KB |
Output is correct |
84 |
Correct |
1 ms |
604 KB |
Output is correct |
85 |
Correct |
1 ms |
604 KB |
Output is correct |
86 |
Correct |
167 ms |
1908 KB |
Output is correct |
87 |
Correct |
160 ms |
1856 KB |
Output is correct |
88 |
Correct |
160 ms |
2132 KB |
Output is correct |
89 |
Correct |
161 ms |
2364 KB |
Output is correct |
90 |
Correct |
160 ms |
1908 KB |
Output is correct |
91 |
Correct |
154 ms |
1912 KB |
Output is correct |
92 |
Correct |
158 ms |
1792 KB |
Output is correct |
93 |
Correct |
101 ms |
21756 KB |
Output is correct |
94 |
Correct |
84 ms |
21876 KB |
Output is correct |
95 |
Correct |
101 ms |
21708 KB |
Output is correct |
96 |
Correct |
101 ms |
21712 KB |
Output is correct |
97 |
Correct |
93 ms |
21708 KB |
Output is correct |
98 |
Correct |
104 ms |
21712 KB |
Output is correct |
99 |
Correct |
56 ms |
21708 KB |
Output is correct |
100 |
Correct |
283 ms |
22616 KB |
Output is correct |
101 |
Correct |
279 ms |
22276 KB |
Output is correct |
102 |
Correct |
268 ms |
22472 KB |
Output is correct |
103 |
Correct |
264 ms |
22040 KB |
Output is correct |
104 |
Correct |
256 ms |
21852 KB |
Output is correct |
105 |
Correct |
274 ms |
22064 KB |
Output is correct |
106 |
Correct |
256 ms |
22268 KB |
Output is correct |
107 |
Correct |
291 ms |
22360 KB |
Output is correct |
108 |
Correct |
269 ms |
22476 KB |
Output is correct |
109 |
Correct |
263 ms |
22640 KB |
Output is correct |
110 |
Correct |
270 ms |
22400 KB |
Output is correct |
111 |
Correct |
268 ms |
22220 KB |
Output is correct |