#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 |
65 ms |
12840 KB |
Output is correct |
3 |
Correct |
71 ms |
12716 KB |
Output is correct |
4 |
Correct |
92 ms |
12772 KB |
Output is correct |
5 |
Correct |
72 ms |
12884 KB |
Output is correct |
6 |
Correct |
70 ms |
12912 KB |
Output is correct |
7 |
Correct |
70 ms |
12888 KB |
Output is correct |
8 |
Correct |
65 ms |
13356 KB |
Output is correct |
9 |
Correct |
61 ms |
13140 KB |
Output is correct |
10 |
Correct |
69 ms |
13184 KB |
Output is correct |
11 |
Correct |
65 ms |
13140 KB |
Output is correct |
12 |
Correct |
42 ms |
12500 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 |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6488 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 |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6488 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 |
6488 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 |
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 |
2 ms |
6488 KB |
Output is correct |
17 |
Correct |
1 ms |
6620 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
26 ms |
8040 KB |
Output is correct |
20 |
Correct |
24 ms |
8288 KB |
Output is correct |
21 |
Correct |
27 ms |
8524 KB |
Output is correct |
22 |
Correct |
20 ms |
8540 KB |
Output is correct |
23 |
Correct |
24 ms |
8528 KB |
Output is correct |
24 |
Correct |
21 ms |
8460 KB |
Output is correct |
25 |
Correct |
28 ms |
8016 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 |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6488 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 |
6612 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6628 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 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 |
40 ms |
10972 KB |
Output is correct |
20 |
Correct |
39 ms |
10324 KB |
Output is correct |
21 |
Correct |
35 ms |
10448 KB |
Output is correct |
22 |
Correct |
42 ms |
10892 KB |
Output is correct |
23 |
Correct |
39 ms |
11092 KB |
Output is correct |
24 |
Correct |
44 ms |
11092 KB |
Output is correct |
25 |
Correct |
17 ms |
9948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
12824 KB |
Output is correct |
2 |
Correct |
71 ms |
12808 KB |
Output is correct |
3 |
Correct |
81 ms |
12884 KB |
Output is correct |
4 |
Correct |
57 ms |
13460 KB |
Output is correct |
5 |
Correct |
68 ms |
13144 KB |
Output is correct |
6 |
Correct |
73 ms |
12888 KB |
Output is correct |
7 |
Correct |
72 ms |
12948 KB |
Output is correct |
8 |
Correct |
68 ms |
12996 KB |
Output is correct |
9 |
Correct |
37 ms |
11092 KB |
Output is correct |
10 |
Correct |
68 ms |
12652 KB |
Output is correct |
11 |
Correct |
63 ms |
12472 KB |
Output is correct |
12 |
Correct |
68 ms |
12628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
65 ms |
12840 KB |
Output is correct |
3 |
Correct |
71 ms |
12716 KB |
Output is correct |
4 |
Correct |
92 ms |
12772 KB |
Output is correct |
5 |
Correct |
72 ms |
12884 KB |
Output is correct |
6 |
Correct |
70 ms |
12912 KB |
Output is correct |
7 |
Correct |
70 ms |
12888 KB |
Output is correct |
8 |
Correct |
65 ms |
13356 KB |
Output is correct |
9 |
Correct |
61 ms |
13140 KB |
Output is correct |
10 |
Correct |
69 ms |
13184 KB |
Output is correct |
11 |
Correct |
65 ms |
13140 KB |
Output is correct |
12 |
Correct |
42 ms |
12500 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 |
2 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6488 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 |
6488 KB |
Output is correct |
23 |
Correct |
1 ms |
6492 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 |
2 ms |
6488 KB |
Output is correct |
29 |
Correct |
1 ms |
6620 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
26 ms |
8040 KB |
Output is correct |
32 |
Correct |
24 ms |
8288 KB |
Output is correct |
33 |
Correct |
27 ms |
8524 KB |
Output is correct |
34 |
Correct |
20 ms |
8540 KB |
Output is correct |
35 |
Correct |
24 ms |
8528 KB |
Output is correct |
36 |
Correct |
21 ms |
8460 KB |
Output is correct |
37 |
Correct |
28 ms |
8016 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
1 ms |
6612 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
1 ms |
6628 KB |
Output is correct |
42 |
Correct |
1 ms |
6492 KB |
Output is correct |
43 |
Correct |
1 ms |
6492 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 |
40 ms |
10972 KB |
Output is correct |
48 |
Correct |
39 ms |
10324 KB |
Output is correct |
49 |
Correct |
35 ms |
10448 KB |
Output is correct |
50 |
Correct |
42 ms |
10892 KB |
Output is correct |
51 |
Correct |
39 ms |
11092 KB |
Output is correct |
52 |
Correct |
44 ms |
11092 KB |
Output is correct |
53 |
Correct |
17 ms |
9948 KB |
Output is correct |
54 |
Correct |
63 ms |
12824 KB |
Output is correct |
55 |
Correct |
71 ms |
12808 KB |
Output is correct |
56 |
Correct |
81 ms |
12884 KB |
Output is correct |
57 |
Correct |
57 ms |
13460 KB |
Output is correct |
58 |
Correct |
68 ms |
13144 KB |
Output is correct |
59 |
Correct |
73 ms |
12888 KB |
Output is correct |
60 |
Correct |
72 ms |
12948 KB |
Output is correct |
61 |
Correct |
68 ms |
12996 KB |
Output is correct |
62 |
Correct |
37 ms |
11092 KB |
Output is correct |
63 |
Correct |
68 ms |
12652 KB |
Output is correct |
64 |
Correct |
63 ms |
12472 KB |
Output is correct |
65 |
Correct |
68 ms |
12628 KB |
Output is correct |
66 |
Correct |
1 ms |
6492 KB |
Output is correct |
67 |
Correct |
61 ms |
12828 KB |
Output is correct |
68 |
Correct |
73 ms |
12780 KB |
Output is correct |
69 |
Correct |
81 ms |
12764 KB |
Output is correct |
70 |
Correct |
77 ms |
13020 KB |
Output is correct |
71 |
Correct |
74 ms |
12892 KB |
Output is correct |
72 |
Correct |
79 ms |
12856 KB |
Output is correct |
73 |
Correct |
57 ms |
13284 KB |
Output is correct |
74 |
Correct |
58 ms |
13224 KB |
Output is correct |
75 |
Correct |
71 ms |
13132 KB |
Output is correct |
76 |
Correct |
77 ms |
13272 KB |
Output is correct |
77 |
Correct |
42 ms |
12572 KB |
Output is correct |
78 |
Correct |
1 ms |
6488 KB |
Output is correct |
79 |
Correct |
1 ms |
6652 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 |
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 |
6492 KB |
Output is correct |
86 |
Correct |
26 ms |
8036 KB |
Output is correct |
87 |
Correct |
24 ms |
8272 KB |
Output is correct |
88 |
Correct |
27 ms |
8528 KB |
Output is correct |
89 |
Correct |
23 ms |
8784 KB |
Output is correct |
90 |
Correct |
20 ms |
8280 KB |
Output is correct |
91 |
Correct |
19 ms |
8280 KB |
Output is correct |
92 |
Correct |
23 ms |
8088 KB |
Output is correct |
93 |
Correct |
41 ms |
10940 KB |
Output is correct |
94 |
Correct |
39 ms |
10328 KB |
Output is correct |
95 |
Correct |
35 ms |
10532 KB |
Output is correct |
96 |
Correct |
46 ms |
10888 KB |
Output is correct |
97 |
Correct |
38 ms |
11088 KB |
Output is correct |
98 |
Correct |
41 ms |
11092 KB |
Output is correct |
99 |
Correct |
17 ms |
9948 KB |
Output is correct |
100 |
Correct |
72 ms |
12820 KB |
Output is correct |
101 |
Correct |
72 ms |
12888 KB |
Output is correct |
102 |
Correct |
65 ms |
13048 KB |
Output is correct |
103 |
Correct |
68 ms |
12628 KB |
Output is correct |
104 |
Correct |
63 ms |
12372 KB |
Output is correct |
105 |
Correct |
72 ms |
12500 KB |
Output is correct |
106 |
Correct |
66 ms |
12120 KB |
Output is correct |
107 |
Correct |
81 ms |
12304 KB |
Output is correct |
108 |
Correct |
54 ms |
12516 KB |
Output is correct |
109 |
Correct |
60 ms |
12592 KB |
Output is correct |
110 |
Correct |
52 ms |
12500 KB |
Output is correct |
111 |
Correct |
51 ms |
12496 KB |
Output is correct |