#include <bits/stdc++.h>
using namespace std;
int main() {
int n, q;
cin >> n >> q;
vector<vector<int> > arr(17, vector<int>(n));
vector<vector<pair<int, int> > > sparseTables(17, vector<pair<int, int> >(n, make_pair(1000000001, -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 right = lower_bound(b.begin(), b.end(), a[i].second) - b.begin();
sparseTables[0][right] = min(sparseTables[0][right], make_pair(a[i].first, i));
}
for (int i = 0; i + 1 < 17; i++){
for (int j = 0; j + (1 << (i + 1)) <= n; j++){
sparseTables[i + 1][j] = min(sparseTables[i][j], sparseTables[i][j + (1 << i)]);
}
}
for (int i = 0; i < n; i++){
int left = lower_bound(b.begin(), b.end(), a[i].first) - b.begin();
int right = lower_bound(b.begin(), b.end(), a[i].second) - b.begin();
int h = 31 - __builtin_clz(right - left + 1);
arr[0][i] = min(sparseTables[h][left], sparseTables[h][right + 1 - (1 << h)]).second;
}
for (int i = 0; i + 1 < 17; i++){
for (int j = 0; j < n; j++){
arr[i + 1][j] = arr[i][arr[i][j]];
}
}
while (q--) {
int x, y;
int left = 1;
cin >> x >> y;
x--;
y--;
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 = 17 - 1; ~i; i--) {
if (a[x].second < a[arr[i][y]].first){
left += 1 << i, y = arr[i][y];
}
}
y = arr[0][y];
if (a[x].second < a[y].first || a[x].second > a[y].second){
cout << "impossible\n";
}
else{
cout << left + (a[x].second >= a[y].first) << "\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
353 ms |
25736 KB |
Output is correct |
3 |
Correct |
332 ms |
25692 KB |
Output is correct |
4 |
Correct |
372 ms |
25664 KB |
Output is correct |
5 |
Correct |
329 ms |
25696 KB |
Output is correct |
6 |
Correct |
354 ms |
25788 KB |
Output is correct |
7 |
Correct |
329 ms |
25856 KB |
Output is correct |
8 |
Correct |
369 ms |
26180 KB |
Output is correct |
9 |
Correct |
335 ms |
26092 KB |
Output is correct |
10 |
Correct |
365 ms |
26096 KB |
Output is correct |
11 |
Correct |
350 ms |
26084 KB |
Output is correct |
12 |
Correct |
284 ms |
25440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
528 KB |
Output is correct |
13 |
Correct |
2 ms |
468 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 |
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 |
184 ms |
2824 KB |
Output is correct |
20 |
Correct |
198 ms |
2896 KB |
Output is correct |
21 |
Correct |
187 ms |
3112 KB |
Output is correct |
22 |
Correct |
180 ms |
3180 KB |
Output is correct |
23 |
Correct |
178 ms |
2976 KB |
Output is correct |
24 |
Correct |
224 ms |
3028 KB |
Output is correct |
25 |
Correct |
174 ms |
2672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
1 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 |
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 |
137 ms |
23884 KB |
Output is correct |
20 |
Correct |
107 ms |
23164 KB |
Output is correct |
21 |
Correct |
125 ms |
23792 KB |
Output is correct |
22 |
Correct |
134 ms |
23960 KB |
Output is correct |
23 |
Correct |
122 ms |
23944 KB |
Output is correct |
24 |
Correct |
141 ms |
23952 KB |
Output is correct |
25 |
Correct |
84 ms |
23284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
308 ms |
25732 KB |
Output is correct |
2 |
Correct |
342 ms |
25660 KB |
Output is correct |
3 |
Correct |
408 ms |
25736 KB |
Output is correct |
4 |
Correct |
344 ms |
26160 KB |
Output is correct |
5 |
Correct |
346 ms |
26008 KB |
Output is correct |
6 |
Correct |
352 ms |
25780 KB |
Output is correct |
7 |
Correct |
388 ms |
25828 KB |
Output is correct |
8 |
Correct |
356 ms |
25876 KB |
Output is correct |
9 |
Correct |
120 ms |
23896 KB |
Output is correct |
10 |
Correct |
311 ms |
25396 KB |
Output is correct |
11 |
Correct |
347 ms |
25292 KB |
Output is correct |
12 |
Correct |
327 ms |
25396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
353 ms |
25736 KB |
Output is correct |
3 |
Correct |
332 ms |
25692 KB |
Output is correct |
4 |
Correct |
372 ms |
25664 KB |
Output is correct |
5 |
Correct |
329 ms |
25696 KB |
Output is correct |
6 |
Correct |
354 ms |
25788 KB |
Output is correct |
7 |
Correct |
329 ms |
25856 KB |
Output is correct |
8 |
Correct |
369 ms |
26180 KB |
Output is correct |
9 |
Correct |
335 ms |
26092 KB |
Output is correct |
10 |
Correct |
365 ms |
26096 KB |
Output is correct |
11 |
Correct |
350 ms |
26084 KB |
Output is correct |
12 |
Correct |
284 ms |
25440 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
3 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
2 ms |
528 KB |
Output is correct |
25 |
Correct |
2 ms |
468 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 |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
184 ms |
2824 KB |
Output is correct |
32 |
Correct |
198 ms |
2896 KB |
Output is correct |
33 |
Correct |
187 ms |
3112 KB |
Output is correct |
34 |
Correct |
180 ms |
3180 KB |
Output is correct |
35 |
Correct |
178 ms |
2976 KB |
Output is correct |
36 |
Correct |
224 ms |
3028 KB |
Output is correct |
37 |
Correct |
174 ms |
2672 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
212 KB |
Output is correct |
40 |
Correct |
2 ms |
468 KB |
Output is correct |
41 |
Correct |
2 ms |
440 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
468 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 |
137 ms |
23884 KB |
Output is correct |
48 |
Correct |
107 ms |
23164 KB |
Output is correct |
49 |
Correct |
125 ms |
23792 KB |
Output is correct |
50 |
Correct |
134 ms |
23960 KB |
Output is correct |
51 |
Correct |
122 ms |
23944 KB |
Output is correct |
52 |
Correct |
141 ms |
23952 KB |
Output is correct |
53 |
Correct |
84 ms |
23284 KB |
Output is correct |
54 |
Correct |
308 ms |
25732 KB |
Output is correct |
55 |
Correct |
342 ms |
25660 KB |
Output is correct |
56 |
Correct |
408 ms |
25736 KB |
Output is correct |
57 |
Correct |
344 ms |
26160 KB |
Output is correct |
58 |
Correct |
346 ms |
26008 KB |
Output is correct |
59 |
Correct |
352 ms |
25780 KB |
Output is correct |
60 |
Correct |
388 ms |
25828 KB |
Output is correct |
61 |
Correct |
356 ms |
25876 KB |
Output is correct |
62 |
Correct |
120 ms |
23896 KB |
Output is correct |
63 |
Correct |
311 ms |
25396 KB |
Output is correct |
64 |
Correct |
347 ms |
25292 KB |
Output is correct |
65 |
Correct |
327 ms |
25396 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
321 ms |
25708 KB |
Output is correct |
68 |
Correct |
357 ms |
25664 KB |
Output is correct |
69 |
Correct |
357 ms |
25792 KB |
Output is correct |
70 |
Correct |
337 ms |
25776 KB |
Output is correct |
71 |
Correct |
354 ms |
25848 KB |
Output is correct |
72 |
Correct |
333 ms |
25884 KB |
Output is correct |
73 |
Correct |
372 ms |
26220 KB |
Output is correct |
74 |
Correct |
339 ms |
26196 KB |
Output is correct |
75 |
Correct |
358 ms |
25984 KB |
Output is correct |
76 |
Correct |
338 ms |
26040 KB |
Output is correct |
77 |
Correct |
267 ms |
25512 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
2 ms |
468 KB |
Output is correct |
80 |
Correct |
2 ms |
468 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
2 ms |
536 KB |
Output is correct |
83 |
Correct |
2 ms |
468 KB |
Output is correct |
84 |
Correct |
2 ms |
468 KB |
Output is correct |
85 |
Correct |
2 ms |
468 KB |
Output is correct |
86 |
Correct |
180 ms |
2764 KB |
Output is correct |
87 |
Correct |
191 ms |
2884 KB |
Output is correct |
88 |
Correct |
188 ms |
3096 KB |
Output is correct |
89 |
Correct |
221 ms |
3208 KB |
Output is correct |
90 |
Correct |
175 ms |
2960 KB |
Output is correct |
91 |
Correct |
192 ms |
3148 KB |
Output is correct |
92 |
Correct |
216 ms |
2748 KB |
Output is correct |
93 |
Correct |
133 ms |
23852 KB |
Output is correct |
94 |
Correct |
109 ms |
23124 KB |
Output is correct |
95 |
Correct |
125 ms |
23976 KB |
Output is correct |
96 |
Correct |
179 ms |
23992 KB |
Output is correct |
97 |
Correct |
139 ms |
23892 KB |
Output is correct |
98 |
Correct |
200 ms |
23900 KB |
Output is correct |
99 |
Correct |
74 ms |
23180 KB |
Output is correct |
100 |
Correct |
415 ms |
25812 KB |
Output is correct |
101 |
Correct |
372 ms |
25780 KB |
Output is correct |
102 |
Correct |
360 ms |
25920 KB |
Output is correct |
103 |
Correct |
380 ms |
25428 KB |
Output is correct |
104 |
Correct |
375 ms |
25200 KB |
Output is correct |
105 |
Correct |
352 ms |
25448 KB |
Output is correct |
106 |
Correct |
379 ms |
25072 KB |
Output is correct |
107 |
Correct |
521 ms |
25156 KB |
Output is correct |
108 |
Correct |
346 ms |
25752 KB |
Output is correct |
109 |
Correct |
353 ms |
25832 KB |
Output is correct |
110 |
Correct |
311 ms |
25672 KB |
Output is correct |
111 |
Correct |
325 ms |
25704 KB |
Output is correct |