#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const ll mod = 1e9 + 7;
int c[100100], hely[100100];
struct P
{
int poz, id;
bool veg;
};
bool cmp(P& lhs, P& rhs){
if(lhs.poz != rhs.poz){
return lhs.poz < rhs.poz;
}
return lhs.veg < rhs.veg;
}
int kov[100100] = {0}, be[100100] = {0};
int main(){
int n, q;
cin >> n >> q;
vector<P> v;
for(int i = 1; i <= n; ++i){
int b, e;
cin >> b >> e;
v.push_back({b, i, false});
v.push_back({e, i, true});
}
sort(begin(v), end(v), cmp);
set<int> act;
for(auto i : v){
if(i.veg){
act.erase(i.id);
kov[i.id] = *act.begin();
be[*act.begin()]++;
}
else{
act.emplace(i.id);
}
}
int ctr = 0;
for(int i = 1; i <= n; ++i){
if(be[i] == 0){
++ctr;
int ptr = i, poz = 1;
while(ptr != 0){
c[ptr] = ctr;
hely[ptr] = poz;
++poz;
ptr = kov[ptr];
}
}
}
while(q--){
int a, b;
cin >> a >> b;
if(c[a] != c[b] || hely[a] > hely[b]){
cout << "impossible" << endl;
}
else{
cout << hely[b] - hely[a] << endl;
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
278 ms |
4896 KB |
Output is correct |
2 |
Correct |
271 ms |
4912 KB |
Output is correct |
3 |
Correct |
290 ms |
4908 KB |
Output is correct |
4 |
Correct |
290 ms |
5012 KB |
Output is correct |
5 |
Correct |
276 ms |
5276 KB |
Output is correct |
6 |
Incorrect |
1080 ms |
8152 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |