#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';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Runtime error |
492 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
8 ms |
8028 KB |
Output is correct |
12 |
Correct |
10 ms |
8624 KB |
Output is correct |
13 |
Correct |
8 ms |
9052 KB |
Output is correct |
14 |
Correct |
7 ms |
8352 KB |
Output is correct |
15 |
Correct |
7 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
8 ms |
8028 KB |
Output is correct |
12 |
Correct |
10 ms |
8624 KB |
Output is correct |
13 |
Correct |
8 ms |
9052 KB |
Output is correct |
14 |
Correct |
7 ms |
8352 KB |
Output is correct |
15 |
Correct |
7 ms |
8796 KB |
Output is correct |
16 |
Correct |
605 ms |
513760 KB |
Output is correct |
17 |
Correct |
759 ms |
583508 KB |
Output is correct |
18 |
Correct |
594 ms |
579240 KB |
Output is correct |
19 |
Correct |
596 ms |
557280 KB |
Output is correct |
20 |
Correct |
646 ms |
539788 KB |
Output is correct |
21 |
Correct |
658 ms |
580360 KB |
Output is correct |
22 |
Correct |
595 ms |
582256 KB |
Output is correct |
23 |
Correct |
702 ms |
580312 KB |
Output is correct |
24 |
Correct |
610 ms |
580404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
8 ms |
8028 KB |
Output is correct |
12 |
Correct |
10 ms |
8624 KB |
Output is correct |
13 |
Correct |
8 ms |
9052 KB |
Output is correct |
14 |
Correct |
7 ms |
8352 KB |
Output is correct |
15 |
Correct |
7 ms |
8796 KB |
Output is correct |
16 |
Correct |
605 ms |
513760 KB |
Output is correct |
17 |
Correct |
759 ms |
583508 KB |
Output is correct |
18 |
Correct |
594 ms |
579240 KB |
Output is correct |
19 |
Correct |
596 ms |
557280 KB |
Output is correct |
20 |
Correct |
646 ms |
539788 KB |
Output is correct |
21 |
Correct |
658 ms |
580360 KB |
Output is correct |
22 |
Correct |
595 ms |
582256 KB |
Output is correct |
23 |
Correct |
702 ms |
580312 KB |
Output is correct |
24 |
Correct |
610 ms |
580404 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
626 ms |
551672 KB |
Output is correct |
28 |
Correct |
855 ms |
590928 KB |
Output is correct |
29 |
Correct |
722 ms |
539860 KB |
Output is correct |
30 |
Correct |
638 ms |
580324 KB |
Output is correct |
31 |
Correct |
766 ms |
551428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Runtime error |
492 ms |
1048576 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |