#include<bits/stdc++.h>
using namespace std;
#define SIZE 100005
struct rg{
int id,l,r;
bool operator<(rg x){
if(r!=x.r) return r<x.r;
return l<x.l;
}
}rgs[SIZE];
int n,q;
int arr[SIZE];
int lb(int x){return x&-x;}
int minl(int a, int b){return rgs[a].l<rgs[b].l?a:b;}
void edit(int loc, int v){
while(loc<=n){
arr[loc]=minl(arr[loc],v);
loc+=lb(loc);
}
}
int get(int loc){
int ret=0;
while(loc){
ret=minl(ret,arr[loc]);
loc-=lb(loc);
}
return ret;
}
int rid[SIZE];
int irg[SIZE];
int bz[20][SIZE];
void cal(int id){
int l=1,r=id,md;
while(l<r){
md=(l+r)>>1;
if(rgs[md].r<rgs[id].l) l=md+1;
else r=md;
}
if(r==id) bz[0][id]=id;
else bz[0][id]=get(n-r+1);
irg[id]=r;
edit(n-id+1,id);
}
int PID;
int build(){
int pid,p;
for(pid=1,p=2;p<=n;p<<=1,pid++){
for(int i=1;i<=n;i++){
bz[pid][i]=bz[pid-1][bz[pid-1][i]];
}
}
return pid;
}
void query(){
int u,a;
cin>>u>>a;
a=rid[a];
u=rid[u];
if(rgs[a].r==rgs[u].r){
printf("%d\n",int(a!=u));
return;
}
if(a<u){
printf("impossible\n");
return;
}
int cnt=0;
for(int pid=PID,p=1<<PID;pid>=0;pid--,p>>=1){
if(u<irg[bz[pid][a]]){
a=bz[pid][a];
cnt+=p;
}
}
if(irg[a]<=u){
printf("1\n");
return;
}
a=bz[0][a];
if(u < irg[a]) printf("impossible\n");
else printf("%d\n",cnt+2);
return;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++){
rgs[i].id=i;
cin>>rgs[i].l>>rgs[i].r;
}
sort(rgs+1,rgs+1+n);
rgs[0].l=2e9;
for(int i=1;i<=n;i++){
rid[rgs[i].id]=i;
cal(i);
}
PID=build();
while(q--) query();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
62 ms |
14132 KB |
Output is correct |
3 |
Correct |
48 ms |
14160 KB |
Output is correct |
4 |
Correct |
52 ms |
14260 KB |
Output is correct |
5 |
Correct |
55 ms |
14160 KB |
Output is correct |
6 |
Correct |
53 ms |
14164 KB |
Output is correct |
7 |
Correct |
51 ms |
14360 KB |
Output is correct |
8 |
Correct |
46 ms |
14672 KB |
Output is correct |
9 |
Correct |
50 ms |
14796 KB |
Output is correct |
10 |
Correct |
54 ms |
14672 KB |
Output is correct |
11 |
Correct |
56 ms |
14672 KB |
Output is correct |
12 |
Correct |
39 ms |
13916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
6668 KB |
Output is correct |
4 |
Correct |
1 ms |
6648 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
6668 KB |
Output is correct |
4 |
Correct |
1 ms |
6648 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 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 |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4556 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6648 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 |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
23 ms |
10204 KB |
Output is correct |
20 |
Correct |
20 ms |
10076 KB |
Output is correct |
21 |
Correct |
25 ms |
10332 KB |
Output is correct |
22 |
Correct |
19 ms |
10584 KB |
Output is correct |
23 |
Correct |
17 ms |
10332 KB |
Output is correct |
24 |
Correct |
18 ms |
10332 KB |
Output is correct |
25 |
Correct |
19 ms |
10076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
6668 KB |
Output is correct |
4 |
Correct |
1 ms |
6648 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6488 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 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 |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 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 |
2 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
2 ms |
7004 KB |
Output is correct |
18 |
Correct |
1 ms |
6488 KB |
Output is correct |
19 |
Correct |
33 ms |
12376 KB |
Output is correct |
20 |
Correct |
25 ms |
11608 KB |
Output is correct |
21 |
Correct |
32 ms |
12360 KB |
Output is correct |
22 |
Correct |
28 ms |
12368 KB |
Output is correct |
23 |
Correct |
27 ms |
12380 KB |
Output is correct |
24 |
Correct |
29 ms |
12536 KB |
Output is correct |
25 |
Correct |
19 ms |
11868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
14156 KB |
Output is correct |
2 |
Correct |
47 ms |
14160 KB |
Output is correct |
3 |
Correct |
58 ms |
14184 KB |
Output is correct |
4 |
Correct |
59 ms |
14580 KB |
Output is correct |
5 |
Correct |
56 ms |
14676 KB |
Output is correct |
6 |
Correct |
70 ms |
14416 KB |
Output is correct |
7 |
Correct |
55 ms |
14420 KB |
Output is correct |
8 |
Correct |
57 ms |
14416 KB |
Output is correct |
9 |
Correct |
27 ms |
12368 KB |
Output is correct |
10 |
Correct |
48 ms |
13904 KB |
Output is correct |
11 |
Correct |
52 ms |
13652 KB |
Output is correct |
12 |
Correct |
50 ms |
13904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
62 ms |
14132 KB |
Output is correct |
3 |
Correct |
48 ms |
14160 KB |
Output is correct |
4 |
Correct |
52 ms |
14260 KB |
Output is correct |
5 |
Correct |
55 ms |
14160 KB |
Output is correct |
6 |
Correct |
53 ms |
14164 KB |
Output is correct |
7 |
Correct |
51 ms |
14360 KB |
Output is correct |
8 |
Correct |
46 ms |
14672 KB |
Output is correct |
9 |
Correct |
50 ms |
14796 KB |
Output is correct |
10 |
Correct |
54 ms |
14672 KB |
Output is correct |
11 |
Correct |
56 ms |
14672 KB |
Output is correct |
12 |
Correct |
39 ms |
13916 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
6668 KB |
Output is correct |
16 |
Correct |
1 ms |
6648 KB |
Output is correct |
17 |
Correct |
2 ms |
6492 KB |
Output is correct |
18 |
Correct |
2 ms |
6488 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 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 |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4556 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6648 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 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 |
23 ms |
10204 KB |
Output is correct |
32 |
Correct |
20 ms |
10076 KB |
Output is correct |
33 |
Correct |
25 ms |
10332 KB |
Output is correct |
34 |
Correct |
19 ms |
10584 KB |
Output is correct |
35 |
Correct |
17 ms |
10332 KB |
Output is correct |
36 |
Correct |
18 ms |
10332 KB |
Output is correct |
37 |
Correct |
19 ms |
10076 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
4444 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
1 ms |
6492 KB |
Output is correct |
42 |
Correct |
1 ms |
6492 KB |
Output is correct |
43 |
Correct |
2 ms |
6492 KB |
Output is correct |
44 |
Correct |
1 ms |
6492 KB |
Output is correct |
45 |
Correct |
2 ms |
7004 KB |
Output is correct |
46 |
Correct |
1 ms |
6488 KB |
Output is correct |
47 |
Correct |
33 ms |
12376 KB |
Output is correct |
48 |
Correct |
25 ms |
11608 KB |
Output is correct |
49 |
Correct |
32 ms |
12360 KB |
Output is correct |
50 |
Correct |
28 ms |
12368 KB |
Output is correct |
51 |
Correct |
27 ms |
12380 KB |
Output is correct |
52 |
Correct |
29 ms |
12536 KB |
Output is correct |
53 |
Correct |
19 ms |
11868 KB |
Output is correct |
54 |
Correct |
57 ms |
14156 KB |
Output is correct |
55 |
Correct |
47 ms |
14160 KB |
Output is correct |
56 |
Correct |
58 ms |
14184 KB |
Output is correct |
57 |
Correct |
59 ms |
14580 KB |
Output is correct |
58 |
Correct |
56 ms |
14676 KB |
Output is correct |
59 |
Correct |
70 ms |
14416 KB |
Output is correct |
60 |
Correct |
55 ms |
14420 KB |
Output is correct |
61 |
Correct |
57 ms |
14416 KB |
Output is correct |
62 |
Correct |
27 ms |
12368 KB |
Output is correct |
63 |
Correct |
48 ms |
13904 KB |
Output is correct |
64 |
Correct |
52 ms |
13652 KB |
Output is correct |
65 |
Correct |
50 ms |
13904 KB |
Output is correct |
66 |
Correct |
1 ms |
4440 KB |
Output is correct |
67 |
Correct |
47 ms |
14164 KB |
Output is correct |
68 |
Correct |
48 ms |
14160 KB |
Output is correct |
69 |
Correct |
55 ms |
14088 KB |
Output is correct |
70 |
Correct |
51 ms |
14160 KB |
Output is correct |
71 |
Correct |
51 ms |
14160 KB |
Output is correct |
72 |
Correct |
50 ms |
14420 KB |
Output is correct |
73 |
Correct |
54 ms |
14676 KB |
Output is correct |
74 |
Correct |
46 ms |
14792 KB |
Output is correct |
75 |
Correct |
56 ms |
14484 KB |
Output is correct |
76 |
Correct |
55 ms |
14676 KB |
Output is correct |
77 |
Correct |
40 ms |
13988 KB |
Output is correct |
78 |
Correct |
1 ms |
4440 KB |
Output is correct |
79 |
Correct |
2 ms |
6492 KB |
Output is correct |
80 |
Correct |
1 ms |
6648 KB |
Output is correct |
81 |
Correct |
1 ms |
6492 KB |
Output is correct |
82 |
Correct |
1 ms |
6492 KB |
Output is correct |
83 |
Correct |
1 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 |
22 ms |
10004 KB |
Output is correct |
87 |
Correct |
20 ms |
10232 KB |
Output is correct |
88 |
Correct |
23 ms |
10460 KB |
Output is correct |
89 |
Correct |
18 ms |
10584 KB |
Output is correct |
90 |
Correct |
17 ms |
10328 KB |
Output is correct |
91 |
Correct |
21 ms |
10332 KB |
Output is correct |
92 |
Correct |
19 ms |
9952 KB |
Output is correct |
93 |
Correct |
30 ms |
12472 KB |
Output is correct |
94 |
Correct |
25 ms |
11600 KB |
Output is correct |
95 |
Correct |
31 ms |
12368 KB |
Output is correct |
96 |
Correct |
29 ms |
12380 KB |
Output is correct |
97 |
Correct |
29 ms |
12364 KB |
Output is correct |
98 |
Correct |
28 ms |
12372 KB |
Output is correct |
99 |
Correct |
20 ms |
11860 KB |
Output is correct |
100 |
Correct |
57 ms |
14368 KB |
Output is correct |
101 |
Correct |
58 ms |
14416 KB |
Output is correct |
102 |
Correct |
56 ms |
14416 KB |
Output is correct |
103 |
Correct |
46 ms |
13904 KB |
Output is correct |
104 |
Correct |
49 ms |
13648 KB |
Output is correct |
105 |
Correct |
48 ms |
13908 KB |
Output is correct |
106 |
Correct |
54 ms |
13652 KB |
Output is correct |
107 |
Correct |
60 ms |
13512 KB |
Output is correct |
108 |
Correct |
51 ms |
14412 KB |
Output is correct |
109 |
Correct |
52 ms |
14416 KB |
Output is correct |
110 |
Correct |
56 ms |
14288 KB |
Output is correct |
111 |
Correct |
57 ms |
14196 KB |
Output is correct |