#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;
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[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(same.count({a,b})){
cout << 1 << '\n';
continue;
}
if(gr[a]!=gr[b] || d[b]<d[a]){
cout << "impossible\n";
continue;
}
cout << d[b]-d[a] << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
6756 KB |
Output is correct |
2 |
Correct |
42 ms |
6908 KB |
Output is correct |
3 |
Correct |
42 ms |
6848 KB |
Output is correct |
4 |
Correct |
39 ms |
7368 KB |
Output is correct |
5 |
Correct |
47 ms |
7240 KB |
Output is correct |
6 |
Incorrect |
46 ms |
7088 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |