#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> l(n), r(n);
for (int i = 0; i < n; i++) {
cin >> l[i] >> r[i];
l[i] -= 1;
}
vector<vector<int>> f(n * 2);
for (int i = 0; i < n; i++) {
for (int u = l[i] + n, v = r[i] + n; u < v; u >>= 1, v >>= 1) {
if (u & 1) {
f[u++].emplace_back(i);
}
if (v & 1) {
f[--v].emplace_back(i);
}
}
}
auto func = [&](vector<int> &d) -> void {
priority_queue<pair<int, int>> pri_que;
for (int i = 0; i < n; i++) {
pri_que.emplace(-d[i], i);
}
vector<bool> vis(n), used(n * 2);
while (!pri_que.empty()) {
int u = pri_que.top().second;
pri_que.pop();
if (!vis[u]) {
vis[u] = true;
int x = u + n;
while (x > 0 && !used[x]) {
used[x] = true;
for (int v : f[x]) {
if (d[v] > d[u] + 1) {
d[v] = d[u] + 1;
pri_que.emplace(-d[v], v);
}
}
x >>= 1;
}
}
}
};
vector<int> pr(n, 1 << 27);
pr[0] = 0;
func(pr);
vector<int> su(n, 1 << 27);
su[n - 1] = 0;
func(su);
vector<int> d(n);
for (int i = 0; i < n; i++) {
d[i] = pr[i] + su[i];
}
func(d);
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
x -= 1;
cout << d[x] << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |