#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int N;
vector<vector<int>> adj;
int bfs(int start, int end) {
vector<int> dist(N + 1);
queue<int> q;
q.push(start);
while (!q.empty()) {
int curr = q.front(); q.pop();
for (int &v: adj[curr]) {
if (dist[v]) continue;
dist[v] = dist[curr] + 1;
q.push(v);
}
}
return dist[end];
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int Q;
cin >> N >> Q;
adj.resize(N + 1);
vector<pair<int, int>> ranges(N);
set<int> unique;
for (auto &v: ranges){
cin >> v.first >> v.second;
unique.insert(v.first);
unique.insert(v.second);
}
map<int, int> compress;
int idx = 1;
for (auto &v: unique) {
compress[v] = idx++;
}
vector<vector<int>> start(unique.size() + 1), end(unique.size() + 1);
for (int i = 0; i < N; i++) {
ranges[i].first = compress[ranges[i].first];
ranges[i].second = compress[ranges[i].second];
start[ranges[i].first].push_back(i+1);
end[ranges[i].second].push_back(i+1);
}
set<int> running;
for (int i = 1; i <= (int)unique.size(); i++) {
for (auto &v: start[i]) {
running.insert(v);
}
for (auto &v: end[i]) {
for (auto &u: running) {
if (v == u) continue;
adj[v].push_back(u);
}
}
for (auto &v: end[i]) {
running.erase(v);
}
}
while (Q--) {
int s, e;
cin >> s >> e;
if (s == e) {
cout << "0\n";
continue;
}
int res = bfs(s, e);
if (res == 0) {
cout << "impossible\n";
} else {
cout << res << '\n';
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1534 ms |
27804 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
584 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
7 ms |
1300 KB |
Output is correct |
7 |
Correct |
18 ms |
2388 KB |
Output is correct |
8 |
Correct |
22 ms |
3380 KB |
Output is correct |
9 |
Correct |
86 ms |
4432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
584 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
7 ms |
1300 KB |
Output is correct |
7 |
Correct |
18 ms |
2388 KB |
Output is correct |
8 |
Correct |
22 ms |
3380 KB |
Output is correct |
9 |
Correct |
86 ms |
4432 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
588 KB |
Output is correct |
13 |
Correct |
2 ms |
528 KB |
Output is correct |
14 |
Correct |
2 ms |
576 KB |
Output is correct |
15 |
Correct |
7 ms |
1364 KB |
Output is correct |
16 |
Correct |
17 ms |
2356 KB |
Output is correct |
17 |
Correct |
21 ms |
3300 KB |
Output is correct |
18 |
Correct |
87 ms |
4436 KB |
Output is correct |
19 |
Execution timed out |
1574 ms |
31540 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
584 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
7 ms |
1300 KB |
Output is correct |
7 |
Correct |
18 ms |
2388 KB |
Output is correct |
8 |
Correct |
22 ms |
3380 KB |
Output is correct |
9 |
Correct |
86 ms |
4432 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
584 KB |
Output is correct |
14 |
Correct |
3 ms |
560 KB |
Output is correct |
15 |
Correct |
8 ms |
1364 KB |
Output is correct |
16 |
Correct |
19 ms |
2352 KB |
Output is correct |
17 |
Correct |
21 ms |
3340 KB |
Output is correct |
18 |
Correct |
91 ms |
4436 KB |
Output is correct |
19 |
Correct |
439 ms |
27580 KB |
Output is correct |
20 |
Correct |
479 ms |
29084 KB |
Output is correct |
21 |
Execution timed out |
1577 ms |
366848 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1556 ms |
27316 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1534 ms |
27804 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |