#include <bits/stdc++.h>
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
#define sz(x) int((x).size())
#define pb push_back
#define s second
#define f first
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
const int INF=1e9;
const int N=1e5+10, L=18;
int n, q, s[N], e[N], bl[L][N];
map<int, int> c; int idx;
pii t[8*N];
void upd(int p, pii x, int l=1, int r=idx, int id=1)
{
if(l==r){
t[id]=min(t[id],x);
return;
}
int m=(l+r)/2;
if(p<=m) upd(p, x, l, m, id*2);
else upd(p, x, m+1, r, id*2+1);
t[id]=min(t[id*2], t[id*2+1]);
}
pii get(int x, int y, int l=1, int r=idx, int id=1)
{
if(r<x||l>y) return {INF,INF};
if(x<=l&&r<=y) return t[id];
int m=(l+r)/2;
return min(
get(x, y, l, m, id*2),
get(x, y, m+1, r, id*2+1)
);
}
void build()
{
FOR(i, 1, L)
{
FOR(j, 0, n)
{
bl[i][j]=bl[i-1][bl[i-1][j]];
}
}
}
int ans(int a, int b)
{
if(a==b) return 0;
if(s[b]<=e[a] && e[a]<=e[b]) return 1;
if(e[a]>e[b]) return INF;
int ret=0;
for(int i=L-1; i>=0; i--){
int k=bl[i][b];
if(s[k]<=e[a]) continue;
b=k; ret+=1<<i;
}
b=bl[0][b]; ret++;
if(s[b]<=e[a]) return ret+1;
return INF;
}
int main()
{
FAST_IO;
cin >> n >> q;
set<int> dif;
FOR(i, 0, n)
{
cin >> s[i] >> e[i];
dif.insert(s[i]);
dif.insert(e[i]);
}
for(auto it : dif) c[it]=++idx;
FOR(i, 0, 8*N) t[i]={INF,INF};
FOR(i, 0, n)
{
upd(c[e[i]], {s[i],i});
}
FOR(i, 0, n)
{
pii ret=get(c[s[i]], c[e[i]]);
int in=ret.s;
bl[0][i]=in;
// cout << i << ": " << in << endl;
}
build();
while(q--){
int a, b; cin >> a >> b; --a; --b;
int k=ans(a, b);
if(k==INF){
cout << "impossible\n";
}
else{
cout << k << '\n';
}
}
}
/*
2 1
1 4
2 3
1 2
5 2
1 3
2 4
4 7
7 9
3 7
tst1:
8 1
1 3
3 6
3 7
3 8
3 9
3 10
10 10
1 10
tst2:
3 1
3 4
1 6
4 9
tst3:
5 1
1 8
1 2
2 3
3 4
6 7
tst4:
7 1
1 4
2 4
3 4
4 5
4 6
4 7
7 9
tst5:
5 1
1 2
2 5
2 6
5 8
6 8
tst6:
5 1
1 2
2 3
3 4
4 9
2 8
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
196 ms |
27496 KB |
Output is correct |
3 |
Correct |
186 ms |
27472 KB |
Output is correct |
4 |
Correct |
191 ms |
27476 KB |
Output is correct |
5 |
Correct |
220 ms |
28500 KB |
Output is correct |
6 |
Correct |
195 ms |
29268 KB |
Output is correct |
7 |
Correct |
199 ms |
29636 KB |
Output is correct |
8 |
Correct |
304 ms |
37452 KB |
Output is correct |
9 |
Correct |
244 ms |
37476 KB |
Output is correct |
10 |
Correct |
193 ms |
27816 KB |
Output is correct |
11 |
Correct |
210 ms |
31644 KB |
Output is correct |
12 |
Correct |
112 ms |
27472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
13144 KB |
Output is correct |
4 |
Correct |
3 ms |
12980 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
4 ms |
13148 KB |
Output is correct |
8 |
Correct |
3 ms |
13144 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
13144 KB |
Output is correct |
4 |
Correct |
3 ms |
12980 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
4 ms |
13148 KB |
Output is correct |
8 |
Correct |
3 ms |
13144 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
3 ms |
12892 KB |
Output is correct |
13 |
Correct |
3 ms |
12892 KB |
Output is correct |
14 |
Correct |
3 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
12892 KB |
Output is correct |
16 |
Correct |
4 ms |
13156 KB |
Output is correct |
17 |
Correct |
4 ms |
13144 KB |
Output is correct |
18 |
Correct |
3 ms |
12892 KB |
Output is correct |
19 |
Correct |
31 ms |
14980 KB |
Output is correct |
20 |
Correct |
28 ms |
14912 KB |
Output is correct |
21 |
Correct |
31 ms |
15188 KB |
Output is correct |
22 |
Correct |
26 ms |
15192 KB |
Output is correct |
23 |
Correct |
26 ms |
15440 KB |
Output is correct |
24 |
Correct |
25 ms |
15444 KB |
Output is correct |
25 |
Correct |
27 ms |
15184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
3 ms |
13144 KB |
Output is correct |
4 |
Correct |
3 ms |
12980 KB |
Output is correct |
5 |
Correct |
3 ms |
12892 KB |
Output is correct |
6 |
Correct |
3 ms |
12892 KB |
Output is correct |
7 |
Correct |
4 ms |
13148 KB |
Output is correct |
8 |
Correct |
3 ms |
13144 KB |
Output is correct |
9 |
Correct |
2 ms |
12892 KB |
Output is correct |
10 |
Correct |
3 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12892 KB |
Output is correct |
12 |
Correct |
4 ms |
13068 KB |
Output is correct |
13 |
Correct |
3 ms |
13092 KB |
Output is correct |
14 |
Correct |
3 ms |
12896 KB |
Output is correct |
15 |
Correct |
3 ms |
13348 KB |
Output is correct |
16 |
Correct |
4 ms |
13148 KB |
Output is correct |
17 |
Correct |
3 ms |
13156 KB |
Output is correct |
18 |
Correct |
3 ms |
12904 KB |
Output is correct |
19 |
Correct |
165 ms |
26068 KB |
Output is correct |
20 |
Correct |
158 ms |
24928 KB |
Output is correct |
21 |
Correct |
167 ms |
25784 KB |
Output is correct |
22 |
Correct |
182 ms |
29264 KB |
Output is correct |
23 |
Correct |
234 ms |
35156 KB |
Output is correct |
24 |
Correct |
253 ms |
35148 KB |
Output is correct |
25 |
Correct |
19 ms |
15960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
27476 KB |
Output is correct |
2 |
Correct |
197 ms |
27484 KB |
Output is correct |
3 |
Correct |
191 ms |
27552 KB |
Output is correct |
4 |
Correct |
245 ms |
37444 KB |
Output is correct |
5 |
Correct |
187 ms |
27992 KB |
Output is correct |
6 |
Correct |
279 ms |
37204 KB |
Output is correct |
7 |
Correct |
274 ms |
37104 KB |
Output is correct |
8 |
Correct |
272 ms |
37204 KB |
Output is correct |
9 |
Correct |
240 ms |
35036 KB |
Output is correct |
10 |
Correct |
294 ms |
36692 KB |
Output is correct |
11 |
Correct |
299 ms |
36604 KB |
Output is correct |
12 |
Correct |
272 ms |
36792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
196 ms |
27496 KB |
Output is correct |
3 |
Correct |
186 ms |
27472 KB |
Output is correct |
4 |
Correct |
191 ms |
27476 KB |
Output is correct |
5 |
Correct |
220 ms |
28500 KB |
Output is correct |
6 |
Correct |
195 ms |
29268 KB |
Output is correct |
7 |
Correct |
199 ms |
29636 KB |
Output is correct |
8 |
Correct |
304 ms |
37452 KB |
Output is correct |
9 |
Correct |
244 ms |
37476 KB |
Output is correct |
10 |
Correct |
193 ms |
27816 KB |
Output is correct |
11 |
Correct |
210 ms |
31644 KB |
Output is correct |
12 |
Correct |
112 ms |
27472 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
3 ms |
13144 KB |
Output is correct |
16 |
Correct |
3 ms |
12980 KB |
Output is correct |
17 |
Correct |
3 ms |
12892 KB |
Output is correct |
18 |
Correct |
3 ms |
12892 KB |
Output is correct |
19 |
Correct |
4 ms |
13148 KB |
Output is correct |
20 |
Correct |
3 ms |
13144 KB |
Output is correct |
21 |
Correct |
2 ms |
12892 KB |
Output is correct |
22 |
Correct |
3 ms |
12892 KB |
Output is correct |
23 |
Correct |
2 ms |
12892 KB |
Output is correct |
24 |
Correct |
3 ms |
12892 KB |
Output is correct |
25 |
Correct |
3 ms |
12892 KB |
Output is correct |
26 |
Correct |
3 ms |
12892 KB |
Output is correct |
27 |
Correct |
3 ms |
12892 KB |
Output is correct |
28 |
Correct |
4 ms |
13156 KB |
Output is correct |
29 |
Correct |
4 ms |
13144 KB |
Output is correct |
30 |
Correct |
3 ms |
12892 KB |
Output is correct |
31 |
Correct |
31 ms |
14980 KB |
Output is correct |
32 |
Correct |
28 ms |
14912 KB |
Output is correct |
33 |
Correct |
31 ms |
15188 KB |
Output is correct |
34 |
Correct |
26 ms |
15192 KB |
Output is correct |
35 |
Correct |
26 ms |
15440 KB |
Output is correct |
36 |
Correct |
25 ms |
15444 KB |
Output is correct |
37 |
Correct |
27 ms |
15184 KB |
Output is correct |
38 |
Correct |
3 ms |
12892 KB |
Output is correct |
39 |
Correct |
2 ms |
12892 KB |
Output is correct |
40 |
Correct |
4 ms |
13068 KB |
Output is correct |
41 |
Correct |
3 ms |
13092 KB |
Output is correct |
42 |
Correct |
3 ms |
12896 KB |
Output is correct |
43 |
Correct |
3 ms |
13348 KB |
Output is correct |
44 |
Correct |
4 ms |
13148 KB |
Output is correct |
45 |
Correct |
3 ms |
13156 KB |
Output is correct |
46 |
Correct |
3 ms |
12904 KB |
Output is correct |
47 |
Correct |
165 ms |
26068 KB |
Output is correct |
48 |
Correct |
158 ms |
24928 KB |
Output is correct |
49 |
Correct |
167 ms |
25784 KB |
Output is correct |
50 |
Correct |
182 ms |
29264 KB |
Output is correct |
51 |
Correct |
234 ms |
35156 KB |
Output is correct |
52 |
Correct |
253 ms |
35148 KB |
Output is correct |
53 |
Correct |
19 ms |
15960 KB |
Output is correct |
54 |
Correct |
173 ms |
27476 KB |
Output is correct |
55 |
Correct |
197 ms |
27484 KB |
Output is correct |
56 |
Correct |
191 ms |
27552 KB |
Output is correct |
57 |
Correct |
245 ms |
37444 KB |
Output is correct |
58 |
Correct |
187 ms |
27992 KB |
Output is correct |
59 |
Correct |
279 ms |
37204 KB |
Output is correct |
60 |
Correct |
274 ms |
37104 KB |
Output is correct |
61 |
Correct |
272 ms |
37204 KB |
Output is correct |
62 |
Correct |
240 ms |
35036 KB |
Output is correct |
63 |
Correct |
294 ms |
36692 KB |
Output is correct |
64 |
Correct |
299 ms |
36604 KB |
Output is correct |
65 |
Correct |
272 ms |
36792 KB |
Output is correct |
66 |
Correct |
2 ms |
12888 KB |
Output is correct |
67 |
Correct |
171 ms |
27568 KB |
Output is correct |
68 |
Correct |
182 ms |
27544 KB |
Output is correct |
69 |
Correct |
196 ms |
27372 KB |
Output is correct |
70 |
Correct |
221 ms |
28536 KB |
Output is correct |
71 |
Correct |
198 ms |
29268 KB |
Output is correct |
72 |
Correct |
196 ms |
29784 KB |
Output is correct |
73 |
Correct |
236 ms |
37288 KB |
Output is correct |
74 |
Correct |
269 ms |
37548 KB |
Output is correct |
75 |
Correct |
183 ms |
27984 KB |
Output is correct |
76 |
Correct |
212 ms |
31772 KB |
Output is correct |
77 |
Correct |
110 ms |
27248 KB |
Output is correct |
78 |
Correct |
2 ms |
12888 KB |
Output is correct |
79 |
Correct |
3 ms |
12892 KB |
Output is correct |
80 |
Correct |
3 ms |
12892 KB |
Output is correct |
81 |
Correct |
3 ms |
12892 KB |
Output is correct |
82 |
Correct |
3 ms |
12892 KB |
Output is correct |
83 |
Correct |
3 ms |
13156 KB |
Output is correct |
84 |
Correct |
4 ms |
13148 KB |
Output is correct |
85 |
Correct |
3 ms |
12980 KB |
Output is correct |
86 |
Correct |
30 ms |
15120 KB |
Output is correct |
87 |
Correct |
30 ms |
15196 KB |
Output is correct |
88 |
Correct |
30 ms |
15184 KB |
Output is correct |
89 |
Correct |
25 ms |
15188 KB |
Output is correct |
90 |
Correct |
27 ms |
15620 KB |
Output is correct |
91 |
Correct |
24 ms |
15452 KB |
Output is correct |
92 |
Correct |
27 ms |
15184 KB |
Output is correct |
93 |
Correct |
150 ms |
25904 KB |
Output is correct |
94 |
Correct |
149 ms |
24992 KB |
Output is correct |
95 |
Correct |
167 ms |
25664 KB |
Output is correct |
96 |
Correct |
184 ms |
29508 KB |
Output is correct |
97 |
Correct |
244 ms |
35180 KB |
Output is correct |
98 |
Correct |
238 ms |
35156 KB |
Output is correct |
99 |
Correct |
18 ms |
15708 KB |
Output is correct |
100 |
Correct |
313 ms |
37296 KB |
Output is correct |
101 |
Correct |
295 ms |
36948 KB |
Output is correct |
102 |
Correct |
299 ms |
37080 KB |
Output is correct |
103 |
Correct |
273 ms |
36688 KB |
Output is correct |
104 |
Correct |
287 ms |
36432 KB |
Output is correct |
105 |
Correct |
270 ms |
36688 KB |
Output is correct |
106 |
Correct |
160 ms |
24584 KB |
Output is correct |
107 |
Correct |
204 ms |
27000 KB |
Output is correct |
108 |
Correct |
193 ms |
27696 KB |
Output is correct |
109 |
Correct |
194 ms |
27720 KB |
Output is correct |
110 |
Correct |
294 ms |
37076 KB |
Output is correct |
111 |
Correct |
280 ms |
36948 KB |
Output is correct |