#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 = log2(r - l + 1);
nx[0][i] = min(rmq[h][l], rmq[h][r - (1 << h) + 1]).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 |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
471 ms |
25256 KB |
Output is correct |
3 |
Correct |
390 ms |
25284 KB |
Output is correct |
4 |
Correct |
455 ms |
25248 KB |
Output is correct |
5 |
Correct |
441 ms |
25292 KB |
Output is correct |
6 |
Correct |
388 ms |
25388 KB |
Output is correct |
7 |
Correct |
391 ms |
25432 KB |
Output is correct |
8 |
Correct |
454 ms |
25764 KB |
Output is correct |
9 |
Correct |
407 ms |
25752 KB |
Output is correct |
10 |
Correct |
424 ms |
25760 KB |
Output is correct |
11 |
Correct |
399 ms |
25740 KB |
Output is correct |
12 |
Correct |
298 ms |
25184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
444 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
532 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
444 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
532 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
440 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
540 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
180 ms |
2800 KB |
Output is correct |
20 |
Correct |
195 ms |
2884 KB |
Output is correct |
21 |
Correct |
187 ms |
3072 KB |
Output is correct |
22 |
Correct |
187 ms |
3244 KB |
Output is correct |
23 |
Correct |
176 ms |
3036 KB |
Output is correct |
24 |
Correct |
199 ms |
3040 KB |
Output is correct |
25 |
Correct |
183 ms |
2612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
444 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
532 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
280 KB |
Output is correct |
12 |
Correct |
2 ms |
440 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
440 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
142 ms |
23584 KB |
Output is correct |
20 |
Correct |
107 ms |
22776 KB |
Output is correct |
21 |
Correct |
155 ms |
23536 KB |
Output is correct |
22 |
Correct |
132 ms |
23460 KB |
Output is correct |
23 |
Correct |
126 ms |
23564 KB |
Output is correct |
24 |
Correct |
172 ms |
23552 KB |
Output is correct |
25 |
Correct |
72 ms |
22856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
321 ms |
25280 KB |
Output is correct |
2 |
Correct |
345 ms |
25308 KB |
Output is correct |
3 |
Correct |
462 ms |
25312 KB |
Output is correct |
4 |
Correct |
380 ms |
25716 KB |
Output is correct |
5 |
Correct |
387 ms |
25640 KB |
Output is correct |
6 |
Correct |
361 ms |
25412 KB |
Output is correct |
7 |
Correct |
420 ms |
25420 KB |
Output is correct |
8 |
Correct |
368 ms |
25572 KB |
Output is correct |
9 |
Correct |
143 ms |
23560 KB |
Output is correct |
10 |
Correct |
364 ms |
25076 KB |
Output is correct |
11 |
Correct |
374 ms |
24852 KB |
Output is correct |
12 |
Correct |
374 ms |
25100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
471 ms |
25256 KB |
Output is correct |
3 |
Correct |
390 ms |
25284 KB |
Output is correct |
4 |
Correct |
455 ms |
25248 KB |
Output is correct |
5 |
Correct |
441 ms |
25292 KB |
Output is correct |
6 |
Correct |
388 ms |
25388 KB |
Output is correct |
7 |
Correct |
391 ms |
25432 KB |
Output is correct |
8 |
Correct |
454 ms |
25764 KB |
Output is correct |
9 |
Correct |
407 ms |
25752 KB |
Output is correct |
10 |
Correct |
424 ms |
25760 KB |
Output is correct |
11 |
Correct |
399 ms |
25740 KB |
Output is correct |
12 |
Correct |
298 ms |
25184 KB |
Output is correct |
13 |
Correct |
1 ms |
296 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
3 ms |
444 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
3 ms |
532 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
440 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
540 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
180 ms |
2800 KB |
Output is correct |
32 |
Correct |
195 ms |
2884 KB |
Output is correct |
33 |
Correct |
187 ms |
3072 KB |
Output is correct |
34 |
Correct |
187 ms |
3244 KB |
Output is correct |
35 |
Correct |
176 ms |
3036 KB |
Output is correct |
36 |
Correct |
199 ms |
3040 KB |
Output is correct |
37 |
Correct |
183 ms |
2612 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
280 KB |
Output is correct |
40 |
Correct |
2 ms |
440 KB |
Output is correct |
41 |
Correct |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
3 ms |
440 KB |
Output is correct |
44 |
Correct |
2 ms |
468 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
142 ms |
23584 KB |
Output is correct |
48 |
Correct |
107 ms |
22776 KB |
Output is correct |
49 |
Correct |
155 ms |
23536 KB |
Output is correct |
50 |
Correct |
132 ms |
23460 KB |
Output is correct |
51 |
Correct |
126 ms |
23564 KB |
Output is correct |
52 |
Correct |
172 ms |
23552 KB |
Output is correct |
53 |
Correct |
72 ms |
22856 KB |
Output is correct |
54 |
Correct |
321 ms |
25280 KB |
Output is correct |
55 |
Correct |
345 ms |
25308 KB |
Output is correct |
56 |
Correct |
462 ms |
25312 KB |
Output is correct |
57 |
Correct |
380 ms |
25716 KB |
Output is correct |
58 |
Correct |
387 ms |
25640 KB |
Output is correct |
59 |
Correct |
361 ms |
25412 KB |
Output is correct |
60 |
Correct |
420 ms |
25420 KB |
Output is correct |
61 |
Correct |
368 ms |
25572 KB |
Output is correct |
62 |
Correct |
143 ms |
23560 KB |
Output is correct |
63 |
Correct |
364 ms |
25076 KB |
Output is correct |
64 |
Correct |
374 ms |
24852 KB |
Output is correct |
65 |
Correct |
374 ms |
25100 KB |
Output is correct |
66 |
Correct |
2 ms |
212 KB |
Output is correct |
67 |
Correct |
358 ms |
25328 KB |
Output is correct |
68 |
Correct |
441 ms |
25336 KB |
Output is correct |
69 |
Correct |
415 ms |
25216 KB |
Output is correct |
70 |
Correct |
387 ms |
25344 KB |
Output is correct |
71 |
Correct |
341 ms |
25392 KB |
Output is correct |
72 |
Correct |
395 ms |
25360 KB |
Output is correct |
73 |
Correct |
366 ms |
25844 KB |
Output is correct |
74 |
Correct |
364 ms |
25796 KB |
Output is correct |
75 |
Correct |
413 ms |
25664 KB |
Output is correct |
76 |
Correct |
385 ms |
25652 KB |
Output is correct |
77 |
Correct |
305 ms |
24976 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
3 ms |
468 KB |
Output is correct |
80 |
Correct |
2 ms |
536 KB |
Output is correct |
81 |
Correct |
2 ms |
468 KB |
Output is correct |
82 |
Correct |
2 ms |
440 KB |
Output is correct |
83 |
Correct |
2 ms |
468 KB |
Output is correct |
84 |
Correct |
2 ms |
440 KB |
Output is correct |
85 |
Correct |
2 ms |
468 KB |
Output is correct |
86 |
Correct |
222 ms |
2740 KB |
Output is correct |
87 |
Correct |
185 ms |
2900 KB |
Output is correct |
88 |
Correct |
212 ms |
3140 KB |
Output is correct |
89 |
Correct |
216 ms |
3112 KB |
Output is correct |
90 |
Correct |
190 ms |
2980 KB |
Output is correct |
91 |
Correct |
194 ms |
2952 KB |
Output is correct |
92 |
Correct |
223 ms |
2664 KB |
Output is correct |
93 |
Correct |
155 ms |
23580 KB |
Output is correct |
94 |
Correct |
115 ms |
22776 KB |
Output is correct |
95 |
Correct |
137 ms |
23476 KB |
Output is correct |
96 |
Correct |
158 ms |
23628 KB |
Output is correct |
97 |
Correct |
141 ms |
23452 KB |
Output is correct |
98 |
Correct |
145 ms |
23448 KB |
Output is correct |
99 |
Correct |
90 ms |
22872 KB |
Output is correct |
100 |
Correct |
444 ms |
25352 KB |
Output is correct |
101 |
Correct |
397 ms |
25412 KB |
Output is correct |
102 |
Correct |
369 ms |
25736 KB |
Output is correct |
103 |
Correct |
462 ms |
25012 KB |
Output is correct |
104 |
Correct |
399 ms |
24900 KB |
Output is correct |
105 |
Correct |
356 ms |
25144 KB |
Output is correct |
106 |
Correct |
363 ms |
24736 KB |
Output is correct |
107 |
Correct |
435 ms |
24728 KB |
Output is correct |
108 |
Correct |
370 ms |
25388 KB |
Output is correct |
109 |
Correct |
345 ms |
25556 KB |
Output is correct |
110 |
Correct |
325 ms |
25300 KB |
Output is correct |
111 |
Correct |
390 ms |
25344 KB |
Output is correct |