#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<bool> vb;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++)
#define all(a) a.begin(),a.end()
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pb emplace_back
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
template<class T> void out(T a){cout<<a<<endl;}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<endl;}
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
const ll inf=1001001001001001001;
int main(){
ll n,q;cin>>n>>q;
vp v;
vi sx;
rep(i,n){
ll a,b;cin>>a>>b;
v.pb(a,b);
sx.pb(a);sx.pb(b);
}
dupli(sx);
rep(i,n)v[i].fi=lb(sx,v[i].fi);
rep(i,n)v[i].se=lb(sx,v[i].se);
ll N=sx.size();
vp seg(N*2,P(inf,-1));
rep(t,n){
for(ll i=v[t].se+N;i>0;i>>=1)chmin(seg[i],P(v[t].fi,t));
}
vvi table(20,vi(n,-1));
rep(i,n){
P mi(inf,-1);
for(ll l=v[i].fi+N,r=v[i].se+1+N;l<r;l>>=1,r>>=1){
if(l&1)chmin(mi,seg[l++]);
if(r&1)chmin(mi,seg[--r]);
}
if(v[mi.se].fi<v[i].fi)table[0][i]=mi.se;
}
rep(i,19)rep(j,n)if(table[i][j]!=-1)table[i+1][j]=table[i][table[i][j]];
rep(qq,q){
ll s,t;cin>>s>>t;s--;t--;
if(s==t){
out(0);continue;
}
ll ans=0;
for(ll j=19;j>=0;j--)if(table[j][t]!=-1&&v[s].se<v[table[j][t]].fi){
ans+=1<<j;
t=table[j][t];
}
if(v[t].se<v[s].se)ans=-1;
else if(v[t].fi>v[s].se){
if(table[0][t]==-1)ans=-1;
else ans++;
}
if(ans==-1)out("impossible");
else out(ans+1);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
328 ms |
26632 KB |
Output is correct |
3 |
Correct |
370 ms |
26660 KB |
Output is correct |
4 |
Correct |
464 ms |
26588 KB |
Output is correct |
5 |
Correct |
381 ms |
26964 KB |
Output is correct |
6 |
Correct |
392 ms |
27376 KB |
Output is correct |
7 |
Correct |
338 ms |
27396 KB |
Output is correct |
8 |
Correct |
372 ms |
30212 KB |
Output is correct |
9 |
Correct |
335 ms |
30224 KB |
Output is correct |
10 |
Correct |
403 ms |
27068 KB |
Output is correct |
11 |
Correct |
400 ms |
28440 KB |
Output is correct |
12 |
Correct |
314 ms |
26408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
292 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 |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
292 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 |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 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 |
3 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
440 KB |
Output is correct |
19 |
Correct |
198 ms |
2972 KB |
Output is correct |
20 |
Correct |
205 ms |
3148 KB |
Output is correct |
21 |
Correct |
207 ms |
3160 KB |
Output is correct |
22 |
Correct |
197 ms |
3328 KB |
Output is correct |
23 |
Correct |
203 ms |
3268 KB |
Output is correct |
24 |
Correct |
253 ms |
3272 KB |
Output is correct |
25 |
Correct |
196 ms |
2944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
292 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 |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
300 KB |
Output is correct |
12 |
Correct |
3 ms |
468 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 |
440 KB |
Output is correct |
16 |
Correct |
2 ms |
500 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
133 ms |
24932 KB |
Output is correct |
20 |
Correct |
120 ms |
24088 KB |
Output is correct |
21 |
Correct |
148 ms |
24896 KB |
Output is correct |
22 |
Correct |
147 ms |
26072 KB |
Output is correct |
23 |
Correct |
154 ms |
28072 KB |
Output is correct |
24 |
Correct |
177 ms |
28004 KB |
Output is correct |
25 |
Correct |
75 ms |
21048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
331 ms |
26704 KB |
Output is correct |
2 |
Correct |
364 ms |
26744 KB |
Output is correct |
3 |
Correct |
413 ms |
26452 KB |
Output is correct |
4 |
Correct |
374 ms |
30044 KB |
Output is correct |
5 |
Correct |
421 ms |
26820 KB |
Output is correct |
6 |
Correct |
448 ms |
29860 KB |
Output is correct |
7 |
Correct |
424 ms |
29836 KB |
Output is correct |
8 |
Correct |
370 ms |
30108 KB |
Output is correct |
9 |
Correct |
151 ms |
27992 KB |
Output is correct |
10 |
Correct |
360 ms |
29644 KB |
Output is correct |
11 |
Correct |
521 ms |
29308 KB |
Output is correct |
12 |
Correct |
358 ms |
29632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
328 ms |
26632 KB |
Output is correct |
3 |
Correct |
370 ms |
26660 KB |
Output is correct |
4 |
Correct |
464 ms |
26588 KB |
Output is correct |
5 |
Correct |
381 ms |
26964 KB |
Output is correct |
6 |
Correct |
392 ms |
27376 KB |
Output is correct |
7 |
Correct |
338 ms |
27396 KB |
Output is correct |
8 |
Correct |
372 ms |
30212 KB |
Output is correct |
9 |
Correct |
335 ms |
30224 KB |
Output is correct |
10 |
Correct |
403 ms |
27068 KB |
Output is correct |
11 |
Correct |
400 ms |
28440 KB |
Output is correct |
12 |
Correct |
314 ms |
26408 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
292 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 |
3 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
300 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
2 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 |
3 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
440 KB |
Output is correct |
31 |
Correct |
198 ms |
2972 KB |
Output is correct |
32 |
Correct |
205 ms |
3148 KB |
Output is correct |
33 |
Correct |
207 ms |
3160 KB |
Output is correct |
34 |
Correct |
197 ms |
3328 KB |
Output is correct |
35 |
Correct |
203 ms |
3268 KB |
Output is correct |
36 |
Correct |
253 ms |
3272 KB |
Output is correct |
37 |
Correct |
196 ms |
2944 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
300 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
440 KB |
Output is correct |
44 |
Correct |
2 ms |
500 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
133 ms |
24932 KB |
Output is correct |
48 |
Correct |
120 ms |
24088 KB |
Output is correct |
49 |
Correct |
148 ms |
24896 KB |
Output is correct |
50 |
Correct |
147 ms |
26072 KB |
Output is correct |
51 |
Correct |
154 ms |
28072 KB |
Output is correct |
52 |
Correct |
177 ms |
28004 KB |
Output is correct |
53 |
Correct |
75 ms |
21048 KB |
Output is correct |
54 |
Correct |
331 ms |
26704 KB |
Output is correct |
55 |
Correct |
364 ms |
26744 KB |
Output is correct |
56 |
Correct |
413 ms |
26452 KB |
Output is correct |
57 |
Correct |
374 ms |
30044 KB |
Output is correct |
58 |
Correct |
421 ms |
26820 KB |
Output is correct |
59 |
Correct |
448 ms |
29860 KB |
Output is correct |
60 |
Correct |
424 ms |
29836 KB |
Output is correct |
61 |
Correct |
370 ms |
30108 KB |
Output is correct |
62 |
Correct |
151 ms |
27992 KB |
Output is correct |
63 |
Correct |
360 ms |
29644 KB |
Output is correct |
64 |
Correct |
521 ms |
29308 KB |
Output is correct |
65 |
Correct |
358 ms |
29632 KB |
Output is correct |
66 |
Correct |
1 ms |
212 KB |
Output is correct |
67 |
Correct |
333 ms |
26624 KB |
Output is correct |
68 |
Correct |
357 ms |
26712 KB |
Output is correct |
69 |
Correct |
414 ms |
26700 KB |
Output is correct |
70 |
Correct |
343 ms |
27044 KB |
Output is correct |
71 |
Correct |
369 ms |
27268 KB |
Output is correct |
72 |
Correct |
359 ms |
27452 KB |
Output is correct |
73 |
Correct |
353 ms |
30372 KB |
Output is correct |
74 |
Correct |
350 ms |
30272 KB |
Output is correct |
75 |
Correct |
447 ms |
27132 KB |
Output is correct |
76 |
Correct |
406 ms |
28400 KB |
Output is correct |
77 |
Correct |
277 ms |
26348 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
2 ms |
436 KB |
Output is correct |
80 |
Correct |
2 ms |
468 KB |
Output is correct |
81 |
Correct |
2 ms |
468 KB |
Output is correct |
82 |
Correct |
2 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 |
468 KB |
Output is correct |
86 |
Correct |
191 ms |
2992 KB |
Output is correct |
87 |
Correct |
207 ms |
2972 KB |
Output is correct |
88 |
Correct |
227 ms |
3204 KB |
Output is correct |
89 |
Correct |
185 ms |
3284 KB |
Output is correct |
90 |
Correct |
204 ms |
3304 KB |
Output is correct |
91 |
Correct |
207 ms |
3212 KB |
Output is correct |
92 |
Correct |
202 ms |
3056 KB |
Output is correct |
93 |
Correct |
139 ms |
24984 KB |
Output is correct |
94 |
Correct |
113 ms |
24124 KB |
Output is correct |
95 |
Correct |
142 ms |
24860 KB |
Output is correct |
96 |
Correct |
163 ms |
26096 KB |
Output is correct |
97 |
Correct |
153 ms |
28068 KB |
Output is correct |
98 |
Correct |
155 ms |
27988 KB |
Output is correct |
99 |
Correct |
67 ms |
21060 KB |
Output is correct |
100 |
Correct |
420 ms |
29932 KB |
Output is correct |
101 |
Correct |
399 ms |
29932 KB |
Output is correct |
102 |
Correct |
435 ms |
29988 KB |
Output is correct |
103 |
Correct |
355 ms |
29568 KB |
Output is correct |
104 |
Correct |
420 ms |
29396 KB |
Output is correct |
105 |
Correct |
349 ms |
29620 KB |
Output is correct |
106 |
Correct |
367 ms |
25284 KB |
Output is correct |
107 |
Correct |
452 ms |
26096 KB |
Output is correct |
108 |
Correct |
378 ms |
26824 KB |
Output is correct |
109 |
Correct |
369 ms |
26780 KB |
Output is correct |
110 |
Correct |
411 ms |
29956 KB |
Output is correct |
111 |
Correct |
387 ms |
29832 KB |
Output is correct |