#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e5;
const int P=1<<18;
const int L=18;
const int INF=1e9+7;
pair<int,int> t[N+10];
map<int,int> sc;
int pot;
pair<int,int> tr[2*P+10];
int jmp[N+10][L+1];
bool vis_jmp[N+10];
void add(int x,pair<int,int> c)
{
for(x+=pot-1;x>=1;x/=2)
tr[x]=min(tr[x],c);
return;
}
int read(int l,int r)
{
pair<int,int> ans={INF,0};
for(l+=pot-1,r+=pot-1;l<=r;l/=2,r/=2)
{
if(l%2==1)
ans=min(ans,tr[l++]);
if(r%2==0)
ans=min(ans,tr[r--]);
}
return ans.se;
}
void calc_jmp(int x)
{
if(t[jmp[x][0]].fi>=t[x].fi)
jmp[x][0]=0;
vis_jmp[x]=true;
if(!vis_jmp[jmp[x][0]])
calc_jmp(jmp[x][0]);
for(int i=1;i<=L;i++)
jmp[x][i]=jmp[jmp[x][i-1]][i-1];
return;
}
pair<int,int> far(int x,int y)
{
int c=0;
for(int i=L;i>=0;i--)
{
if(t[jmp[x][i]].fi>y)
{
c+=(1<<i);
x=jmp[x][i];
}
}
return {x,c};
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++)
{
cin>>t[i].fi>>t[i].se;
sc[t[i].fi]=sc[t[i].se]=0;
}
int k=0;
for(auto [a,b]:sc)
sc[a]=++k;
for(pot=1;pot<k;pot*=2);
for(int i=1;i<=2*pot;i++)
tr[i]={INF,0};
for(int i=1;i<=n;i++)
add(sc[t[i].se],{t[i].fi,i});
for(int i=1;i<=n;i++)
jmp[i][0]=read(sc[t[i].fi],sc[t[i].se]);
vis_jmp[0]=true;
for(int i=1;i<=n;i++)
{
if(!vis_jmp[i])
calc_jmp(i);
//cerr<<i<<": "<<jmp[i][0]<<"\n";
}
while(q--)
{
int a,b;
cin>>a>>b;
if(a==b)
{
cout<<"0\n";
continue;
}
auto [x,c]=far(b,t[a].fi);
if(t[a].se>t[x].se)
cout<<"impossible\n";
else if(t[a].se>=t[x].fi)
cout<<c+1<<"\n";
else if(jmp[x][0]==0)
cout<<"impossible\n";
else
cout<<c+2<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
312 ms |
17320 KB |
Output is correct |
3 |
Correct |
236 ms |
18208 KB |
Output is correct |
4 |
Correct |
347 ms |
18584 KB |
Output is correct |
5 |
Correct |
259 ms |
17200 KB |
Output is correct |
6 |
Correct |
297 ms |
17816 KB |
Output is correct |
7 |
Correct |
306 ms |
18048 KB |
Output is correct |
8 |
Correct |
325 ms |
23116 KB |
Output is correct |
9 |
Correct |
338 ms |
23168 KB |
Output is correct |
10 |
Correct |
312 ms |
16980 KB |
Output is correct |
11 |
Correct |
323 ms |
21228 KB |
Output is correct |
12 |
Correct |
118 ms |
16412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 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 |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 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 |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
496 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
35 ms |
1740 KB |
Output is correct |
20 |
Correct |
33 ms |
1624 KB |
Output is correct |
21 |
Correct |
52 ms |
1896 KB |
Output is correct |
22 |
Correct |
34 ms |
1872 KB |
Output is correct |
23 |
Correct |
34 ms |
2024 KB |
Output is correct |
24 |
Correct |
33 ms |
2004 KB |
Output is correct |
25 |
Correct |
49 ms |
1736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 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 |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
200 ms |
16728 KB |
Output is correct |
20 |
Correct |
229 ms |
16664 KB |
Output is correct |
21 |
Correct |
188 ms |
15324 KB |
Output is correct |
22 |
Correct |
289 ms |
19808 KB |
Output is correct |
23 |
Correct |
297 ms |
22080 KB |
Output is correct |
24 |
Correct |
366 ms |
22096 KB |
Output is correct |
25 |
Correct |
29 ms |
8528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
17332 KB |
Output is correct |
2 |
Correct |
219 ms |
18216 KB |
Output is correct |
3 |
Correct |
247 ms |
18584 KB |
Output is correct |
4 |
Correct |
288 ms |
23116 KB |
Output is correct |
5 |
Correct |
256 ms |
16976 KB |
Output is correct |
6 |
Correct |
395 ms |
22888 KB |
Output is correct |
7 |
Correct |
356 ms |
22852 KB |
Output is correct |
8 |
Correct |
369 ms |
22928 KB |
Output is correct |
9 |
Correct |
268 ms |
22112 KB |
Output is correct |
10 |
Correct |
351 ms |
22456 KB |
Output is correct |
11 |
Correct |
389 ms |
22268 KB |
Output is correct |
12 |
Correct |
351 ms |
22476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
312 ms |
17320 KB |
Output is correct |
3 |
Correct |
236 ms |
18208 KB |
Output is correct |
4 |
Correct |
347 ms |
18584 KB |
Output is correct |
5 |
Correct |
259 ms |
17200 KB |
Output is correct |
6 |
Correct |
297 ms |
17816 KB |
Output is correct |
7 |
Correct |
306 ms |
18048 KB |
Output is correct |
8 |
Correct |
325 ms |
23116 KB |
Output is correct |
9 |
Correct |
338 ms |
23168 KB |
Output is correct |
10 |
Correct |
312 ms |
16980 KB |
Output is correct |
11 |
Correct |
323 ms |
21228 KB |
Output is correct |
12 |
Correct |
118 ms |
16412 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
468 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 |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
496 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
35 ms |
1740 KB |
Output is correct |
32 |
Correct |
33 ms |
1624 KB |
Output is correct |
33 |
Correct |
52 ms |
1896 KB |
Output is correct |
34 |
Correct |
34 ms |
1872 KB |
Output is correct |
35 |
Correct |
34 ms |
2024 KB |
Output is correct |
36 |
Correct |
33 ms |
2004 KB |
Output is correct |
37 |
Correct |
49 ms |
1736 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
2 ms |
468 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
200 ms |
16728 KB |
Output is correct |
48 |
Correct |
229 ms |
16664 KB |
Output is correct |
49 |
Correct |
188 ms |
15324 KB |
Output is correct |
50 |
Correct |
289 ms |
19808 KB |
Output is correct |
51 |
Correct |
297 ms |
22080 KB |
Output is correct |
52 |
Correct |
366 ms |
22096 KB |
Output is correct |
53 |
Correct |
29 ms |
8528 KB |
Output is correct |
54 |
Correct |
224 ms |
17332 KB |
Output is correct |
55 |
Correct |
219 ms |
18216 KB |
Output is correct |
56 |
Correct |
247 ms |
18584 KB |
Output is correct |
57 |
Correct |
288 ms |
23116 KB |
Output is correct |
58 |
Correct |
256 ms |
16976 KB |
Output is correct |
59 |
Correct |
395 ms |
22888 KB |
Output is correct |
60 |
Correct |
356 ms |
22852 KB |
Output is correct |
61 |
Correct |
369 ms |
22928 KB |
Output is correct |
62 |
Correct |
268 ms |
22112 KB |
Output is correct |
63 |
Correct |
351 ms |
22456 KB |
Output is correct |
64 |
Correct |
389 ms |
22268 KB |
Output is correct |
65 |
Correct |
351 ms |
22476 KB |
Output is correct |
66 |
Correct |
1 ms |
340 KB |
Output is correct |
67 |
Correct |
210 ms |
17440 KB |
Output is correct |
68 |
Correct |
203 ms |
18260 KB |
Output is correct |
69 |
Correct |
224 ms |
18688 KB |
Output is correct |
70 |
Correct |
219 ms |
17316 KB |
Output is correct |
71 |
Correct |
237 ms |
17940 KB |
Output is correct |
72 |
Correct |
218 ms |
18084 KB |
Output is correct |
73 |
Correct |
229 ms |
23176 KB |
Output is correct |
74 |
Correct |
234 ms |
23260 KB |
Output is correct |
75 |
Correct |
214 ms |
16972 KB |
Output is correct |
76 |
Correct |
246 ms |
21176 KB |
Output is correct |
77 |
Correct |
119 ms |
16444 KB |
Output is correct |
78 |
Correct |
0 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
1 ms |
468 KB |
Output is correct |
81 |
Correct |
1 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
468 KB |
Output is correct |
83 |
Correct |
2 ms |
468 KB |
Output is correct |
84 |
Correct |
2 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
340 KB |
Output is correct |
86 |
Correct |
36 ms |
1676 KB |
Output is correct |
87 |
Correct |
29 ms |
1612 KB |
Output is correct |
88 |
Correct |
35 ms |
1952 KB |
Output is correct |
89 |
Correct |
28 ms |
1920 KB |
Output is correct |
90 |
Correct |
30 ms |
2128 KB |
Output is correct |
91 |
Correct |
30 ms |
2108 KB |
Output is correct |
92 |
Correct |
29 ms |
1736 KB |
Output is correct |
93 |
Correct |
194 ms |
16704 KB |
Output is correct |
94 |
Correct |
162 ms |
16748 KB |
Output is correct |
95 |
Correct |
163 ms |
15380 KB |
Output is correct |
96 |
Correct |
198 ms |
19688 KB |
Output is correct |
97 |
Correct |
232 ms |
22084 KB |
Output is correct |
98 |
Correct |
313 ms |
22120 KB |
Output is correct |
99 |
Correct |
32 ms |
8532 KB |
Output is correct |
100 |
Correct |
317 ms |
22912 KB |
Output is correct |
101 |
Correct |
308 ms |
22924 KB |
Output is correct |
102 |
Correct |
304 ms |
23004 KB |
Output is correct |
103 |
Correct |
313 ms |
22584 KB |
Output is correct |
104 |
Correct |
301 ms |
22376 KB |
Output is correct |
105 |
Correct |
308 ms |
22480 KB |
Output is correct |
106 |
Correct |
195 ms |
16312 KB |
Output is correct |
107 |
Correct |
249 ms |
17792 KB |
Output is correct |
108 |
Correct |
187 ms |
16180 KB |
Output is correct |
109 |
Correct |
198 ms |
16188 KB |
Output is correct |
110 |
Correct |
328 ms |
22648 KB |
Output is correct |
111 |
Correct |
366 ms |
22644 KB |
Output is correct |