#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
int N, Q;
vector<pair<int, int>> events;
vector<int> graph[MAXN];
vector<int> switches;
int main() {
cin >> N >> Q;
events.resize(N + 1);
for (int i = 1; i <= N; i++) {
cin >> events[i].first >> events[i].second;
}
sort(events.begin() + 1, events.end());
for (int i = 1; i <= N; i++) {
for (int j = i + 1; j <= N; j++) {
if (events[j].first <= events[i].second) {
graph[i].push_back(j);
} else {
break;
}
}
}
while (Q--) {
int s, e;
cin >> s >> e;
switches.assign(N + 1, -1);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s});
switches[s] = 0;
while (!pq.empty()) {
int u = pq.top().second;
int w = pq.top().first;
pq.pop();
if (u == e) {
cout << switches[e] << endl;
break;
}
for (int i = 0; i < graph[u].size(); i++) {
int v = graph[u][i];
if (switches[v] == -1 || switches[v] > switches[u] + 1) {
switches[v] = switches[u] + 1;
pq.push({switches[v], v});
}
}
}
if (switches[e] == -1) {
cout << "impossible" << endl;
}
}
return 0;
}
Compilation message
events.cpp: In function 'int main()':
events.cpp:42:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | for (int i = 0; i < graph[u].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
events.cpp:36:17: warning: unused variable 'w' [-Wunused-variable]
36 | int w = pq.top().first;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1550 ms |
7176 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |