#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 |
6488 KB |
Output is correct |
2 |
Correct |
64 ms |
12500 KB |
Output is correct |
3 |
Correct |
80 ms |
12552 KB |
Output is correct |
4 |
Correct |
87 ms |
12376 KB |
Output is correct |
5 |
Correct |
75 ms |
12700 KB |
Output is correct |
6 |
Correct |
82 ms |
12860 KB |
Output is correct |
7 |
Correct |
71 ms |
12884 KB |
Output is correct |
8 |
Correct |
68 ms |
13352 KB |
Output is correct |
9 |
Correct |
59 ms |
13312 KB |
Output is correct |
10 |
Correct |
72 ms |
13204 KB |
Output is correct |
11 |
Correct |
68 ms |
13140 KB |
Output is correct |
12 |
Correct |
42 ms |
12388 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 |
6500 KB |
Output is correct |
4 |
Correct |
1 ms |
6672 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 |
6672 KB |
Output is correct |
8 |
Correct |
1 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 |
6500 KB |
Output is correct |
4 |
Correct |
1 ms |
6672 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 |
6672 KB |
Output is correct |
8 |
Correct |
1 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 |
6500 KB |
Output is correct |
12 |
Correct |
2 ms |
6488 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 ms |
6620 KB |
Output is correct |
15 |
Correct |
1 ms |
6664 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 |
26 ms |
8048 KB |
Output is correct |
20 |
Correct |
34 ms |
8492 KB |
Output is correct |
21 |
Correct |
36 ms |
8532 KB |
Output is correct |
22 |
Correct |
21 ms |
8540 KB |
Output is correct |
23 |
Correct |
21 ms |
8384 KB |
Output is correct |
24 |
Correct |
24 ms |
8340 KB |
Output is correct |
25 |
Correct |
24 ms |
8028 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 |
6500 KB |
Output is correct |
4 |
Correct |
1 ms |
6672 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 |
6672 KB |
Output is correct |
8 |
Correct |
1 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 |
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 |
6576 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
43 ms |
10884 KB |
Output is correct |
20 |
Correct |
41 ms |
10220 KB |
Output is correct |
21 |
Correct |
33 ms |
10356 KB |
Output is correct |
22 |
Correct |
46 ms |
10732 KB |
Output is correct |
23 |
Correct |
38 ms |
10836 KB |
Output is correct |
24 |
Correct |
46 ms |
10852 KB |
Output is correct |
25 |
Correct |
17 ms |
9860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
12352 KB |
Output is correct |
2 |
Correct |
79 ms |
12464 KB |
Output is correct |
3 |
Correct |
78 ms |
12512 KB |
Output is correct |
4 |
Correct |
71 ms |
13140 KB |
Output is correct |
5 |
Correct |
73 ms |
13064 KB |
Output is correct |
6 |
Correct |
72 ms |
12884 KB |
Output is correct |
7 |
Correct |
74 ms |
12884 KB |
Output is correct |
8 |
Correct |
74 ms |
13140 KB |
Output is correct |
9 |
Correct |
38 ms |
11032 KB |
Output is correct |
10 |
Correct |
69 ms |
12632 KB |
Output is correct |
11 |
Correct |
62 ms |
12316 KB |
Output is correct |
12 |
Correct |
73 ms |
12476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
64 ms |
12500 KB |
Output is correct |
3 |
Correct |
80 ms |
12552 KB |
Output is correct |
4 |
Correct |
87 ms |
12376 KB |
Output is correct |
5 |
Correct |
75 ms |
12700 KB |
Output is correct |
6 |
Correct |
82 ms |
12860 KB |
Output is correct |
7 |
Correct |
71 ms |
12884 KB |
Output is correct |
8 |
Correct |
68 ms |
13352 KB |
Output is correct |
9 |
Correct |
59 ms |
13312 KB |
Output is correct |
10 |
Correct |
72 ms |
13204 KB |
Output is correct |
11 |
Correct |
68 ms |
13140 KB |
Output is correct |
12 |
Correct |
42 ms |
12388 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 |
6500 KB |
Output is correct |
16 |
Correct |
1 ms |
6672 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 |
6672 KB |
Output is correct |
20 |
Correct |
1 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 |
6500 KB |
Output is correct |
24 |
Correct |
2 ms |
6488 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6620 KB |
Output is correct |
27 |
Correct |
1 ms |
6664 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
26 ms |
8048 KB |
Output is correct |
32 |
Correct |
34 ms |
8492 KB |
Output is correct |
33 |
Correct |
36 ms |
8532 KB |
Output is correct |
34 |
Correct |
21 ms |
8540 KB |
Output is correct |
35 |
Correct |
21 ms |
8384 KB |
Output is correct |
36 |
Correct |
24 ms |
8340 KB |
Output is correct |
37 |
Correct |
24 ms |
8028 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 |
1 ms |
6492 KB |
Output is correct |
43 |
Correct |
1 ms |
6492 KB |
Output is correct |
44 |
Correct |
1 ms |
6492 KB |
Output is correct |
45 |
Correct |
2 ms |
6576 KB |
Output is correct |
46 |
Correct |
1 ms |
6492 KB |
Output is correct |
47 |
Correct |
43 ms |
10884 KB |
Output is correct |
48 |
Correct |
41 ms |
10220 KB |
Output is correct |
49 |
Correct |
33 ms |
10356 KB |
Output is correct |
50 |
Correct |
46 ms |
10732 KB |
Output is correct |
51 |
Correct |
38 ms |
10836 KB |
Output is correct |
52 |
Correct |
46 ms |
10852 KB |
Output is correct |
53 |
Correct |
17 ms |
9860 KB |
Output is correct |
54 |
Correct |
67 ms |
12352 KB |
Output is correct |
55 |
Correct |
79 ms |
12464 KB |
Output is correct |
56 |
Correct |
78 ms |
12512 KB |
Output is correct |
57 |
Correct |
71 ms |
13140 KB |
Output is correct |
58 |
Correct |
73 ms |
13064 KB |
Output is correct |
59 |
Correct |
72 ms |
12884 KB |
Output is correct |
60 |
Correct |
74 ms |
12884 KB |
Output is correct |
61 |
Correct |
74 ms |
13140 KB |
Output is correct |
62 |
Correct |
38 ms |
11032 KB |
Output is correct |
63 |
Correct |
69 ms |
12632 KB |
Output is correct |
64 |
Correct |
62 ms |
12316 KB |
Output is correct |
65 |
Correct |
73 ms |
12476 KB |
Output is correct |
66 |
Correct |
1 ms |
6492 KB |
Output is correct |
67 |
Correct |
61 ms |
12892 KB |
Output is correct |
68 |
Correct |
73 ms |
12888 KB |
Output is correct |
69 |
Correct |
116 ms |
12884 KB |
Output is correct |
70 |
Correct |
83 ms |
12888 KB |
Output is correct |
71 |
Correct |
70 ms |
12888 KB |
Output is correct |
72 |
Correct |
72 ms |
12720 KB |
Output is correct |
73 |
Correct |
58 ms |
13292 KB |
Output is correct |
74 |
Correct |
58 ms |
13232 KB |
Output is correct |
75 |
Correct |
81 ms |
13396 KB |
Output is correct |
76 |
Correct |
84 ms |
13080 KB |
Output is correct |
77 |
Correct |
43 ms |
12504 KB |
Output is correct |
78 |
Correct |
1 ms |
6492 KB |
Output is correct |
79 |
Correct |
1 ms |
6492 KB |
Output is correct |
80 |
Correct |
2 ms |
6692 KB |
Output is correct |
81 |
Correct |
2 ms |
6492 KB |
Output is correct |
82 |
Correct |
1 ms |
6492 KB |
Output is correct |
83 |
Correct |
2 ms |
6492 KB |
Output is correct |
84 |
Correct |
1 ms |
6492 KB |
Output is correct |
85 |
Correct |
1 ms |
6488 KB |
Output is correct |
86 |
Correct |
26 ms |
8000 KB |
Output is correct |
87 |
Correct |
25 ms |
8132 KB |
Output is correct |
88 |
Correct |
34 ms |
8500 KB |
Output is correct |
89 |
Correct |
23 ms |
8420 KB |
Output is correct |
90 |
Correct |
21 ms |
8284 KB |
Output is correct |
91 |
Correct |
20 ms |
8296 KB |
Output is correct |
92 |
Correct |
28 ms |
8020 KB |
Output is correct |
93 |
Correct |
42 ms |
10944 KB |
Output is correct |
94 |
Correct |
41 ms |
10324 KB |
Output is correct |
95 |
Correct |
35 ms |
10648 KB |
Output is correct |
96 |
Correct |
39 ms |
10896 KB |
Output is correct |
97 |
Correct |
38 ms |
10988 KB |
Output is correct |
98 |
Correct |
47 ms |
10912 KB |
Output is correct |
99 |
Correct |
18 ms |
9960 KB |
Output is correct |
100 |
Correct |
72 ms |
12880 KB |
Output is correct |
101 |
Correct |
85 ms |
12984 KB |
Output is correct |
102 |
Correct |
78 ms |
13144 KB |
Output is correct |
103 |
Correct |
77 ms |
12636 KB |
Output is correct |
104 |
Correct |
64 ms |
12384 KB |
Output is correct |
105 |
Correct |
74 ms |
12632 KB |
Output is correct |
106 |
Correct |
68 ms |
12196 KB |
Output is correct |
107 |
Correct |
77 ms |
12236 KB |
Output is correct |
108 |
Correct |
55 ms |
12492 KB |
Output is correct |
109 |
Correct |
60 ms |
12504 KB |
Output is correct |
110 |
Correct |
67 ms |
12496 KB |
Output is correct |
111 |
Correct |
58 ms |
12376 KB |
Output is correct |