#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<std::vector<std::pair<ll, ll>>> adj(5 * n);
auto add_edge = [&](ll u, ll v, ll w) { adj[v].push_back({u, w}); };
std::function<void(ll, ll, ll)> init;
init = [&](ll v, ll tl, ll tr) {
if (tl == tr) {
add_edge(v + n, tl, 0);
return;
}
ll tm = (tl + tr) / 2;
init(2 * v, tl, tm);
init(2 * v + 1, tm + 1, tr);
add_edge(v + n, 2 * v + n, 0);
add_edge(v + n, 2 * v + 1 + n, 0);
};
init(1, 1, n);
std::function<void(ll, ll, ll, ll)> add;
add = [&](ll v, ll tl, ll tr, ll i) {
if (r[i] < tl or tr < l[i]) {
return;
}
if (l[i] <= tl and tr <= r[i]) {
add_edge(i, v + n, 1);
return;
}
ll tm = (tl + tr) / 2;
add(2 * v, tl, tm, i);
add(2 * v + 1, tm + 1, tr, i);
};
for (ll i = 1; i <= n; ++i) {
add(1, 1, n, i);
}
auto bfs_0_1 = [&](ll x) {
std::priority_queue<std::pair<ll, ll>> dq;
std::vector<bool> vis(5 * n);
std::vector<ll> dist(5 * n, inf);
dist[x] = 0;
dq.push({0, x});
while (!dq.empty()) {
auto [d, node] = dq.top();
dq.pop();
d *= -1;
if (vis[node]) {
continue;
}
vis[node] = true;
for (auto &[i, w] : adj[node]) {
if (dist[node] + w < dist[i]) {
dist[i] = dist[node] + w;
dq.push({-dist[i], i});
}
}
}
return dist;
};
auto a = bfs_0_1(1);
auto b = bfs_0_1(n);
for (ll i = 1; i <= n; ++i) {
if (a[i] != 0) {
a[i]--;
}
if (b[i] != 0) {
b[i]--;
}
}
std::vector<bool> vis(5 * n);
std::vector<ll> dist(5 * n, inf);
std::priority_queue<std::pair<ll, ll>> pq;
for (ll i = 1; i <= n; ++i) {
dist[i] = std::min(inf, a[i] + b[i] + 1);
pq.push({-dist[i], i});
}
while (!pq.empty()) {
auto [d, node] = pq.top();
pq.pop();
d *= -1;
if (vis[node]) {
continue;
}
vis[node] = true;
for (auto &[i, w] : adj[node]) {
if (dist[node] + w < dist[i]) {
dist[i] = dist[node] + w;
pq.push({-dist[i], i});
}
}
}
ll q;
std::cin >> q;
while (q--) {
ll x;
std::cin >> x;
ll ans = dist[x];
if (ans == inf) {
std::cout << "-1\n";
} else {
std::cout << ans << '\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 |
344 KB |
Output is correct |
4 |
Correct |
821 ms |
136144 KB |
Output is correct |
5 |
Correct |
416 ms |
94920 KB |
Output is correct |
6 |
Correct |
251 ms |
85316 KB |
Output is correct |
7 |
Correct |
221 ms |
79464 KB |
Output is correct |
8 |
Correct |
159 ms |
84388 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 |
344 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 |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
416 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
600 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 |
344 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 |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
416 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
5 ms |
1628 KB |
Output is correct |
17 |
Correct |
4 ms |
1628 KB |
Output is correct |
18 |
Correct |
5 ms |
2136 KB |
Output is correct |
19 |
Correct |
5 ms |
1884 KB |
Output is correct |
20 |
Correct |
2 ms |
1372 KB |
Output is correct |
21 |
Correct |
3 ms |
1628 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
4 ms |
1628 KB |
Output is correct |
24 |
Correct |
3 ms |
1720 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 |
344 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 |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
416 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
600 KB |
Output is correct |
16 |
Correct |
5 ms |
1628 KB |
Output is correct |
17 |
Correct |
4 ms |
1628 KB |
Output is correct |
18 |
Correct |
5 ms |
2136 KB |
Output is correct |
19 |
Correct |
5 ms |
1884 KB |
Output is correct |
20 |
Correct |
2 ms |
1372 KB |
Output is correct |
21 |
Correct |
3 ms |
1628 KB |
Output is correct |
22 |
Correct |
2 ms |
1372 KB |
Output is correct |
23 |
Correct |
4 ms |
1628 KB |
Output is correct |
24 |
Correct |
3 ms |
1720 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
5 ms |
1880 KB |
Output is correct |
28 |
Correct |
5 ms |
1628 KB |
Output is correct |
29 |
Correct |
5 ms |
1372 KB |
Output is correct |
30 |
Correct |
3 ms |
1676 KB |
Output is correct |
31 |
Correct |
4 ms |
1628 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 |
344 KB |
Output is correct |
4 |
Correct |
821 ms |
136144 KB |
Output is correct |
5 |
Correct |
416 ms |
94920 KB |
Output is correct |
6 |
Correct |
251 ms |
85316 KB |
Output is correct |
7 |
Correct |
221 ms |
79464 KB |
Output is correct |
8 |
Correct |
159 ms |
84388 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
416 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
600 KB |
Output is correct |
24 |
Correct |
5 ms |
1628 KB |
Output is correct |
25 |
Correct |
4 ms |
1628 KB |
Output is correct |
26 |
Correct |
5 ms |
2136 KB |
Output is correct |
27 |
Correct |
5 ms |
1884 KB |
Output is correct |
28 |
Correct |
2 ms |
1372 KB |
Output is correct |
29 |
Correct |
3 ms |
1628 KB |
Output is correct |
30 |
Correct |
2 ms |
1372 KB |
Output is correct |
31 |
Correct |
4 ms |
1628 KB |
Output is correct |
32 |
Correct |
3 ms |
1720 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
5 ms |
1880 KB |
Output is correct |
36 |
Correct |
5 ms |
1628 KB |
Output is correct |
37 |
Correct |
5 ms |
1372 KB |
Output is correct |
38 |
Correct |
3 ms |
1676 KB |
Output is correct |
39 |
Correct |
4 ms |
1628 KB |
Output is correct |
40 |
Correct |
893 ms |
143128 KB |
Output is correct |
41 |
Correct |
435 ms |
97252 KB |
Output is correct |
42 |
Correct |
590 ms |
147584 KB |
Output is correct |
43 |
Correct |
558 ms |
148492 KB |
Output is correct |
44 |
Correct |
262 ms |
86972 KB |
Output is correct |
45 |
Correct |
321 ms |
100824 KB |
Output is correct |
46 |
Correct |
143 ms |
36528 KB |
Output is correct |
47 |
Correct |
479 ms |
104820 KB |
Output is correct |
48 |
Correct |
351 ms |
117648 KB |
Output is correct |