#include <algorithm>
#include <climits>
#include <iostream>
#include <set>
#include <queue>
#include <vector>
#define int long long
using namespace std;
int const INF = INT_MAX / 3;
struct event {
int s, e, id;
};
enum point_type {
start,
end,
};
struct point {
point_type ty;
int tm;
int event_id;
};
vector<int> bfs(vector<vector<int>> const& graph, int const start) {
queue<int> q;
q.push(start);
vector<int> dist(graph.size(), INF);
dist[start] = 0;
while (!q.empty()) {
int const cur = q.front();
q.pop();
for (int const& neigh : graph[cur]) {
if (dist[neigh] != INF) {
continue;
}
dist[neigh] = dist[cur] + 1;
q.push(neigh);
}
}
return dist;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<event> events(n);
vector<point> points;
for (int i = 0; i < n; i++) {
cin >> events[i].s >> events[i].e;
events[i].id = i;
points.push_back({ point_type::start, events[i].s, i });
points.push_back({ point_type::end, events[i].e, i });
}
sort(points.begin(), points.end(), [](auto const& a, auto const& b) {
return a.tm < b.tm || (a.tm == b.tm && a.ty == point_type::start && b.ty == point_type::end);
});
vector<vector<int>> graph(n);
{
set<int> cur;
vector<int> removed;
int last_tm = -1;
for (auto const& [ty, tm, e_id] : points) {
if (tm > last_tm) {
for (int const& r : removed) {
cur.erase(r);
}
removed.clear();
}
last_tm = tm;
if (ty == point_type::start) {
cur.insert(e_id);
}
else {
removed.push_back(e_id);
for (int const& c : cur) {
graph[e_id].push_back(c);
}
}
}
}
while (q--) {
int s, e;
cin >> s >> e;
s--; e--;
auto const dist = bfs(graph, s);
if (dist[e] == INF) {
cout << "impossible\n";
}
else {
cout << dist[e] << "\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1581 ms |
15036 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
7 ms |
1992 KB |
Output is correct |
7 |
Correct |
17 ms |
3664 KB |
Output is correct |
8 |
Correct |
20 ms |
5760 KB |
Output is correct |
9 |
Correct |
98 ms |
8404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
7 ms |
1992 KB |
Output is correct |
7 |
Correct |
17 ms |
3664 KB |
Output is correct |
8 |
Correct |
20 ms |
5760 KB |
Output is correct |
9 |
Correct |
98 ms |
8404 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
7 ms |
1992 KB |
Output is correct |
16 |
Correct |
18 ms |
3668 KB |
Output is correct |
17 |
Correct |
22 ms |
5692 KB |
Output is correct |
18 |
Correct |
100 ms |
8524 KB |
Output is correct |
19 |
Execution timed out |
1569 ms |
54444 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
3 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
7 ms |
1992 KB |
Output is correct |
7 |
Correct |
17 ms |
3664 KB |
Output is correct |
8 |
Correct |
20 ms |
5760 KB |
Output is correct |
9 |
Correct |
98 ms |
8404 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 |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
7 ms |
2072 KB |
Output is correct |
16 |
Correct |
19 ms |
3688 KB |
Output is correct |
17 |
Correct |
22 ms |
5648 KB |
Output is correct |
18 |
Correct |
106 ms |
8528 KB |
Output is correct |
19 |
Correct |
364 ms |
15488 KB |
Output is correct |
20 |
Correct |
334 ms |
19392 KB |
Output is correct |
21 |
Runtime error |
1135 ms |
524288 KB |
Execution killed with signal 9 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1519 ms |
15036 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1581 ms |
15036 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |