#include <bits/stdc++.h>
using namespace std;
const long long MX=2e5+10;
const long long INF=1e9;
pair < int , int > tree[4*MX];
void upd(int v,int tl,int tr,int in,pair < int , int > zn)
{
if(tl==tr)
{
tree[v]=min(tree[v],zn);
return;
}
int mid=(tl+tr)/2;
if(in<=mid)
{
upd(v*2,tl,mid,in,zn);
}
else
{
upd(v*2+1,mid+1,tr,in,zn);
}
tree[v]=min(tree[v*2],tree[v*2+1]);
}
pair < int , int > fget(int v,int tl,int tr,int l,int r)
{
if(r<tl || tr<l)
{
return make_pair(INF,0);
}
if(l<=tl && tr<=r)
{
return tree[v];
}
int mid=(tl+tr)/2;
return min(fget(v*2,tl,mid,l,r),fget(v*2+1,mid+1,tr,l,r));
}
int l[MX],r[MX];
int n,k;
void compr()
{
map < int , int > mp;
for(int i=1;i<=n;i++)
{
mp[l[i]]=1;
mp[r[i]]=1;
}
int uk=0;
for(auto & u:mp)
{
uk++;
u.second=uk;
}
for(int i=1;i<=n;i++)
{
l[i]=mp[l[i]];
r[i]=mp[r[i]];
}
}
int lca[20][MX];
void fun(int a,int b)
{
if(a==b)
{
cout<<"0\n";
return;
}
if(l[b]<=r[a] && r[a]<=r[b])
{
cout<<"1\n";
return;
}
if(r[b]<r[a] || r[a]<l[lca[19][b]])
{
cout<<"impossible\n";
return;
}
int ans=2;
for(int i=19;i>=0;i--)
{
if(r[a]<l[lca[i][b]])
{
b=lca[i][b];
ans+=(1<<i);
}
}
cout<<ans<<"\n";
}
int main()
{
cin.tie(0);
ios_base::sync_with_stdio(0);
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>l[i]>>r[i];
}
compr();
fill(tree+1,tree+4*MX,make_pair(INF,0));
for(int i=1;i<=n;i++)
{
upd(1,1,2*n,r[i],make_pair(l[i],i));
}
for(int i=1;i<=n;i++)
{
lca[0][i]=fget(1,1,2*n,l[i],r[i]).second;
}
for(int i=1;i<20;i++)
{
for(int j=1;j<=n;j++)
{
lca[i][j]=lca[i-1][lca[i-1][j]];
}
}
for(int i=1;i<=k;i++)
{
int a,b;
cin>>a>>b;
fun(a,b);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23644 KB |
Output is correct |
2 |
Correct |
134 ms |
32156 KB |
Output is correct |
3 |
Correct |
145 ms |
32280 KB |
Output is correct |
4 |
Correct |
153 ms |
32324 KB |
Output is correct |
5 |
Correct |
155 ms |
33004 KB |
Output is correct |
6 |
Correct |
147 ms |
33196 KB |
Output is correct |
7 |
Correct |
153 ms |
33424 KB |
Output is correct |
8 |
Correct |
153 ms |
37348 KB |
Output is correct |
9 |
Correct |
143 ms |
37356 KB |
Output is correct |
10 |
Correct |
140 ms |
32596 KB |
Output is correct |
11 |
Correct |
144 ms |
34588 KB |
Output is correct |
12 |
Correct |
84 ms |
31824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23640 KB |
Output is correct |
2 |
Correct |
4 ms |
23644 KB |
Output is correct |
3 |
Correct |
4 ms |
23640 KB |
Output is correct |
4 |
Correct |
5 ms |
23644 KB |
Output is correct |
5 |
Correct |
4 ms |
23644 KB |
Output is correct |
6 |
Correct |
5 ms |
23896 KB |
Output is correct |
7 |
Correct |
4 ms |
23644 KB |
Output is correct |
8 |
Correct |
4 ms |
23772 KB |
Output is correct |
9 |
Correct |
4 ms |
23644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23640 KB |
Output is correct |
2 |
Correct |
4 ms |
23644 KB |
Output is correct |
3 |
Correct |
4 ms |
23640 KB |
Output is correct |
4 |
Correct |
5 ms |
23644 KB |
Output is correct |
5 |
Correct |
4 ms |
23644 KB |
Output is correct |
6 |
Correct |
5 ms |
23896 KB |
Output is correct |
7 |
Correct |
4 ms |
23644 KB |
Output is correct |
8 |
Correct |
4 ms |
23772 KB |
Output is correct |
9 |
Correct |
4 ms |
23644 KB |
Output is correct |
10 |
Correct |
4 ms |
23644 KB |
Output is correct |
11 |
Correct |
4 ms |
23640 KB |
Output is correct |
12 |
Correct |
5 ms |
23644 KB |
Output is correct |
13 |
Correct |
5 ms |
23644 KB |
Output is correct |
14 |
Correct |
4 ms |
23644 KB |
Output is correct |
15 |
Correct |
4 ms |
23640 KB |
Output is correct |
16 |
Correct |
4 ms |
23644 KB |
Output is correct |
17 |
Correct |
4 ms |
23872 KB |
Output is correct |
18 |
Correct |
4 ms |
23644 KB |
Output is correct |
19 |
Correct |
30 ms |
25560 KB |
Output is correct |
20 |
Correct |
28 ms |
25724 KB |
Output is correct |
21 |
Correct |
36 ms |
25740 KB |
Output is correct |
22 |
Correct |
23 ms |
25680 KB |
Output is correct |
23 |
Correct |
24 ms |
25692 KB |
Output is correct |
24 |
Correct |
22 ms |
25948 KB |
Output is correct |
25 |
Correct |
27 ms |
25436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23640 KB |
Output is correct |
2 |
Correct |
4 ms |
23644 KB |
Output is correct |
3 |
Correct |
4 ms |
23640 KB |
Output is correct |
4 |
Correct |
5 ms |
23644 KB |
Output is correct |
5 |
Correct |
4 ms |
23644 KB |
Output is correct |
6 |
Correct |
5 ms |
23896 KB |
Output is correct |
7 |
Correct |
4 ms |
23644 KB |
Output is correct |
8 |
Correct |
4 ms |
23772 KB |
Output is correct |
9 |
Correct |
4 ms |
23644 KB |
Output is correct |
10 |
Correct |
4 ms |
23952 KB |
Output is correct |
11 |
Correct |
3 ms |
23644 KB |
Output is correct |
12 |
Correct |
5 ms |
23640 KB |
Output is correct |
13 |
Correct |
4 ms |
23644 KB |
Output is correct |
14 |
Correct |
4 ms |
23644 KB |
Output is correct |
15 |
Correct |
4 ms |
23644 KB |
Output is correct |
16 |
Correct |
5 ms |
23788 KB |
Output is correct |
17 |
Correct |
4 ms |
23644 KB |
Output is correct |
18 |
Correct |
4 ms |
23644 KB |
Output is correct |
19 |
Correct |
114 ms |
30548 KB |
Output is correct |
20 |
Correct |
115 ms |
29596 KB |
Output is correct |
21 |
Correct |
129 ms |
30300 KB |
Output is correct |
22 |
Correct |
125 ms |
32124 KB |
Output is correct |
23 |
Correct |
162 ms |
35052 KB |
Output is correct |
24 |
Correct |
154 ms |
35248 KB |
Output is correct |
25 |
Correct |
36 ms |
25168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
142 ms |
29108 KB |
Output is correct |
2 |
Correct |
144 ms |
29092 KB |
Output is correct |
3 |
Correct |
153 ms |
29012 KB |
Output is correct |
4 |
Correct |
142 ms |
34424 KB |
Output is correct |
5 |
Correct |
152 ms |
32652 KB |
Output is correct |
6 |
Correct |
183 ms |
36944 KB |
Output is correct |
7 |
Correct |
181 ms |
36952 KB |
Output is correct |
8 |
Correct |
193 ms |
37164 KB |
Output is correct |
9 |
Correct |
146 ms |
35084 KB |
Output is correct |
10 |
Correct |
186 ms |
36648 KB |
Output is correct |
11 |
Correct |
190 ms |
36660 KB |
Output is correct |
12 |
Correct |
179 ms |
36612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
23644 KB |
Output is correct |
2 |
Correct |
134 ms |
32156 KB |
Output is correct |
3 |
Correct |
145 ms |
32280 KB |
Output is correct |
4 |
Correct |
153 ms |
32324 KB |
Output is correct |
5 |
Correct |
155 ms |
33004 KB |
Output is correct |
6 |
Correct |
147 ms |
33196 KB |
Output is correct |
7 |
Correct |
153 ms |
33424 KB |
Output is correct |
8 |
Correct |
153 ms |
37348 KB |
Output is correct |
9 |
Correct |
143 ms |
37356 KB |
Output is correct |
10 |
Correct |
140 ms |
32596 KB |
Output is correct |
11 |
Correct |
144 ms |
34588 KB |
Output is correct |
12 |
Correct |
84 ms |
31824 KB |
Output is correct |
13 |
Correct |
4 ms |
23640 KB |
Output is correct |
14 |
Correct |
4 ms |
23644 KB |
Output is correct |
15 |
Correct |
4 ms |
23640 KB |
Output is correct |
16 |
Correct |
5 ms |
23644 KB |
Output is correct |
17 |
Correct |
4 ms |
23644 KB |
Output is correct |
18 |
Correct |
5 ms |
23896 KB |
Output is correct |
19 |
Correct |
4 ms |
23644 KB |
Output is correct |
20 |
Correct |
4 ms |
23772 KB |
Output is correct |
21 |
Correct |
4 ms |
23644 KB |
Output is correct |
22 |
Correct |
4 ms |
23644 KB |
Output is correct |
23 |
Correct |
4 ms |
23640 KB |
Output is correct |
24 |
Correct |
5 ms |
23644 KB |
Output is correct |
25 |
Correct |
5 ms |
23644 KB |
Output is correct |
26 |
Correct |
4 ms |
23644 KB |
Output is correct |
27 |
Correct |
4 ms |
23640 KB |
Output is correct |
28 |
Correct |
4 ms |
23644 KB |
Output is correct |
29 |
Correct |
4 ms |
23872 KB |
Output is correct |
30 |
Correct |
4 ms |
23644 KB |
Output is correct |
31 |
Correct |
30 ms |
25560 KB |
Output is correct |
32 |
Correct |
28 ms |
25724 KB |
Output is correct |
33 |
Correct |
36 ms |
25740 KB |
Output is correct |
34 |
Correct |
23 ms |
25680 KB |
Output is correct |
35 |
Correct |
24 ms |
25692 KB |
Output is correct |
36 |
Correct |
22 ms |
25948 KB |
Output is correct |
37 |
Correct |
27 ms |
25436 KB |
Output is correct |
38 |
Correct |
4 ms |
23952 KB |
Output is correct |
39 |
Correct |
3 ms |
23644 KB |
Output is correct |
40 |
Correct |
5 ms |
23640 KB |
Output is correct |
41 |
Correct |
4 ms |
23644 KB |
Output is correct |
42 |
Correct |
4 ms |
23644 KB |
Output is correct |
43 |
Correct |
4 ms |
23644 KB |
Output is correct |
44 |
Correct |
5 ms |
23788 KB |
Output is correct |
45 |
Correct |
4 ms |
23644 KB |
Output is correct |
46 |
Correct |
4 ms |
23644 KB |
Output is correct |
47 |
Correct |
114 ms |
30548 KB |
Output is correct |
48 |
Correct |
115 ms |
29596 KB |
Output is correct |
49 |
Correct |
129 ms |
30300 KB |
Output is correct |
50 |
Correct |
125 ms |
32124 KB |
Output is correct |
51 |
Correct |
162 ms |
35052 KB |
Output is correct |
52 |
Correct |
154 ms |
35248 KB |
Output is correct |
53 |
Correct |
36 ms |
25168 KB |
Output is correct |
54 |
Correct |
142 ms |
29108 KB |
Output is correct |
55 |
Correct |
144 ms |
29092 KB |
Output is correct |
56 |
Correct |
153 ms |
29012 KB |
Output is correct |
57 |
Correct |
142 ms |
34424 KB |
Output is correct |
58 |
Correct |
152 ms |
32652 KB |
Output is correct |
59 |
Correct |
183 ms |
36944 KB |
Output is correct |
60 |
Correct |
181 ms |
36952 KB |
Output is correct |
61 |
Correct |
193 ms |
37164 KB |
Output is correct |
62 |
Correct |
146 ms |
35084 KB |
Output is correct |
63 |
Correct |
186 ms |
36648 KB |
Output is correct |
64 |
Correct |
190 ms |
36660 KB |
Output is correct |
65 |
Correct |
179 ms |
36612 KB |
Output is correct |
66 |
Correct |
4 ms |
23640 KB |
Output is correct |
67 |
Correct |
133 ms |
32340 KB |
Output is correct |
68 |
Correct |
148 ms |
32340 KB |
Output is correct |
69 |
Correct |
151 ms |
32276 KB |
Output is correct |
70 |
Correct |
151 ms |
32600 KB |
Output is correct |
71 |
Correct |
170 ms |
33092 KB |
Output is correct |
72 |
Correct |
145 ms |
33360 KB |
Output is correct |
73 |
Correct |
151 ms |
37304 KB |
Output is correct |
74 |
Correct |
156 ms |
37356 KB |
Output is correct |
75 |
Correct |
141 ms |
32596 KB |
Output is correct |
76 |
Correct |
148 ms |
34424 KB |
Output is correct |
77 |
Correct |
88 ms |
31888 KB |
Output is correct |
78 |
Correct |
3 ms |
23644 KB |
Output is correct |
79 |
Correct |
4 ms |
23644 KB |
Output is correct |
80 |
Correct |
4 ms |
23640 KB |
Output is correct |
81 |
Correct |
4 ms |
23644 KB |
Output is correct |
82 |
Correct |
4 ms |
23832 KB |
Output is correct |
83 |
Correct |
5 ms |
23644 KB |
Output is correct |
84 |
Correct |
5 ms |
23644 KB |
Output is correct |
85 |
Correct |
4 ms |
23644 KB |
Output is correct |
86 |
Correct |
31 ms |
25540 KB |
Output is correct |
87 |
Correct |
27 ms |
25424 KB |
Output is correct |
88 |
Correct |
30 ms |
25728 KB |
Output is correct |
89 |
Correct |
22 ms |
25692 KB |
Output is correct |
90 |
Correct |
24 ms |
25956 KB |
Output is correct |
91 |
Correct |
22 ms |
25756 KB |
Output is correct |
92 |
Correct |
26 ms |
25436 KB |
Output is correct |
93 |
Correct |
118 ms |
30804 KB |
Output is correct |
94 |
Correct |
111 ms |
29776 KB |
Output is correct |
95 |
Correct |
129 ms |
30292 KB |
Output is correct |
96 |
Correct |
135 ms |
32104 KB |
Output is correct |
97 |
Correct |
159 ms |
35236 KB |
Output is correct |
98 |
Correct |
155 ms |
35232 KB |
Output is correct |
99 |
Correct |
37 ms |
25172 KB |
Output is correct |
100 |
Correct |
179 ms |
37068 KB |
Output is correct |
101 |
Correct |
192 ms |
37216 KB |
Output is correct |
102 |
Correct |
186 ms |
37200 KB |
Output is correct |
103 |
Correct |
192 ms |
36812 KB |
Output is correct |
104 |
Correct |
198 ms |
36480 KB |
Output is correct |
105 |
Correct |
183 ms |
36592 KB |
Output is correct |
106 |
Correct |
142 ms |
30800 KB |
Output is correct |
107 |
Correct |
146 ms |
31572 KB |
Output is correct |
108 |
Correct |
151 ms |
32340 KB |
Output is correct |
109 |
Correct |
155 ms |
32336 KB |
Output is correct |
110 |
Correct |
196 ms |
36840 KB |
Output is correct |
111 |
Correct |
240 ms |
36956 KB |
Output is correct |