#include <bits/stdc++.h>
#define x first
#define y second
#define pii pair<int,int>
using namespace std;
using ll=long long;
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
#define all(a) begin(a),end(a)
const int N=100010,MOD=1e9+7,M=1<<17;
const char en='\n';
const ll LLINF=1ll<<60;
int n,q,l[N],r[N],par[20][N];
pii seg[M*2+10];
pii ge(int l,int r,int lo=0,int hi=M,int i=1)
{
if (lo>=l && hi<=r) return seg[i];
if (lo>=r || hi<=l) return {MOD,MOD};
int mid=(lo+hi)/2;
return min(ge(l,r,lo,mid,i*2),ge(l,r,mid,hi,i*2+1));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
for (int i=0;i<2*M;++i) seg[i]={MOD,MOD};
cin>>n>>q;
vector<pair<pii,int>> v;
for (int i=0;i<n;++i)
{
cin>>l[i]>>r[i];
v.pb({{r[i],l[i]},i});
}
sort(all(v));
for (int i=0;i<n;++i) seg[i+M]={v[i].x.y,v[i].y};
for (int i=M-1;i;--i) seg[i]=min(seg[i*2],seg[i*2+1]);
for (int i=0;i<n;++i)
{
int poc=lower_bound(all(v),pair<pii,int>(pii(l[i],0),0))-v.begin();
int kr=lower_bound(all(v),pair<pii,int>(pii(r[i],MOD),MOD))-v.begin();
pii re=ge(poc,kr);
par[0][i]=re.y;
//cout<<i<<' '<<re.y<<en;
}
for (int j=0;j<17;++j)
{
for (int i=0;i<n;++i) par[j+1][i]=par[j][par[j][i]];
}
while (q--)
{
int a,b;
cin>>a>>b;
--a;
--b;
if (a==b)
{
cout<<0<<en;
continue;
}
if (r[a]>r[b])
{
cout<<"impossible\n";
continue;
}
int an=0;
for (int j=17;j>=0;--j) if (l[par[j][b]]>r[a]) an+=1<<j,b=par[j][b];
if (l[b]>r[a]) ++an,b=par[0][b];
if (l[b]>r[a])
{
cout<<"impossible\n";
continue;
}
cout<<an+1<<en;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2388 KB |
Output is correct |
2 |
Correct |
121 ms |
12084 KB |
Output is correct |
3 |
Correct |
145 ms |
11992 KB |
Output is correct |
4 |
Correct |
213 ms |
11972 KB |
Output is correct |
5 |
Correct |
144 ms |
12124 KB |
Output is correct |
6 |
Correct |
134 ms |
12092 KB |
Output is correct |
7 |
Correct |
163 ms |
12136 KB |
Output is correct |
8 |
Correct |
178 ms |
12596 KB |
Output is correct |
9 |
Correct |
176 ms |
12604 KB |
Output is correct |
10 |
Correct |
148 ms |
12424 KB |
Output is correct |
11 |
Correct |
162 ms |
12436 KB |
Output is correct |
12 |
Correct |
90 ms |
12596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2516 KB |
Output is correct |
4 |
Correct |
3 ms |
2516 KB |
Output is correct |
5 |
Correct |
3 ms |
2516 KB |
Output is correct |
6 |
Correct |
3 ms |
2516 KB |
Output is correct |
7 |
Correct |
3 ms |
2516 KB |
Output is correct |
8 |
Correct |
2 ms |
2516 KB |
Output is correct |
9 |
Correct |
3 ms |
2516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2516 KB |
Output is correct |
4 |
Correct |
3 ms |
2516 KB |
Output is correct |
5 |
Correct |
3 ms |
2516 KB |
Output is correct |
6 |
Correct |
3 ms |
2516 KB |
Output is correct |
7 |
Correct |
3 ms |
2516 KB |
Output is correct |
8 |
Correct |
2 ms |
2516 KB |
Output is correct |
9 |
Correct |
3 ms |
2516 KB |
Output is correct |
10 |
Correct |
2 ms |
2388 KB |
Output is correct |
11 |
Correct |
2 ms |
2388 KB |
Output is correct |
12 |
Correct |
3 ms |
2516 KB |
Output is correct |
13 |
Correct |
3 ms |
2516 KB |
Output is correct |
14 |
Correct |
4 ms |
2536 KB |
Output is correct |
15 |
Correct |
4 ms |
2516 KB |
Output is correct |
16 |
Correct |
3 ms |
2516 KB |
Output is correct |
17 |
Correct |
4 ms |
2516 KB |
Output is correct |
18 |
Correct |
4 ms |
2552 KB |
Output is correct |
19 |
Correct |
45 ms |
3412 KB |
Output is correct |
20 |
Correct |
44 ms |
3480 KB |
Output is correct |
21 |
Correct |
44 ms |
3712 KB |
Output is correct |
22 |
Correct |
44 ms |
3796 KB |
Output is correct |
23 |
Correct |
43 ms |
3628 KB |
Output is correct |
24 |
Correct |
34 ms |
3548 KB |
Output is correct |
25 |
Correct |
40 ms |
3188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2516 KB |
Output is correct |
4 |
Correct |
3 ms |
2516 KB |
Output is correct |
5 |
Correct |
3 ms |
2516 KB |
Output is correct |
6 |
Correct |
3 ms |
2516 KB |
Output is correct |
7 |
Correct |
3 ms |
2516 KB |
Output is correct |
8 |
Correct |
2 ms |
2516 KB |
Output is correct |
9 |
Correct |
3 ms |
2516 KB |
Output is correct |
10 |
Correct |
3 ms |
2388 KB |
Output is correct |
11 |
Correct |
2 ms |
2388 KB |
Output is correct |
12 |
Correct |
3 ms |
2516 KB |
Output is correct |
13 |
Correct |
4 ms |
2516 KB |
Output is correct |
14 |
Correct |
3 ms |
2516 KB |
Output is correct |
15 |
Correct |
3 ms |
2516 KB |
Output is correct |
16 |
Correct |
2 ms |
2516 KB |
Output is correct |
17 |
Correct |
2 ms |
2516 KB |
Output is correct |
18 |
Correct |
3 ms |
2516 KB |
Output is correct |
19 |
Correct |
94 ms |
11460 KB |
Output is correct |
20 |
Correct |
95 ms |
11412 KB |
Output is correct |
21 |
Correct |
132 ms |
11460 KB |
Output is correct |
22 |
Correct |
98 ms |
11396 KB |
Output is correct |
23 |
Correct |
93 ms |
11412 KB |
Output is correct |
24 |
Correct |
128 ms |
11376 KB |
Output is correct |
25 |
Correct |
74 ms |
11436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
150 ms |
12028 KB |
Output is correct |
2 |
Correct |
157 ms |
12084 KB |
Output is correct |
3 |
Correct |
220 ms |
12060 KB |
Output is correct |
4 |
Correct |
156 ms |
12552 KB |
Output is correct |
5 |
Correct |
167 ms |
12444 KB |
Output is correct |
6 |
Correct |
143 ms |
12348 KB |
Output is correct |
7 |
Correct |
140 ms |
12156 KB |
Output is correct |
8 |
Correct |
183 ms |
12344 KB |
Output is correct |
9 |
Correct |
95 ms |
11436 KB |
Output is correct |
10 |
Correct |
157 ms |
11896 KB |
Output is correct |
11 |
Correct |
157 ms |
11640 KB |
Output is correct |
12 |
Correct |
164 ms |
11848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2388 KB |
Output is correct |
2 |
Correct |
121 ms |
12084 KB |
Output is correct |
3 |
Correct |
145 ms |
11992 KB |
Output is correct |
4 |
Correct |
213 ms |
11972 KB |
Output is correct |
5 |
Correct |
144 ms |
12124 KB |
Output is correct |
6 |
Correct |
134 ms |
12092 KB |
Output is correct |
7 |
Correct |
163 ms |
12136 KB |
Output is correct |
8 |
Correct |
178 ms |
12596 KB |
Output is correct |
9 |
Correct |
176 ms |
12604 KB |
Output is correct |
10 |
Correct |
148 ms |
12424 KB |
Output is correct |
11 |
Correct |
162 ms |
12436 KB |
Output is correct |
12 |
Correct |
90 ms |
12596 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
2 ms |
2388 KB |
Output is correct |
15 |
Correct |
2 ms |
2516 KB |
Output is correct |
16 |
Correct |
3 ms |
2516 KB |
Output is correct |
17 |
Correct |
3 ms |
2516 KB |
Output is correct |
18 |
Correct |
3 ms |
2516 KB |
Output is correct |
19 |
Correct |
3 ms |
2516 KB |
Output is correct |
20 |
Correct |
2 ms |
2516 KB |
Output is correct |
21 |
Correct |
3 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
2388 KB |
Output is correct |
23 |
Correct |
2 ms |
2388 KB |
Output is correct |
24 |
Correct |
3 ms |
2516 KB |
Output is correct |
25 |
Correct |
3 ms |
2516 KB |
Output is correct |
26 |
Correct |
4 ms |
2536 KB |
Output is correct |
27 |
Correct |
4 ms |
2516 KB |
Output is correct |
28 |
Correct |
3 ms |
2516 KB |
Output is correct |
29 |
Correct |
4 ms |
2516 KB |
Output is correct |
30 |
Correct |
4 ms |
2552 KB |
Output is correct |
31 |
Correct |
45 ms |
3412 KB |
Output is correct |
32 |
Correct |
44 ms |
3480 KB |
Output is correct |
33 |
Correct |
44 ms |
3712 KB |
Output is correct |
34 |
Correct |
44 ms |
3796 KB |
Output is correct |
35 |
Correct |
43 ms |
3628 KB |
Output is correct |
36 |
Correct |
34 ms |
3548 KB |
Output is correct |
37 |
Correct |
40 ms |
3188 KB |
Output is correct |
38 |
Correct |
3 ms |
2388 KB |
Output is correct |
39 |
Correct |
2 ms |
2388 KB |
Output is correct |
40 |
Correct |
3 ms |
2516 KB |
Output is correct |
41 |
Correct |
4 ms |
2516 KB |
Output is correct |
42 |
Correct |
3 ms |
2516 KB |
Output is correct |
43 |
Correct |
3 ms |
2516 KB |
Output is correct |
44 |
Correct |
2 ms |
2516 KB |
Output is correct |
45 |
Correct |
2 ms |
2516 KB |
Output is correct |
46 |
Correct |
3 ms |
2516 KB |
Output is correct |
47 |
Correct |
94 ms |
11460 KB |
Output is correct |
48 |
Correct |
95 ms |
11412 KB |
Output is correct |
49 |
Correct |
132 ms |
11460 KB |
Output is correct |
50 |
Correct |
98 ms |
11396 KB |
Output is correct |
51 |
Correct |
93 ms |
11412 KB |
Output is correct |
52 |
Correct |
128 ms |
11376 KB |
Output is correct |
53 |
Correct |
74 ms |
11436 KB |
Output is correct |
54 |
Correct |
150 ms |
12028 KB |
Output is correct |
55 |
Correct |
157 ms |
12084 KB |
Output is correct |
56 |
Correct |
220 ms |
12060 KB |
Output is correct |
57 |
Correct |
156 ms |
12552 KB |
Output is correct |
58 |
Correct |
167 ms |
12444 KB |
Output is correct |
59 |
Correct |
143 ms |
12348 KB |
Output is correct |
60 |
Correct |
140 ms |
12156 KB |
Output is correct |
61 |
Correct |
183 ms |
12344 KB |
Output is correct |
62 |
Correct |
95 ms |
11436 KB |
Output is correct |
63 |
Correct |
157 ms |
11896 KB |
Output is correct |
64 |
Correct |
157 ms |
11640 KB |
Output is correct |
65 |
Correct |
164 ms |
11848 KB |
Output is correct |
66 |
Correct |
2 ms |
2388 KB |
Output is correct |
67 |
Correct |
132 ms |
12100 KB |
Output is correct |
68 |
Correct |
169 ms |
12084 KB |
Output is correct |
69 |
Correct |
161 ms |
11996 KB |
Output is correct |
70 |
Correct |
159 ms |
12112 KB |
Output is correct |
71 |
Correct |
160 ms |
12156 KB |
Output is correct |
72 |
Correct |
149 ms |
12148 KB |
Output is correct |
73 |
Correct |
149 ms |
12580 KB |
Output is correct |
74 |
Correct |
183 ms |
12608 KB |
Output is correct |
75 |
Correct |
144 ms |
12488 KB |
Output is correct |
76 |
Correct |
146 ms |
12488 KB |
Output is correct |
77 |
Correct |
80 ms |
12532 KB |
Output is correct |
78 |
Correct |
2 ms |
2388 KB |
Output is correct |
79 |
Correct |
4 ms |
2516 KB |
Output is correct |
80 |
Correct |
3 ms |
2516 KB |
Output is correct |
81 |
Correct |
3 ms |
2516 KB |
Output is correct |
82 |
Correct |
3 ms |
2516 KB |
Output is correct |
83 |
Correct |
3 ms |
2516 KB |
Output is correct |
84 |
Correct |
2 ms |
2516 KB |
Output is correct |
85 |
Correct |
2 ms |
2516 KB |
Output is correct |
86 |
Correct |
35 ms |
3384 KB |
Output is correct |
87 |
Correct |
36 ms |
3468 KB |
Output is correct |
88 |
Correct |
49 ms |
3736 KB |
Output is correct |
89 |
Correct |
44 ms |
3800 KB |
Output is correct |
90 |
Correct |
27 ms |
3668 KB |
Output is correct |
91 |
Correct |
35 ms |
3532 KB |
Output is correct |
92 |
Correct |
32 ms |
3252 KB |
Output is correct |
93 |
Correct |
108 ms |
11504 KB |
Output is correct |
94 |
Correct |
101 ms |
11508 KB |
Output is correct |
95 |
Correct |
96 ms |
11504 KB |
Output is correct |
96 |
Correct |
102 ms |
11384 KB |
Output is correct |
97 |
Correct |
84 ms |
11456 KB |
Output is correct |
98 |
Correct |
107 ms |
11476 KB |
Output is correct |
99 |
Correct |
46 ms |
11448 KB |
Output is correct |
100 |
Correct |
167 ms |
12212 KB |
Output is correct |
101 |
Correct |
190 ms |
12176 KB |
Output is correct |
102 |
Correct |
144 ms |
12280 KB |
Output is correct |
103 |
Correct |
185 ms |
11840 KB |
Output is correct |
104 |
Correct |
163 ms |
11640 KB |
Output is correct |
105 |
Correct |
152 ms |
11888 KB |
Output is correct |
106 |
Correct |
194 ms |
12268 KB |
Output is correct |
107 |
Correct |
147 ms |
12340 KB |
Output is correct |
108 |
Correct |
150 ms |
12224 KB |
Output is correct |
109 |
Correct |
155 ms |
12296 KB |
Output is correct |
110 |
Correct |
151 ms |
12148 KB |
Output is correct |
111 |
Correct |
170 ms |
12076 KB |
Output is correct |