#include <bits/stdc++.h>
using namespace std;
int main() {
int n, q;
cin >> n >> q;
vector<vector<int> > dp(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);
dp[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++){
dp[i + 1][j] = dp[i][dp[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[dp[i][y]].first){
left += 1 << i, y = dp[i][y];
}
}
y = dp[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 |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
320 ms |
25676 KB |
Output is correct |
3 |
Correct |
326 ms |
25656 KB |
Output is correct |
4 |
Correct |
356 ms |
25628 KB |
Output is correct |
5 |
Correct |
328 ms |
25720 KB |
Output is correct |
6 |
Correct |
334 ms |
25708 KB |
Output is correct |
7 |
Correct |
330 ms |
25736 KB |
Output is correct |
8 |
Correct |
350 ms |
26172 KB |
Output is correct |
9 |
Correct |
337 ms |
26164 KB |
Output is correct |
10 |
Correct |
338 ms |
26136 KB |
Output is correct |
11 |
Correct |
364 ms |
26068 KB |
Output is correct |
12 |
Correct |
275 ms |
25364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
444 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 |
436 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 |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
444 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 |
436 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 |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
436 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
440 KB |
Output is correct |
16 |
Correct |
2 ms |
436 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
186 ms |
2860 KB |
Output is correct |
20 |
Correct |
190 ms |
2896 KB |
Output is correct |
21 |
Correct |
184 ms |
3048 KB |
Output is correct |
22 |
Correct |
183 ms |
3100 KB |
Output is correct |
23 |
Correct |
177 ms |
3200 KB |
Output is correct |
24 |
Correct |
182 ms |
3048 KB |
Output is correct |
25 |
Correct |
175 ms |
2700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
444 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 |
436 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 |
300 KB |
Output is correct |
12 |
Correct |
2 ms |
468 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 |
436 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 |
135 ms |
23908 KB |
Output is correct |
20 |
Correct |
106 ms |
23136 KB |
Output is correct |
21 |
Correct |
122 ms |
23776 KB |
Output is correct |
22 |
Correct |
128 ms |
23892 KB |
Output is correct |
23 |
Correct |
123 ms |
23948 KB |
Output is correct |
24 |
Correct |
128 ms |
23860 KB |
Output is correct |
25 |
Correct |
69 ms |
23264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
314 ms |
25732 KB |
Output is correct |
2 |
Correct |
322 ms |
25652 KB |
Output is correct |
3 |
Correct |
360 ms |
25684 KB |
Output is correct |
4 |
Correct |
323 ms |
26148 KB |
Output is correct |
5 |
Correct |
342 ms |
26004 KB |
Output is correct |
6 |
Correct |
345 ms |
25836 KB |
Output is correct |
7 |
Correct |
341 ms |
25908 KB |
Output is correct |
8 |
Correct |
319 ms |
25892 KB |
Output is correct |
9 |
Correct |
119 ms |
23916 KB |
Output is correct |
10 |
Correct |
322 ms |
25480 KB |
Output is correct |
11 |
Correct |
308 ms |
25212 KB |
Output is correct |
12 |
Correct |
322 ms |
25484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
320 ms |
25676 KB |
Output is correct |
3 |
Correct |
326 ms |
25656 KB |
Output is correct |
4 |
Correct |
356 ms |
25628 KB |
Output is correct |
5 |
Correct |
328 ms |
25720 KB |
Output is correct |
6 |
Correct |
334 ms |
25708 KB |
Output is correct |
7 |
Correct |
330 ms |
25736 KB |
Output is correct |
8 |
Correct |
350 ms |
26172 KB |
Output is correct |
9 |
Correct |
337 ms |
26164 KB |
Output is correct |
10 |
Correct |
338 ms |
26136 KB |
Output is correct |
11 |
Correct |
364 ms |
26068 KB |
Output is correct |
12 |
Correct |
275 ms |
25364 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
2 ms |
444 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 |
436 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 |
1 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 |
436 KB |
Output is correct |
26 |
Correct |
2 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
440 KB |
Output is correct |
28 |
Correct |
2 ms |
436 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
186 ms |
2860 KB |
Output is correct |
32 |
Correct |
190 ms |
2896 KB |
Output is correct |
33 |
Correct |
184 ms |
3048 KB |
Output is correct |
34 |
Correct |
183 ms |
3100 KB |
Output is correct |
35 |
Correct |
177 ms |
3200 KB |
Output is correct |
36 |
Correct |
182 ms |
3048 KB |
Output is correct |
37 |
Correct |
175 ms |
2700 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
300 KB |
Output is correct |
40 |
Correct |
2 ms |
468 KB |
Output is correct |
41 |
Correct |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
436 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 |
135 ms |
23908 KB |
Output is correct |
48 |
Correct |
106 ms |
23136 KB |
Output is correct |
49 |
Correct |
122 ms |
23776 KB |
Output is correct |
50 |
Correct |
128 ms |
23892 KB |
Output is correct |
51 |
Correct |
123 ms |
23948 KB |
Output is correct |
52 |
Correct |
128 ms |
23860 KB |
Output is correct |
53 |
Correct |
69 ms |
23264 KB |
Output is correct |
54 |
Correct |
314 ms |
25732 KB |
Output is correct |
55 |
Correct |
322 ms |
25652 KB |
Output is correct |
56 |
Correct |
360 ms |
25684 KB |
Output is correct |
57 |
Correct |
323 ms |
26148 KB |
Output is correct |
58 |
Correct |
342 ms |
26004 KB |
Output is correct |
59 |
Correct |
345 ms |
25836 KB |
Output is correct |
60 |
Correct |
341 ms |
25908 KB |
Output is correct |
61 |
Correct |
319 ms |
25892 KB |
Output is correct |
62 |
Correct |
119 ms |
23916 KB |
Output is correct |
63 |
Correct |
322 ms |
25480 KB |
Output is correct |
64 |
Correct |
308 ms |
25212 KB |
Output is correct |
65 |
Correct |
322 ms |
25484 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
314 ms |
25672 KB |
Output is correct |
68 |
Correct |
328 ms |
25596 KB |
Output is correct |
69 |
Correct |
369 ms |
25648 KB |
Output is correct |
70 |
Correct |
330 ms |
25788 KB |
Output is correct |
71 |
Correct |
325 ms |
25792 KB |
Output is correct |
72 |
Correct |
331 ms |
25772 KB |
Output is correct |
73 |
Correct |
328 ms |
26192 KB |
Output is correct |
74 |
Correct |
333 ms |
26108 KB |
Output is correct |
75 |
Correct |
342 ms |
26076 KB |
Output is correct |
76 |
Correct |
361 ms |
26084 KB |
Output is correct |
77 |
Correct |
264 ms |
25424 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 |
2 ms |
468 KB |
Output is correct |
82 |
Correct |
2 ms |
468 KB |
Output is correct |
83 |
Correct |
2 ms |
436 KB |
Output is correct |
84 |
Correct |
2 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
188 ms |
2804 KB |
Output is correct |
87 |
Correct |
187 ms |
3012 KB |
Output is correct |
88 |
Correct |
187 ms |
3132 KB |
Output is correct |
89 |
Correct |
181 ms |
3120 KB |
Output is correct |
90 |
Correct |
179 ms |
3080 KB |
Output is correct |
91 |
Correct |
181 ms |
3068 KB |
Output is correct |
92 |
Correct |
190 ms |
2664 KB |
Output is correct |
93 |
Correct |
131 ms |
23880 KB |
Output is correct |
94 |
Correct |
109 ms |
23160 KB |
Output is correct |
95 |
Correct |
125 ms |
23772 KB |
Output is correct |
96 |
Correct |
136 ms |
23940 KB |
Output is correct |
97 |
Correct |
118 ms |
23948 KB |
Output is correct |
98 |
Correct |
139 ms |
23864 KB |
Output is correct |
99 |
Correct |
72 ms |
23248 KB |
Output is correct |
100 |
Correct |
350 ms |
25856 KB |
Output is correct |
101 |
Correct |
342 ms |
25892 KB |
Output is correct |
102 |
Correct |
326 ms |
25956 KB |
Output is correct |
103 |
Correct |
340 ms |
25556 KB |
Output is correct |
104 |
Correct |
308 ms |
25192 KB |
Output is correct |
105 |
Correct |
325 ms |
25456 KB |
Output is correct |
106 |
Correct |
328 ms |
25060 KB |
Output is correct |
107 |
Correct |
363 ms |
25268 KB |
Output is correct |
108 |
Correct |
323 ms |
25736 KB |
Output is correct |
109 |
Correct |
334 ms |
25864 KB |
Output is correct |
110 |
Correct |
334 ms |
25736 KB |
Output is correct |
111 |
Correct |
324 ms |
25784 KB |
Output is correct |