#include "bits/stdc++.h"
using namespace std;
using ll = long long;
int INF = 2e9;
struct node {
int l, r, i;
};
bool comp(node a, node b) {
if (a.r < b.r) return true;
if (a.r > b.r) return false;
return a.l < b.l;
}
void solve() {
int n, q;
cin >> n >> q;
vector<node> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].l >> a[i].r;
a[i].i = i + 1;
}
sort(a.begin(), a.end(), comp);
vector<int> ind(n + 1);
for (int i = 0; i < n; i++) {
ind[a[i].i] = i;
}
vector<vector<int>> can(n + 1, vector<int>(n + 1, -1));
vector<vector<int>> g(n + 1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[j].l <= a[i].r and a[i].r <= a[j].r) {
g[i].emplace_back(j);
}
}
}
for (int i = 0; i < n; i++) {
can[i][i] = 0;
queue<int> pq;
pq.push(i);
while (!pq.empty()) {
int u = pq.front();
pq.pop();
for (int v : g[u]) {
if (can[i][v] == -1) {
can[i][v] = can[i][u] + 1;
pq.push(v);
}
}
}
}
while (q--) {
int si, ei;
cin >> si >> ei;
si = ind[si];
ei = ind[ei];
if (can[si][ei] == -1) cout << "impossible";
else cout << can[si][ei];
cout << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false); cout.tie(nullptr); cin.tie(nullptr);
int queries = 1;
#ifdef test_cases
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
// cin >> queries;
#else
// cin >> queries;
#endif
for (int test_case = 1; test_case <= queries; test_case++) {
solve();
// cout << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
244 ms |
524288 KB |
Execution killed with signal 9 |
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 |
12 ms |
4308 KB |
Output is correct |
4 |
Correct |
8 ms |
4308 KB |
Output is correct |
5 |
Correct |
11 ms |
4356 KB |
Output is correct |
6 |
Correct |
55 ms |
5140 KB |
Output is correct |
7 |
Correct |
172 ms |
5844 KB |
Output is correct |
8 |
Correct |
208 ms |
6868 KB |
Output is correct |
9 |
Correct |
1138 ms |
8344 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 |
12 ms |
4308 KB |
Output is correct |
4 |
Correct |
8 ms |
4308 KB |
Output is correct |
5 |
Correct |
11 ms |
4356 KB |
Output is correct |
6 |
Correct |
55 ms |
5140 KB |
Output is correct |
7 |
Correct |
172 ms |
5844 KB |
Output is correct |
8 |
Correct |
208 ms |
6868 KB |
Output is correct |
9 |
Correct |
1138 ms |
8344 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
12 ms |
4376 KB |
Output is correct |
13 |
Correct |
9 ms |
4308 KB |
Output is correct |
14 |
Correct |
12 ms |
4356 KB |
Output is correct |
15 |
Correct |
60 ms |
5140 KB |
Output is correct |
16 |
Correct |
182 ms |
5948 KB |
Output is correct |
17 |
Correct |
198 ms |
6976 KB |
Output is correct |
18 |
Correct |
1131 ms |
8344 KB |
Output is correct |
19 |
Execution timed out |
1579 ms |
128436 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 |
12 ms |
4308 KB |
Output is correct |
4 |
Correct |
8 ms |
4308 KB |
Output is correct |
5 |
Correct |
11 ms |
4356 KB |
Output is correct |
6 |
Correct |
55 ms |
5140 KB |
Output is correct |
7 |
Correct |
172 ms |
5844 KB |
Output is correct |
8 |
Correct |
208 ms |
6868 KB |
Output is correct |
9 |
Correct |
1138 ms |
8344 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
13 ms |
4292 KB |
Output is correct |
13 |
Correct |
9 ms |
4380 KB |
Output is correct |
14 |
Correct |
12 ms |
4308 KB |
Output is correct |
15 |
Correct |
55 ms |
5136 KB |
Output is correct |
16 |
Correct |
168 ms |
5948 KB |
Output is correct |
17 |
Correct |
197 ms |
6980 KB |
Output is correct |
18 |
Correct |
1199 ms |
8356 KB |
Output is correct |
19 |
Runtime error |
233 ms |
524288 KB |
Execution killed with signal 9 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
245 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
244 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |