#include <bits/stdc++.h>
typedef long long ll;
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);
std::function<void(ll, ll, ll)> init;
init = [&](ll v, ll tl, ll tr) {
if (tl == tr) {
adj[v + n].push_back({tl, 0});
return;
}
ll tm = (tl + tr) / 2;
init(2 * v, tl, tm);
init(2 * v + 1, tm + 1, tr);
adj[v + n].push_back({2 * v + n, 0});
adj[v + n].push_back({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]) {
adj[i].push_back({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);
}
ll q;
std::cin >> q;
while (q--) {
ll x;
std::cin >> x;
std::deque<ll> dq;
std::vector<bool> vis(5 * n);
std::vector<ll> dist(5 * n, 1e15);
dist[x] = 0;
dq.push_front(x);
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 ans = 0;
for (ll i = 1; i <= n; ++i) {
ans = std::max(ans, dist[i]);
}
if (ans == ll(1e15)) {
std::cout << "-1\n";
} else {
std::cout << ans << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
266 ms |
116220 KB |
Output is correct |
5 |
Correct |
153 ms |
72276 KB |
Output is correct |
6 |
Correct |
120 ms |
63316 KB |
Output is correct |
7 |
Correct |
79 ms |
57000 KB |
Output is correct |
8 |
Correct |
113 ms |
65920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
266 ms |
116220 KB |
Output is correct |
5 |
Correct |
153 ms |
72276 KB |
Output is correct |
6 |
Correct |
120 ms |
63316 KB |
Output is correct |
7 |
Correct |
79 ms |
57000 KB |
Output is correct |
8 |
Correct |
113 ms |
65920 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 |
348 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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |