#include <bits/stdc++.h>
typedef long long ll;
const ll inf = 1e15;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll n;
std::cin >> n;
std::vector<ll> l(n + 1), r(n + 1);
for (ll i = 1; i <= n; ++i) {
std::cin >> l[i] >> r[i];
}
std::vector min_l(n + 1, std::vector<ll>(n + 1, inf));
std::vector max_r(n + 1, std::vector<ll>(n + 1, inf));
for (ll i = 1; i <= n; ++i) {
min_l[i][i] = l[i];
max_r[i][i] = r[i];
for (ll j = i + 1; j <= n; ++j) {
min_l[i][j] = std::min(min_l[i][j - 1], l[j]);
max_r[i][j] = std::max(max_r[i][j - 1], r[j]);
}
}
auto f = [n](ll i, ll j) { return (j - 1) + (i - 1) * n; };
std::vector<std::vector<std::pair<ll, ll>>> adj(n * n);
auto add_edge = [&](ll u, ll v, ll w) { adj[v].push_back({u, w}); };
for (ll i = 1; i <= n; ++i) {
add_edge(f(i, i), f(l[i], r[i]), 1);
for (ll j = i + 1; j <= n; ++j) {
add_edge(f(i, j), f(i + 1, j), 0);
add_edge(f(i, j), f(i, j - 1), 0);
add_edge(f(i, j), f(min_l[i][j], j), 1);
add_edge(f(i, j), f(i, max_r[i][j]), 1);
}
}
std::deque<ll> dq;
dq.push_back(f(1, n));
std::vector<ll> dist(n * n, inf);
std::vector<bool> vis(n * n);
dist[f(1, n)] = 0;
while (!dq.empty()) {
ll node = dq.front();
dq.pop_front();
if (vis[node]) {
continue;
}
vis[node] = true;
for (auto &[i, w] : adj[node]) {
if (dist[node] + w < dist[i]) {
dist[i] = dist[node] + w;
if (w == 0) {
dq.push_front(i);
} else {
dq.push_back(i);
}
}
}
}
ll q;
std::cin >> q;
while (q--) {
ll x;
std::cin >> x;
std::cout << (dist[f(x, x)] == inf ? -1 : dist[f(x, x)]) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Runtime error |
464 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
11 ms |
8280 KB |
Output is correct |
12 |
Correct |
10 ms |
8540 KB |
Output is correct |
13 |
Correct |
8 ms |
8872 KB |
Output is correct |
14 |
Correct |
8 ms |
8284 KB |
Output is correct |
15 |
Correct |
7 ms |
9048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
11 ms |
8280 KB |
Output is correct |
12 |
Correct |
10 ms |
8540 KB |
Output is correct |
13 |
Correct |
8 ms |
8872 KB |
Output is correct |
14 |
Correct |
8 ms |
8284 KB |
Output is correct |
15 |
Correct |
7 ms |
9048 KB |
Output is correct |
16 |
Correct |
623 ms |
513288 KB |
Output is correct |
17 |
Correct |
839 ms |
583652 KB |
Output is correct |
18 |
Correct |
595 ms |
579324 KB |
Output is correct |
19 |
Correct |
579 ms |
557392 KB |
Output is correct |
20 |
Correct |
632 ms |
539728 KB |
Output is correct |
21 |
Correct |
598 ms |
580408 KB |
Output is correct |
22 |
Correct |
551 ms |
581948 KB |
Output is correct |
23 |
Correct |
645 ms |
580832 KB |
Output is correct |
24 |
Correct |
592 ms |
579808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
11 ms |
8280 KB |
Output is correct |
12 |
Correct |
10 ms |
8540 KB |
Output is correct |
13 |
Correct |
8 ms |
8872 KB |
Output is correct |
14 |
Correct |
8 ms |
8284 KB |
Output is correct |
15 |
Correct |
7 ms |
9048 KB |
Output is correct |
16 |
Correct |
623 ms |
513288 KB |
Output is correct |
17 |
Correct |
839 ms |
583652 KB |
Output is correct |
18 |
Correct |
595 ms |
579324 KB |
Output is correct |
19 |
Correct |
579 ms |
557392 KB |
Output is correct |
20 |
Correct |
632 ms |
539728 KB |
Output is correct |
21 |
Correct |
598 ms |
580408 KB |
Output is correct |
22 |
Correct |
551 ms |
581948 KB |
Output is correct |
23 |
Correct |
645 ms |
580832 KB |
Output is correct |
24 |
Correct |
592 ms |
579808 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
580 ms |
551904 KB |
Output is correct |
28 |
Correct |
768 ms |
590900 KB |
Output is correct |
29 |
Correct |
641 ms |
539824 KB |
Output is correct |
30 |
Correct |
621 ms |
580436 KB |
Output is correct |
31 |
Correct |
603 ms |
551432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Runtime error |
464 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |