#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10 , L = 18;
int n , q , l[N] , r[N] , mv[L][N];
vector <int> vec , st;
bool cmp(int a , int b) {
if(r[a] != r[b]) return r[a] < r[b];
return l[a] < l[b];
}
int Bs(int x){
int low = -1 , high = st.size();
while(high - low > 1) {
int mid = (low + high) >> 1;
if(r[st[mid]] >= x) high = mid;
else low = mid;
}
return st[high];
}
int solve(int from , int to , int bit) {
if(bit == -1) return 0;
int nex = mv[bit][from];
if(l[nex] > r[to]) return (1 << bit) + solve(nex , to , bit - 1);
return solve(from , to , bit - 1);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> q;
for(int i = 1 ; i <= n ; i++) {
cin >> l[i] >> r[i];
vec.push_back(i);
}
sort(vec.begin() , vec.end() , cmp);
for(int i = 1 ; i <= n ; i++) {
int now = vec[i];
while(!st.empty() && l[st.back()] >= l[now]) st.pop_back();
st.push_back(now);
mv[0][now] = Bs(l[now]);
}
for(int i = 1 ; i < L ; i++) for(int j = 1 ; j <= n ; j++) mv[i][j] = mv[i - 1][mv[i - 1][j]];
while(q--) {
int s , e; cin >> s >> e;
if(l[mv[L - 1][e]] > r[s] || r[e] < r[s]) {
cout << "impossible" << '\n';
continue;
}
if(s == e) {
cout << 0 << '\n';
continue;
}
if(l[e] <= r[s]) {
cout << 1 << '\n';
continue;
}
cout << solve(e , s , L - 1) + 2 << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
61 ms |
12888 KB |
Output is correct |
3 |
Correct |
69 ms |
12888 KB |
Output is correct |
4 |
Correct |
79 ms |
12684 KB |
Output is correct |
5 |
Correct |
73 ms |
12884 KB |
Output is correct |
6 |
Correct |
70 ms |
12880 KB |
Output is correct |
7 |
Correct |
72 ms |
12872 KB |
Output is correct |
8 |
Correct |
58 ms |
13216 KB |
Output is correct |
9 |
Correct |
58 ms |
13196 KB |
Output is correct |
10 |
Correct |
71 ms |
13140 KB |
Output is correct |
11 |
Correct |
66 ms |
13268 KB |
Output is correct |
12 |
Correct |
42 ms |
12496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6608 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6488 KB |
Output is correct |
19 |
Correct |
26 ms |
8096 KB |
Output is correct |
20 |
Correct |
25 ms |
8280 KB |
Output is correct |
21 |
Correct |
27 ms |
8532 KB |
Output is correct |
22 |
Correct |
20 ms |
8536 KB |
Output is correct |
23 |
Correct |
20 ms |
8284 KB |
Output is correct |
24 |
Correct |
20 ms |
8464 KB |
Output is correct |
25 |
Correct |
22 ms |
8020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6488 KB |
Output is correct |
14 |
Correct |
2 ms |
6668 KB |
Output is correct |
15 |
Correct |
1 ms |
6660 KB |
Output is correct |
16 |
Correct |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
42 ms |
10928 KB |
Output is correct |
20 |
Correct |
38 ms |
10196 KB |
Output is correct |
21 |
Correct |
32 ms |
10452 KB |
Output is correct |
22 |
Correct |
38 ms |
10924 KB |
Output is correct |
23 |
Correct |
37 ms |
10892 KB |
Output is correct |
24 |
Correct |
40 ms |
11092 KB |
Output is correct |
25 |
Correct |
17 ms |
9948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
13144 KB |
Output is correct |
2 |
Correct |
71 ms |
12888 KB |
Output is correct |
3 |
Correct |
79 ms |
12772 KB |
Output is correct |
4 |
Correct |
56 ms |
13144 KB |
Output is correct |
5 |
Correct |
68 ms |
13124 KB |
Output is correct |
6 |
Correct |
74 ms |
12896 KB |
Output is correct |
7 |
Correct |
71 ms |
12960 KB |
Output is correct |
8 |
Correct |
67 ms |
13144 KB |
Output is correct |
9 |
Correct |
37 ms |
11012 KB |
Output is correct |
10 |
Correct |
70 ms |
12888 KB |
Output is correct |
11 |
Correct |
61 ms |
12256 KB |
Output is correct |
12 |
Correct |
69 ms |
12628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
61 ms |
12888 KB |
Output is correct |
3 |
Correct |
69 ms |
12888 KB |
Output is correct |
4 |
Correct |
79 ms |
12684 KB |
Output is correct |
5 |
Correct |
73 ms |
12884 KB |
Output is correct |
6 |
Correct |
70 ms |
12880 KB |
Output is correct |
7 |
Correct |
72 ms |
12872 KB |
Output is correct |
8 |
Correct |
58 ms |
13216 KB |
Output is correct |
9 |
Correct |
58 ms |
13196 KB |
Output is correct |
10 |
Correct |
71 ms |
13140 KB |
Output is correct |
11 |
Correct |
66 ms |
13268 KB |
Output is correct |
12 |
Correct |
42 ms |
12496 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
2 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 ms |
6608 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6492 KB |
Output is correct |
30 |
Correct |
2 ms |
6488 KB |
Output is correct |
31 |
Correct |
26 ms |
8096 KB |
Output is correct |
32 |
Correct |
25 ms |
8280 KB |
Output is correct |
33 |
Correct |
27 ms |
8532 KB |
Output is correct |
34 |
Correct |
20 ms |
8536 KB |
Output is correct |
35 |
Correct |
20 ms |
8284 KB |
Output is correct |
36 |
Correct |
20 ms |
8464 KB |
Output is correct |
37 |
Correct |
22 ms |
8020 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
1 ms |
6492 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
1 ms |
6488 KB |
Output is correct |
42 |
Correct |
2 ms |
6668 KB |
Output is correct |
43 |
Correct |
1 ms |
6660 KB |
Output is correct |
44 |
Correct |
2 ms |
6492 KB |
Output is correct |
45 |
Correct |
1 ms |
6492 KB |
Output is correct |
46 |
Correct |
1 ms |
6492 KB |
Output is correct |
47 |
Correct |
42 ms |
10928 KB |
Output is correct |
48 |
Correct |
38 ms |
10196 KB |
Output is correct |
49 |
Correct |
32 ms |
10452 KB |
Output is correct |
50 |
Correct |
38 ms |
10924 KB |
Output is correct |
51 |
Correct |
37 ms |
10892 KB |
Output is correct |
52 |
Correct |
40 ms |
11092 KB |
Output is correct |
53 |
Correct |
17 ms |
9948 KB |
Output is correct |
54 |
Correct |
68 ms |
13144 KB |
Output is correct |
55 |
Correct |
71 ms |
12888 KB |
Output is correct |
56 |
Correct |
79 ms |
12772 KB |
Output is correct |
57 |
Correct |
56 ms |
13144 KB |
Output is correct |
58 |
Correct |
68 ms |
13124 KB |
Output is correct |
59 |
Correct |
74 ms |
12896 KB |
Output is correct |
60 |
Correct |
71 ms |
12960 KB |
Output is correct |
61 |
Correct |
67 ms |
13144 KB |
Output is correct |
62 |
Correct |
37 ms |
11012 KB |
Output is correct |
63 |
Correct |
70 ms |
12888 KB |
Output is correct |
64 |
Correct |
61 ms |
12256 KB |
Output is correct |
65 |
Correct |
69 ms |
12628 KB |
Output is correct |
66 |
Correct |
1 ms |
6488 KB |
Output is correct |
67 |
Correct |
62 ms |
12752 KB |
Output is correct |
68 |
Correct |
72 ms |
12804 KB |
Output is correct |
69 |
Correct |
84 ms |
12684 KB |
Output is correct |
70 |
Correct |
74 ms |
12880 KB |
Output is correct |
71 |
Correct |
71 ms |
12884 KB |
Output is correct |
72 |
Correct |
71 ms |
12880 KB |
Output is correct |
73 |
Correct |
60 ms |
13396 KB |
Output is correct |
74 |
Correct |
57 ms |
13144 KB |
Output is correct |
75 |
Correct |
69 ms |
13144 KB |
Output is correct |
76 |
Correct |
64 ms |
13264 KB |
Output is correct |
77 |
Correct |
42 ms |
12496 KB |
Output is correct |
78 |
Correct |
1 ms |
6488 KB |
Output is correct |
79 |
Correct |
1 ms |
6492 KB |
Output is correct |
80 |
Correct |
1 ms |
6492 KB |
Output is correct |
81 |
Correct |
1 ms |
6492 KB |
Output is correct |
82 |
Correct |
1 ms |
6740 KB |
Output is correct |
83 |
Correct |
1 ms |
6492 KB |
Output is correct |
84 |
Correct |
1 ms |
6492 KB |
Output is correct |
85 |
Correct |
1 ms |
6492 KB |
Output is correct |
86 |
Correct |
27 ms |
8140 KB |
Output is correct |
87 |
Correct |
24 ms |
8280 KB |
Output is correct |
88 |
Correct |
29 ms |
8340 KB |
Output is correct |
89 |
Correct |
20 ms |
8540 KB |
Output is correct |
90 |
Correct |
20 ms |
8272 KB |
Output is correct |
91 |
Correct |
19 ms |
8272 KB |
Output is correct |
92 |
Correct |
25 ms |
8028 KB |
Output is correct |
93 |
Correct |
41 ms |
11036 KB |
Output is correct |
94 |
Correct |
42 ms |
10332 KB |
Output is correct |
95 |
Correct |
33 ms |
10624 KB |
Output is correct |
96 |
Correct |
39 ms |
10904 KB |
Output is correct |
97 |
Correct |
39 ms |
11012 KB |
Output is correct |
98 |
Correct |
41 ms |
11104 KB |
Output is correct |
99 |
Correct |
20 ms |
9844 KB |
Output is correct |
100 |
Correct |
72 ms |
12888 KB |
Output is correct |
101 |
Correct |
77 ms |
13004 KB |
Output is correct |
102 |
Correct |
71 ms |
13152 KB |
Output is correct |
103 |
Correct |
68 ms |
12632 KB |
Output is correct |
104 |
Correct |
64 ms |
12376 KB |
Output is correct |
105 |
Correct |
68 ms |
12668 KB |
Output is correct |
106 |
Correct |
65 ms |
12116 KB |
Output is correct |
107 |
Correct |
76 ms |
12108 KB |
Output is correct |
108 |
Correct |
54 ms |
12500 KB |
Output is correct |
109 |
Correct |
54 ms |
12412 KB |
Output is correct |
110 |
Correct |
57 ms |
12828 KB |
Output is correct |
111 |
Correct |
53 ms |
12492 KB |
Output is correct |