#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(s[j]<s[i] && 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
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
3824 KB |
Output is correct |
2 |
Correct |
42 ms |
3776 KB |
Output is correct |
3 |
Correct |
41 ms |
3784 KB |
Output is correct |
4 |
Correct |
37 ms |
4292 KB |
Output is correct |
5 |
Correct |
40 ms |
4036 KB |
Output is correct |
6 |
Incorrect |
41 ms |
4040 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |