#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2")
#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);
if (n <= 1000) {
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';
}
return;
}
vector<int> id(n + 1);
for (int i = 0; i < n; i++)
id[a[i].i] = i;
vector<pair<int, int>> ind(n, {-1, -1});
int cnt = 0;
auto get = [&](int u) {
for (int i = max(u - 2, 0); i < min(n, u + 2); i++) {
if (i == u) continue;
if (a[i].l <= a[u].r and a[u].r <= a[i].r) return i;
}
return -1;
};
for (int i = 0; i < n; i++) {
if (ind[i].first != -1) continue;
ind[i] = {cnt, 0};
int j = i, k = get(i);
int now = 1;
set<int> way = {-1, i};
while (!way.count(k)) {
way.insert(j);
j = k;
k = get(j);
ind[j] = {cnt, now};
now++;
}
}
while (q--) {
int si, ei;
cin >> si >> ei;
si = id[si];
ei = id[ei];
if (get(si) == ei) {
cout << "1\n";
} else if (si == ei) {
cout << "0\n";
} else if (ind[si].first == ind[ei].first and ind[si].second < ind[ei].second) {
cout << ind[ei].second - ind[si].second + 1 << '\n';
} else {
cout << "impossible\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';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
118 ms |
8016 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
9 ms |
4308 KB |
Output is correct |
4 |
Correct |
7 ms |
4316 KB |
Output is correct |
5 |
Correct |
10 ms |
4352 KB |
Output is correct |
6 |
Correct |
57 ms |
5136 KB |
Output is correct |
7 |
Correct |
168 ms |
5936 KB |
Output is correct |
8 |
Correct |
232 ms |
6976 KB |
Output is correct |
9 |
Correct |
1122 ms |
8340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
9 ms |
4308 KB |
Output is correct |
4 |
Correct |
7 ms |
4316 KB |
Output is correct |
5 |
Correct |
10 ms |
4352 KB |
Output is correct |
6 |
Correct |
57 ms |
5136 KB |
Output is correct |
7 |
Correct |
168 ms |
5936 KB |
Output is correct |
8 |
Correct |
232 ms |
6976 KB |
Output is correct |
9 |
Correct |
1122 ms |
8340 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
10 ms |
4308 KB |
Output is correct |
13 |
Correct |
7 ms |
4308 KB |
Output is correct |
14 |
Correct |
10 ms |
4308 KB |
Output is correct |
15 |
Correct |
54 ms |
5136 KB |
Output is correct |
16 |
Correct |
160 ms |
5944 KB |
Output is correct |
17 |
Correct |
251 ms |
6996 KB |
Output is correct |
18 |
Correct |
1151 ms |
8344 KB |
Output is correct |
19 |
Incorrect |
32 ms |
1552 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
9 ms |
4308 KB |
Output is correct |
4 |
Correct |
7 ms |
4316 KB |
Output is correct |
5 |
Correct |
10 ms |
4352 KB |
Output is correct |
6 |
Correct |
57 ms |
5136 KB |
Output is correct |
7 |
Correct |
168 ms |
5936 KB |
Output is correct |
8 |
Correct |
232 ms |
6976 KB |
Output is correct |
9 |
Correct |
1122 ms |
8340 KB |
Output is correct |
10 |
Correct |
1 ms |
320 KB |
Output is correct |
11 |
Correct |
0 ms |
320 KB |
Output is correct |
12 |
Correct |
10 ms |
4372 KB |
Output is correct |
13 |
Correct |
8 ms |
4308 KB |
Output is correct |
14 |
Correct |
10 ms |
4360 KB |
Output is correct |
15 |
Correct |
58 ms |
5076 KB |
Output is correct |
16 |
Correct |
161 ms |
5944 KB |
Output is correct |
17 |
Correct |
205 ms |
6992 KB |
Output is correct |
18 |
Correct |
1319 ms |
8348 KB |
Output is correct |
19 |
Incorrect |
65 ms |
7908 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
95 ms |
8016 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
118 ms |
8016 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |