#include <bits/stdc++.h>
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
#define sz(x) int((x).size())
#define pb push_back
#define s second
#define f first
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef tuple<int, int, int> tiii;
const int INF=1e9;
const int N=1e5+10;
int n, q, s[N], e[N], gr[N], d[N];
set<pii> same;
bool in[N];
int main()
{
FAST_IO;
cin >> n >> q;
vector<tiii> inf;
FOR(i, 0, n)
{
cin >> s[i] >> e[i];
inf.pb({s[i],e[i],i});
}
sort(inf.begin(), inf.end());
int j=-1;
for(auto it : inf){
int a, b, i;
tie(a, b, i)=it;
if(j==-1){
j=i;
gr[i]=i;
d[i]=0;
continue;
}
if(e[i]<e[j]){
in[i]=true;
gr[i]=gr[j];
d[i]=d[j]-1;
if(e[i]==e[j]){
same.insert({i,j});
same.insert({j,i});
}
continue;
}
if(s[i]<=e[j] && e[j]<=e[i]){
if(e[j]==e[i]){
same.insert({i,j});
same.insert({j,i});
}
gr[i]=gr[j];
d[i]=d[j]+1;
}
else{
gr[i]=i;
d[i]=0;
}
j=i;
}
while(q--){
int a, b; cin >> a >> b; --a; --b;
if(a==b){
cout << 0 << '\n';
continue;
}
if(same.count({a,b})){
cout << 1 << '\n';
continue;
}
if(in[a]){
if(gr[b]!=gr[a] || d[b]<=d[a]){
cout << "impossible\n";
}
else{
cout << d[b]-d[a] << '\n';
}
continue;
}
if(in[b] || gr[a]!=gr[b] || d[b]<d[a]){
cout << "impossible\n";
continue;
}
cout << d[b]-d[a] << '\n';
}
FOR(i, 0, n)
{
cout << gr[i] << " " << d[i] << '\n';
}
}
/*
5 5
1 1
1 2
2 3
4 5
5 6
1 2
2 3
3 3
3 4
4 5
3 3
1 2
2 3
3 3
3 2
1 3
3 1
4 5
1 4
2 3
4 6
5 6
1 4
4 3
2 1
2 3
2 4
6 5
1 2
2 3
3 4
4 5
5 6
6 7
1 6
2 3
2 2
3 5
1 5
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
8136 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |