This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
pr[0] = 1;
vector<int> su(n, 1 << 27);
su[n - 1] = 0;
func(su);
su[n - 1] = 1;
vector<int> d(n);
for (int i = 0; i < n; i++) {
d[i] = pr[i] + su[i];
}
func(d);
for (int i = 0; i < n; i++) {
if (d[i] > n) {
d[i] = -1;
}
else {
d[i] -= 1;
}
}
int q;
cin >> q;
while (q--) {
int x;
cin >> x;
x -= 1;
cout << d[x] << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |