#include "bits/stdc++.h"
#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
using namespace std;
const int N = 2e5 + 5;
const int LOG = 20;
vector<array<int,2>> v(N);
vector<int> zip;
int seg[4*N],lift[N][LOG];
int merge(int a,int b){
if(!a || !b) return (!a ? b : a);
if(v[a][0]<v[b][0]) return a;
return b;
}
void upd(int rt,int l,int r,int ind,int u){
if(r<ind || l>ind) return;
if(l==r){
seg[rt]=merge(seg[rt],u);
return;
}
int m=(l+r)/2;
upd(rt*2,l,m,ind,u),upd(rt*2+1,m+1,r,ind,u);
seg[rt]=merge(seg[rt*2],seg[rt*2+1]);
}
int query(int rt,int l,int r,int gl,int gr){
if(r<gl || l>gr) return 0;
if(l>=gl && r<=gr) return seg[rt];
int m=(l+r)/2;
return merge(query(rt*2,l,m,gl,gr),query(rt*2+1,m+1,r,gl,gr));
}
void _(){
int n,q;
cin >> n >> q;
for(int i=1;i<=n;i++){
int l,r;
cin >> l >> r;
v[i]={l,r};
zip.push_back(l);
zip.push_back(r);
}
sort(all(zip));
zip.erase(unique(all(zip)),zip.end());
for(int i=1;i<=n;i++){
v[i][0]=(lower_bound(all(zip),v[i][0])-zip.begin())+1;
v[i][1]=(lower_bound(all(zip),v[i][1])-zip.begin())+1;
}
vector<int> ans(q+5,-1);
vector<int> lmao[N];
for(int i=1;i<=n;i++) lmao[v[i][1]].push_back(i);
for(int i=1;i<N;i++){
for(int u:lmao[i]) upd(1,1,N,v[u][1],u);
for(int u:lmao[i]){
int xd = query(1,1,N,v[u][0],v[u][1]);
if(xd && v[xd][0] < v[u][0]) lift[u][0]=xd;
}
}
for(int j=1;j<LOG;j++){
for(int i=1;i<=n;i++){
lift[i][j]=lift[lift[i][j-1]][j-1];
}
}
for(int j=1;j<=q;j++){
int a,b,ind = j;
cin >> a >> b;
if(a==b){
ans[ind]=0;
continue;
}
if(v[a][1]>v[b][1]) continue;
if(v[a][1]>=v[b][0]){
ans[ind]=1;
continue;
}
int res=0,p=b;
for(int i=LOG-1;i>=0;i--){
if(!lift[p][i]) continue;
if(v[lift[p][i]][0]>v[a][1]){
p=lift[p][i];
res+=(1LL<<i);
}
}
//cout << "ulan: " << ind << ' ' << p << '\n';
if(lift[p][0] && v[p][0]>v[a][1]){
res++;
p=lift[p][0];
}
if(v[p][0]<=v[a][1]) ans[ind]=res+1;
}
for(int i=1;i<=q;i++){
if(ans[i]==-1) cout << "impossible\n";
else cout << ans[i] << '\n';
}
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int tc=1;//cin >> tc;
while(tc--) _();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8284 KB |
Output is correct |
2 |
Correct |
116 ms |
35200 KB |
Output is correct |
3 |
Correct |
125 ms |
35200 KB |
Output is correct |
4 |
Correct |
141 ms |
35020 KB |
Output is correct |
5 |
Correct |
131 ms |
35276 KB |
Output is correct |
6 |
Correct |
131 ms |
35528 KB |
Output is correct |
7 |
Correct |
134 ms |
35528 KB |
Output is correct |
8 |
Correct |
113 ms |
37576 KB |
Output is correct |
9 |
Correct |
111 ms |
37656 KB |
Output is correct |
10 |
Correct |
134 ms |
35564 KB |
Output is correct |
11 |
Correct |
127 ms |
36300 KB |
Output is correct |
12 |
Correct |
78 ms |
33184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8108 KB |
Output is correct |
2 |
Correct |
4 ms |
8244 KB |
Output is correct |
3 |
Correct |
5 ms |
8540 KB |
Output is correct |
4 |
Correct |
5 ms |
8540 KB |
Output is correct |
5 |
Correct |
5 ms |
8540 KB |
Output is correct |
6 |
Correct |
5 ms |
8540 KB |
Output is correct |
7 |
Correct |
6 ms |
8608 KB |
Output is correct |
8 |
Correct |
5 ms |
8540 KB |
Output is correct |
9 |
Correct |
6 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8108 KB |
Output is correct |
2 |
Correct |
4 ms |
8244 KB |
Output is correct |
3 |
Correct |
5 ms |
8540 KB |
Output is correct |
4 |
Correct |
5 ms |
8540 KB |
Output is correct |
5 |
Correct |
5 ms |
8540 KB |
Output is correct |
6 |
Correct |
5 ms |
8540 KB |
Output is correct |
7 |
Correct |
6 ms |
8608 KB |
Output is correct |
8 |
Correct |
5 ms |
8540 KB |
Output is correct |
9 |
Correct |
6 ms |
8540 KB |
Output is correct |
10 |
Correct |
5 ms |
8284 KB |
Output is correct |
11 |
Correct |
5 ms |
8284 KB |
Output is correct |
12 |
Correct |
5 ms |
8540 KB |
Output is correct |
13 |
Correct |
5 ms |
8536 KB |
Output is correct |
14 |
Correct |
5 ms |
8792 KB |
Output is correct |
15 |
Correct |
8 ms |
8536 KB |
Output is correct |
16 |
Correct |
6 ms |
8448 KB |
Output is correct |
17 |
Correct |
5 ms |
8540 KB |
Output is correct |
18 |
Correct |
5 ms |
8540 KB |
Output is correct |
19 |
Correct |
27 ms |
11612 KB |
Output is correct |
20 |
Correct |
29 ms |
11608 KB |
Output is correct |
21 |
Correct |
32 ms |
11856 KB |
Output is correct |
22 |
Correct |
34 ms |
11852 KB |
Output is correct |
23 |
Correct |
25 ms |
12124 KB |
Output is correct |
24 |
Correct |
33 ms |
11868 KB |
Output is correct |
25 |
Correct |
26 ms |
11604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8108 KB |
Output is correct |
2 |
Correct |
4 ms |
8244 KB |
Output is correct |
3 |
Correct |
5 ms |
8540 KB |
Output is correct |
4 |
Correct |
5 ms |
8540 KB |
Output is correct |
5 |
Correct |
5 ms |
8540 KB |
Output is correct |
6 |
Correct |
5 ms |
8540 KB |
Output is correct |
7 |
Correct |
6 ms |
8608 KB |
Output is correct |
8 |
Correct |
5 ms |
8540 KB |
Output is correct |
9 |
Correct |
6 ms |
8540 KB |
Output is correct |
10 |
Correct |
5 ms |
8280 KB |
Output is correct |
11 |
Correct |
5 ms |
8284 KB |
Output is correct |
12 |
Correct |
5 ms |
8572 KB |
Output is correct |
13 |
Correct |
6 ms |
8540 KB |
Output is correct |
14 |
Correct |
9 ms |
8364 KB |
Output is correct |
15 |
Correct |
7 ms |
8488 KB |
Output is correct |
16 |
Correct |
6 ms |
8536 KB |
Output is correct |
17 |
Correct |
6 ms |
8572 KB |
Output is correct |
18 |
Correct |
5 ms |
8312 KB |
Output is correct |
19 |
Correct |
107 ms |
32552 KB |
Output is correct |
20 |
Correct |
101 ms |
31632 KB |
Output is correct |
21 |
Correct |
114 ms |
31032 KB |
Output is correct |
22 |
Correct |
106 ms |
33232 KB |
Output is correct |
23 |
Correct |
119 ms |
32676 KB |
Output is correct |
24 |
Correct |
141 ms |
34508 KB |
Output is correct |
25 |
Correct |
52 ms |
28608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
128 ms |
35156 KB |
Output is correct |
2 |
Correct |
149 ms |
35036 KB |
Output is correct |
3 |
Correct |
147 ms |
35192 KB |
Output is correct |
4 |
Correct |
114 ms |
37576 KB |
Output is correct |
5 |
Correct |
132 ms |
35352 KB |
Output is correct |
6 |
Correct |
140 ms |
37324 KB |
Output is correct |
7 |
Correct |
138 ms |
37236 KB |
Output is correct |
8 |
Correct |
147 ms |
37328 KB |
Output is correct |
9 |
Correct |
108 ms |
32672 KB |
Output is correct |
10 |
Correct |
135 ms |
37008 KB |
Output is correct |
11 |
Correct |
136 ms |
36168 KB |
Output is correct |
12 |
Correct |
139 ms |
36808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8284 KB |
Output is correct |
2 |
Correct |
116 ms |
35200 KB |
Output is correct |
3 |
Correct |
125 ms |
35200 KB |
Output is correct |
4 |
Correct |
141 ms |
35020 KB |
Output is correct |
5 |
Correct |
131 ms |
35276 KB |
Output is correct |
6 |
Correct |
131 ms |
35528 KB |
Output is correct |
7 |
Correct |
134 ms |
35528 KB |
Output is correct |
8 |
Correct |
113 ms |
37576 KB |
Output is correct |
9 |
Correct |
111 ms |
37656 KB |
Output is correct |
10 |
Correct |
134 ms |
35564 KB |
Output is correct |
11 |
Correct |
127 ms |
36300 KB |
Output is correct |
12 |
Correct |
78 ms |
33184 KB |
Output is correct |
13 |
Correct |
5 ms |
8108 KB |
Output is correct |
14 |
Correct |
4 ms |
8244 KB |
Output is correct |
15 |
Correct |
5 ms |
8540 KB |
Output is correct |
16 |
Correct |
5 ms |
8540 KB |
Output is correct |
17 |
Correct |
5 ms |
8540 KB |
Output is correct |
18 |
Correct |
5 ms |
8540 KB |
Output is correct |
19 |
Correct |
6 ms |
8608 KB |
Output is correct |
20 |
Correct |
5 ms |
8540 KB |
Output is correct |
21 |
Correct |
6 ms |
8540 KB |
Output is correct |
22 |
Correct |
5 ms |
8284 KB |
Output is correct |
23 |
Correct |
5 ms |
8284 KB |
Output is correct |
24 |
Correct |
5 ms |
8540 KB |
Output is correct |
25 |
Correct |
5 ms |
8536 KB |
Output is correct |
26 |
Correct |
5 ms |
8792 KB |
Output is correct |
27 |
Correct |
8 ms |
8536 KB |
Output is correct |
28 |
Correct |
6 ms |
8448 KB |
Output is correct |
29 |
Correct |
5 ms |
8540 KB |
Output is correct |
30 |
Correct |
5 ms |
8540 KB |
Output is correct |
31 |
Correct |
27 ms |
11612 KB |
Output is correct |
32 |
Correct |
29 ms |
11608 KB |
Output is correct |
33 |
Correct |
32 ms |
11856 KB |
Output is correct |
34 |
Correct |
34 ms |
11852 KB |
Output is correct |
35 |
Correct |
25 ms |
12124 KB |
Output is correct |
36 |
Correct |
33 ms |
11868 KB |
Output is correct |
37 |
Correct |
26 ms |
11604 KB |
Output is correct |
38 |
Correct |
5 ms |
8280 KB |
Output is correct |
39 |
Correct |
5 ms |
8284 KB |
Output is correct |
40 |
Correct |
5 ms |
8572 KB |
Output is correct |
41 |
Correct |
6 ms |
8540 KB |
Output is correct |
42 |
Correct |
9 ms |
8364 KB |
Output is correct |
43 |
Correct |
7 ms |
8488 KB |
Output is correct |
44 |
Correct |
6 ms |
8536 KB |
Output is correct |
45 |
Correct |
6 ms |
8572 KB |
Output is correct |
46 |
Correct |
5 ms |
8312 KB |
Output is correct |
47 |
Correct |
107 ms |
32552 KB |
Output is correct |
48 |
Correct |
101 ms |
31632 KB |
Output is correct |
49 |
Correct |
114 ms |
31032 KB |
Output is correct |
50 |
Correct |
106 ms |
33232 KB |
Output is correct |
51 |
Correct |
119 ms |
32676 KB |
Output is correct |
52 |
Correct |
141 ms |
34508 KB |
Output is correct |
53 |
Correct |
52 ms |
28608 KB |
Output is correct |
54 |
Correct |
128 ms |
35156 KB |
Output is correct |
55 |
Correct |
149 ms |
35036 KB |
Output is correct |
56 |
Correct |
147 ms |
35192 KB |
Output is correct |
57 |
Correct |
114 ms |
37576 KB |
Output is correct |
58 |
Correct |
132 ms |
35352 KB |
Output is correct |
59 |
Correct |
140 ms |
37324 KB |
Output is correct |
60 |
Correct |
138 ms |
37236 KB |
Output is correct |
61 |
Correct |
147 ms |
37328 KB |
Output is correct |
62 |
Correct |
108 ms |
32672 KB |
Output is correct |
63 |
Correct |
135 ms |
37008 KB |
Output is correct |
64 |
Correct |
136 ms |
36168 KB |
Output is correct |
65 |
Correct |
139 ms |
36808 KB |
Output is correct |
66 |
Correct |
4 ms |
8280 KB |
Output is correct |
67 |
Correct |
115 ms |
35016 KB |
Output is correct |
68 |
Correct |
136 ms |
35016 KB |
Output is correct |
69 |
Correct |
147 ms |
35016 KB |
Output is correct |
70 |
Correct |
151 ms |
35440 KB |
Output is correct |
71 |
Correct |
133 ms |
35640 KB |
Output is correct |
72 |
Correct |
139 ms |
35692 KB |
Output is correct |
73 |
Correct |
123 ms |
37576 KB |
Output is correct |
74 |
Correct |
109 ms |
37576 KB |
Output is correct |
75 |
Correct |
135 ms |
35448 KB |
Output is correct |
76 |
Correct |
143 ms |
36212 KB |
Output is correct |
77 |
Correct |
81 ms |
33224 KB |
Output is correct |
78 |
Correct |
4 ms |
8280 KB |
Output is correct |
79 |
Correct |
5 ms |
8536 KB |
Output is correct |
80 |
Correct |
5 ms |
8364 KB |
Output is correct |
81 |
Correct |
6 ms |
8536 KB |
Output is correct |
82 |
Correct |
5 ms |
8508 KB |
Output is correct |
83 |
Correct |
5 ms |
8536 KB |
Output is correct |
84 |
Correct |
7 ms |
8540 KB |
Output is correct |
85 |
Correct |
5 ms |
8536 KB |
Output is correct |
86 |
Correct |
28 ms |
11644 KB |
Output is correct |
87 |
Correct |
26 ms |
11612 KB |
Output is correct |
88 |
Correct |
32 ms |
11860 KB |
Output is correct |
89 |
Correct |
25 ms |
11860 KB |
Output is correct |
90 |
Correct |
29 ms |
11856 KB |
Output is correct |
91 |
Correct |
22 ms |
11856 KB |
Output is correct |
92 |
Correct |
23 ms |
11612 KB |
Output is correct |
93 |
Correct |
98 ms |
32460 KB |
Output is correct |
94 |
Correct |
95 ms |
31688 KB |
Output is correct |
95 |
Correct |
99 ms |
31176 KB |
Output is correct |
96 |
Correct |
96 ms |
33228 KB |
Output is correct |
97 |
Correct |
106 ms |
32456 KB |
Output is correct |
98 |
Correct |
111 ms |
34532 KB |
Output is correct |
99 |
Correct |
53 ms |
28616 KB |
Output is correct |
100 |
Correct |
144 ms |
37244 KB |
Output is correct |
101 |
Correct |
159 ms |
37332 KB |
Output is correct |
102 |
Correct |
142 ms |
37320 KB |
Output is correct |
103 |
Correct |
137 ms |
36968 KB |
Output is correct |
104 |
Correct |
133 ms |
36232 KB |
Output is correct |
105 |
Correct |
146 ms |
36804 KB |
Output is correct |
106 |
Correct |
128 ms |
33476 KB |
Output is correct |
107 |
Correct |
134 ms |
34504 KB |
Output is correct |
108 |
Correct |
127 ms |
33992 KB |
Output is correct |
109 |
Correct |
127 ms |
33876 KB |
Output is correct |
110 |
Correct |
164 ms |
37064 KB |
Output is correct |
111 |
Correct |
155 ms |
37060 KB |
Output is correct |