#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
const int ln=18;
int pow2[ln];
const int N=100000;
int st[N];
int en[N];
pair<int,int> seg[4*N];
void Build(int l,int r,int pos){
if(l==r){
seg[pos]=mp(en[l],l);
return;
}
int mid=(l+r)/2;
Build(l,mid,2*pos);
Build(mid+1,r,2*pos+1);
seg[pos]=min(seg[2*pos],seg[2*pos+1]);
}
pair<int,int> Query(int l,int r,int pos,int ql,int qr){
if(ql>r || qr<l) return mp(1000000001,-1);
if(ql<=l && qr>=r) return seg[pos];
int mid=(l+r)/2;
return min(Query(l,mid,2*pos,ql,qr),Query(mid+1,r,2*pos+1,ql,qr));
}
void solve(){
int n,q;
cin >> n >> q;
vector<pair<pair<int,int>,int>> v;
for(int i=0;i<n;i++){
int s,e;
cin >> s >> e;
v.push_back(mp(mp(e,s),i));
}
sort(v.begin(),v.end());
int rn[n];
for(int i=0;i<n;i++) rn[v[i].second]=i;
for(int i=0;i<n;i++){
st[i]=v[i].first.second;
en[i]=v[i].first.first;
}
int sparsetable[ln][n];
Build(0,n-1,1);
for(int i=0;i<n;i++){
int si=lower_bound(en,en+n,st[i])-en;
int ei=upper_bound(en,en+n,en[i])-en-1;
sparsetable[0][i]=Query(0,n-1,1,si,ei).second;
}
for(int k=0;k<ln-1;k++){
for(int i=0;i<n;i++){
sparsetable[k+1][i]=sparsetable[k][sparsetable[k][i]];
}
}
while(q--){
int u,v;
cin >> u >> v;
u--,v--;
u=rn[u];
v=rn[v];
if(en[v]<en[u]){
cout << "impossible" << "\n";
return;
}
if(u==v){
cout << 0 << "\n";
continue;
}
if(st[v]<=en[u]){
cout << 1 << "\n";
continue;
}
int ans=0;
for(int k=ln-1;k>=0;k--){
if(st[sparsetable[k][v]]>en[u]){
ans+=pow2[k];
v=sparsetable[k][v];
}
}
if(st[sparsetable[0][v]]<=en[u]){
cout << ans+2 << "\n";
continue;
}
cout << "impossible" << "\n";
}
}
int main(){
pow2[0]=1;
for(int i=0;i<ln-1;i++){
pow2[i+1]=2*pow2[i];
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
//cin >> t;
t=1;
while(t--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
78 ms |
14380 KB |
Output is correct |
3 |
Correct |
74 ms |
14532 KB |
Output is correct |
4 |
Correct |
78 ms |
14532 KB |
Output is correct |
5 |
Correct |
87 ms |
14504 KB |
Output is correct |
6 |
Correct |
76 ms |
14524 KB |
Output is correct |
7 |
Correct |
76 ms |
14608 KB |
Output is correct |
8 |
Correct |
72 ms |
15040 KB |
Output is correct |
9 |
Correct |
72 ms |
14928 KB |
Output is correct |
10 |
Incorrect |
54 ms |
13764 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
14528 KB |
Output is correct |
2 |
Correct |
73 ms |
14380 KB |
Output is correct |
3 |
Correct |
86 ms |
14496 KB |
Output is correct |
4 |
Correct |
71 ms |
15028 KB |
Output is correct |
5 |
Incorrect |
51 ms |
13968 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
78 ms |
14380 KB |
Output is correct |
3 |
Correct |
74 ms |
14532 KB |
Output is correct |
4 |
Correct |
78 ms |
14532 KB |
Output is correct |
5 |
Correct |
87 ms |
14504 KB |
Output is correct |
6 |
Correct |
76 ms |
14524 KB |
Output is correct |
7 |
Correct |
76 ms |
14608 KB |
Output is correct |
8 |
Correct |
72 ms |
15040 KB |
Output is correct |
9 |
Correct |
72 ms |
14928 KB |
Output is correct |
10 |
Incorrect |
54 ms |
13764 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |