#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
const int maxn=1e5+9;
pii a[maxn];
int st[maxn][19];
int n,q;
pii st1[maxn*8];
pii combine(const pii &p1, const pii &q1){
if (p1.fi>q1.fi)return q1;
return p1;
}
void build(int id,int l,int r){
st1[id]={maxn*2,0};
if (l==r)return;
int mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
}
void update(int id,int l,int r,int u,pii val){
if (l>u||r<u)return;
if (l==r){
st1[id]=combine(st1[id],val);
return;
}
int mid=(l+r)/2;
update(id*2,l,mid,u,val);
update(id*2+1,mid+1,r,u,val);
st1[id]=combine(st1[id*2],st1[id*2+1]);
}
pii get(int id,int l,int r,int u,int v){
if (l>v||r<u||u>v)return {maxn*2,0};
if (u<=l&&r<=v)return st1[id];
int mid=(l+r)/2;
return combine(get(id*2,l,mid,u,v),get(id*2+1,mid+1,r,u,v));
}
void build(){
int m=n*2;
build(1,1,m);
for1(i,1,n){
update(1,1,m,a[i].se,{a[i].fi,i});
}
for1(i,1,n){
pii tmp=get(1,1,m,a[i].fi,a[i].se);
//if (i==64571)cout<<tmp.fi<<" "<<a[i].fi<<'\n';
if (tmp.fi>=a[i].fi)continue;
st[i][0]=tmp.se;
}
for1(j,1,18){
for1(i,1,n){
st[i][j]=st[st[i][j-1]][j-1];
}
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("events.INP","r",stdin);
//freopen("events.OUT","w",stdout);
cin>>n>>q;
vector<int>nen;
nen.pb(0);
for1(i,1,n){
cin>>a[i].fi>>a[i].se;
nen.pb(a[i].fi);
nen.pb(a[i].se);
}
sort(all(nen));
map<int,int>n1;
for1(i,1,sz(nen)-1){
if (nen[i]==nen[i-1])continue;
n1[nen[i]]=n1[nen[i-1]]+1;
}
for1(i,1,n){
a[i].fi=n1[a[i].fi];
a[i].se=n1[a[i].se];
//cout<<"type "<<" "<<a[i].fi<<" "<<a[i].se<<'\n';
}
build();
for1(i,1,q){
int s,e;
cin>>s>>e;
//if (i!=13)continue;
if (a[s].se>a[e].se){
cout<<"impossible"<<'\n';
}
else if (s==e){
cout<<0<<'\n';
}
else if (a[e].fi<=a[s].se&&a[s].se<=a[e].se){
cout<<1<<'\n';
}
else {
int ans=0;
for2(j,18,0){
if (!st[e][j])continue;
int id=st[e][j];
if (a[id].fi>a[s].se){
//cerr<<j<<'\n';
e=id;
ans+=(1<<j);
}
}
//cerr<<st[e][0]<<'\n';
//cout<<e<<'\n';
//cout<<a[s].fi<<" "<<a[s].se<<'\n';
//cout<<get(1,1,2*n,a[e].fi,a[e].se).fi<<" "<<a[e].fi<<'\n';
if (st[e][0]==0)ans=-1;
else {
int id=st[e][0];
if (a[id].fi<=a[s].se){
ans+=2;
}
else ans=-1;
}
if (ans==-1)cout<<"impossible"<<'\n';
else cout<<ans<<'\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
148 ms |
19136 KB |
Output is correct |
3 |
Correct |
151 ms |
19212 KB |
Output is correct |
4 |
Correct |
188 ms |
19108 KB |
Output is correct |
5 |
Correct |
158 ms |
19720 KB |
Output is correct |
6 |
Correct |
157 ms |
19916 KB |
Output is correct |
7 |
Correct |
167 ms |
20080 KB |
Output is correct |
8 |
Correct |
184 ms |
24056 KB |
Output is correct |
9 |
Correct |
186 ms |
24092 KB |
Output is correct |
10 |
Correct |
168 ms |
19300 KB |
Output is correct |
11 |
Correct |
180 ms |
21184 KB |
Output is correct |
12 |
Correct |
103 ms |
19440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
484 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
484 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
644 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
34 ms |
2308 KB |
Output is correct |
20 |
Correct |
33 ms |
2272 KB |
Output is correct |
21 |
Correct |
41 ms |
2528 KB |
Output is correct |
22 |
Correct |
28 ms |
2612 KB |
Output is correct |
23 |
Correct |
24 ms |
2680 KB |
Output is correct |
24 |
Correct |
32 ms |
2640 KB |
Output is correct |
25 |
Correct |
27 ms |
2268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
472 KB |
Output is correct |
6 |
Correct |
1 ms |
484 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 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 |
1 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 |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
124 ms |
18600 KB |
Output is correct |
20 |
Correct |
125 ms |
18552 KB |
Output is correct |
21 |
Correct |
146 ms |
18636 KB |
Output is correct |
22 |
Correct |
150 ms |
20356 KB |
Output is correct |
23 |
Correct |
223 ms |
23716 KB |
Output is correct |
24 |
Correct |
175 ms |
23608 KB |
Output is correct |
25 |
Correct |
50 ms |
14408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
19060 KB |
Output is correct |
2 |
Correct |
153 ms |
19220 KB |
Output is correct |
3 |
Correct |
163 ms |
19124 KB |
Output is correct |
4 |
Correct |
185 ms |
24352 KB |
Output is correct |
5 |
Correct |
155 ms |
19592 KB |
Output is correct |
6 |
Correct |
199 ms |
24000 KB |
Output is correct |
7 |
Correct |
217 ms |
23812 KB |
Output is correct |
8 |
Correct |
196 ms |
23948 KB |
Output is correct |
9 |
Correct |
183 ms |
23076 KB |
Output is correct |
10 |
Correct |
202 ms |
23480 KB |
Output is correct |
11 |
Correct |
210 ms |
23200 KB |
Output is correct |
12 |
Correct |
195 ms |
23412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
148 ms |
19136 KB |
Output is correct |
3 |
Correct |
151 ms |
19212 KB |
Output is correct |
4 |
Correct |
188 ms |
19108 KB |
Output is correct |
5 |
Correct |
158 ms |
19720 KB |
Output is correct |
6 |
Correct |
157 ms |
19916 KB |
Output is correct |
7 |
Correct |
167 ms |
20080 KB |
Output is correct |
8 |
Correct |
184 ms |
24056 KB |
Output is correct |
9 |
Correct |
186 ms |
24092 KB |
Output is correct |
10 |
Correct |
168 ms |
19300 KB |
Output is correct |
11 |
Correct |
180 ms |
21184 KB |
Output is correct |
12 |
Correct |
103 ms |
19440 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
472 KB |
Output is correct |
18 |
Correct |
1 ms |
484 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
2 ms |
644 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
28 |
Correct |
2 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
1 ms |
468 KB |
Output is correct |
31 |
Correct |
34 ms |
2308 KB |
Output is correct |
32 |
Correct |
33 ms |
2272 KB |
Output is correct |
33 |
Correct |
41 ms |
2528 KB |
Output is correct |
34 |
Correct |
28 ms |
2612 KB |
Output is correct |
35 |
Correct |
24 ms |
2680 KB |
Output is correct |
36 |
Correct |
32 ms |
2640 KB |
Output is correct |
37 |
Correct |
27 ms |
2268 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
0 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
468 KB |
Output is correct |
44 |
Correct |
2 ms |
468 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
124 ms |
18600 KB |
Output is correct |
48 |
Correct |
125 ms |
18552 KB |
Output is correct |
49 |
Correct |
146 ms |
18636 KB |
Output is correct |
50 |
Correct |
150 ms |
20356 KB |
Output is correct |
51 |
Correct |
223 ms |
23716 KB |
Output is correct |
52 |
Correct |
175 ms |
23608 KB |
Output is correct |
53 |
Correct |
50 ms |
14408 KB |
Output is correct |
54 |
Correct |
145 ms |
19060 KB |
Output is correct |
55 |
Correct |
153 ms |
19220 KB |
Output is correct |
56 |
Correct |
163 ms |
19124 KB |
Output is correct |
57 |
Correct |
185 ms |
24352 KB |
Output is correct |
58 |
Correct |
155 ms |
19592 KB |
Output is correct |
59 |
Correct |
199 ms |
24000 KB |
Output is correct |
60 |
Correct |
217 ms |
23812 KB |
Output is correct |
61 |
Correct |
196 ms |
23948 KB |
Output is correct |
62 |
Correct |
183 ms |
23076 KB |
Output is correct |
63 |
Correct |
202 ms |
23480 KB |
Output is correct |
64 |
Correct |
210 ms |
23200 KB |
Output is correct |
65 |
Correct |
195 ms |
23412 KB |
Output is correct |
66 |
Correct |
1 ms |
332 KB |
Output is correct |
67 |
Correct |
141 ms |
19560 KB |
Output is correct |
68 |
Correct |
168 ms |
19524 KB |
Output is correct |
69 |
Correct |
162 ms |
19592 KB |
Output is correct |
70 |
Correct |
165 ms |
20092 KB |
Output is correct |
71 |
Correct |
157 ms |
20548 KB |
Output is correct |
72 |
Correct |
180 ms |
20732 KB |
Output is correct |
73 |
Correct |
173 ms |
24744 KB |
Output is correct |
74 |
Correct |
186 ms |
24756 KB |
Output is correct |
75 |
Correct |
172 ms |
19912 KB |
Output is correct |
76 |
Correct |
178 ms |
21956 KB |
Output is correct |
77 |
Correct |
127 ms |
20036 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
1 ms |
468 KB |
Output is correct |
80 |
Correct |
2 ms |
504 KB |
Output is correct |
81 |
Correct |
2 ms |
468 KB |
Output is correct |
82 |
Correct |
1 ms |
476 KB |
Output is correct |
83 |
Correct |
2 ms |
468 KB |
Output is correct |
84 |
Correct |
1 ms |
468 KB |
Output is correct |
85 |
Correct |
1 ms |
468 KB |
Output is correct |
86 |
Correct |
29 ms |
2760 KB |
Output is correct |
87 |
Correct |
32 ms |
2648 KB |
Output is correct |
88 |
Correct |
30 ms |
2932 KB |
Output is correct |
89 |
Correct |
26 ms |
2904 KB |
Output is correct |
90 |
Correct |
24 ms |
2992 KB |
Output is correct |
91 |
Correct |
33 ms |
3044 KB |
Output is correct |
92 |
Correct |
30 ms |
2648 KB |
Output is correct |
93 |
Correct |
125 ms |
18900 KB |
Output is correct |
94 |
Correct |
122 ms |
18892 KB |
Output is correct |
95 |
Correct |
150 ms |
18828 KB |
Output is correct |
96 |
Correct |
150 ms |
20232 KB |
Output is correct |
97 |
Correct |
188 ms |
23188 KB |
Output is correct |
98 |
Correct |
217 ms |
23144 KB |
Output is correct |
99 |
Correct |
46 ms |
13944 KB |
Output is correct |
100 |
Correct |
218 ms |
23800 KB |
Output is correct |
101 |
Correct |
214 ms |
23720 KB |
Output is correct |
102 |
Correct |
197 ms |
23916 KB |
Output is correct |
103 |
Correct |
199 ms |
23448 KB |
Output is correct |
104 |
Correct |
222 ms |
23196 KB |
Output is correct |
105 |
Correct |
218 ms |
23468 KB |
Output is correct |
106 |
Correct |
156 ms |
17908 KB |
Output is correct |
107 |
Correct |
190 ms |
19104 KB |
Output is correct |
108 |
Correct |
159 ms |
19180 KB |
Output is correct |
109 |
Correct |
173 ms |
19180 KB |
Output is correct |
110 |
Correct |
220 ms |
23664 KB |
Output is correct |
111 |
Correct |
222 ms |
23712 KB |
Output is correct |