#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
#define MP make_pair
const int N = 100500;
vector<int> g[N];
int n;
int d[N];
void bfs(int x){
fill(d, d + n, 1e9);
d[x] = 0;
queue<int> q;
q.push(x);
while (!q.empty()){
x = q.front();
q.pop();
for (int y : g[x]){
if (d[y] == 1e9){
d[y] = d[x] + 1;
q.push(y);
}
}
}
}
void solve(){
int q;
cin >> n >> q;
vector<int> l(n), r(n);
for (int i = 0; i < n; i++){
cin >> l[i] >> r[i];
}
vector<int> perm(n);
iota(all(perm), 0);
sort(all(perm), [&](int i, int j){
return r[i] > r[j];
});
set<pair<int, int>> ls;
for (int i : perm){
while (!ls.empty() && ls.rbegin()->first > r[i]){
ls.erase(*ls.rbegin());
}
for (auto [_l, j] : ls){
if (l[j] <= r[i] && r[i] <= r[j]){
g[i].push_back(j);
}
if (l[i] <= r[j] && r[j] <= r[i]){
g[j].push_back(i);
}
}
ls.emplace(l[i], i);
}
for (int i = 0; i < q; i++){
int x, y;
cin >> x >> y;
--x, --y;
bfs(x);
if (d[y] == 1e9){
cout << "impossible" << endl;
} else {
cout << d[y] << endl;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
1547 ms |
8228 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
4 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
8 ms |
3412 KB |
Output is correct |
7 |
Correct |
19 ms |
4308 KB |
Output is correct |
8 |
Correct |
23 ms |
5332 KB |
Output is correct |
9 |
Correct |
86 ms |
6732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
4 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
8 ms |
3412 KB |
Output is correct |
7 |
Correct |
19 ms |
4308 KB |
Output is correct |
8 |
Correct |
23 ms |
5332 KB |
Output is correct |
9 |
Correct |
86 ms |
6732 KB |
Output is correct |
10 |
Correct |
1 ms |
2644 KB |
Output is correct |
11 |
Correct |
1 ms |
2644 KB |
Output is correct |
12 |
Correct |
3 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
8 ms |
3412 KB |
Output is correct |
16 |
Correct |
19 ms |
4236 KB |
Output is correct |
17 |
Correct |
21 ms |
5332 KB |
Output is correct |
18 |
Correct |
87 ms |
6740 KB |
Output is correct |
19 |
Execution timed out |
1589 ms |
32464 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
4 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
3 ms |
2644 KB |
Output is correct |
6 |
Correct |
8 ms |
3412 KB |
Output is correct |
7 |
Correct |
19 ms |
4308 KB |
Output is correct |
8 |
Correct |
23 ms |
5332 KB |
Output is correct |
9 |
Correct |
86 ms |
6732 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
4 ms |
2644 KB |
Output is correct |
13 |
Correct |
3 ms |
2644 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
8 ms |
3412 KB |
Output is correct |
16 |
Correct |
19 ms |
4308 KB |
Output is correct |
17 |
Correct |
21 ms |
5332 KB |
Output is correct |
18 |
Correct |
86 ms |
6800 KB |
Output is correct |
19 |
Correct |
302 ms |
9412 KB |
Output is correct |
20 |
Correct |
195 ms |
9976 KB |
Output is correct |
21 |
Execution timed out |
1593 ms |
350924 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1536 ms |
8480 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Execution timed out |
1547 ms |
8228 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |