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()
{
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n;
vector<vector<int>> tree(4 * n);
function<void(int, int, int, int, int, int)> wrzuc = [&](int v, int l, int r, int ql, int qr, int i) {
if (ql <= l && r <= qr) {
tree[v].push_back(i);
return;
}
int m = (l + r) / 2;
if (ql <= m) wrzuc(2 * v, l, m, ql, qr, i);
if (m < qr) wrzuc(2 * v + 1, m + 1, r, ql, qr, i);
};
function<int(int, int, int, int)> szuk = [&](int v, int l, int r, int x) {
if (l == r) return v;
int m = (l + r) / 2;
if (x <= m) return szuk(2 * v, l, m, x);
else return szuk(2 * v + 1, m + 1, r, x);
};
for (int i = 0; i < n; i++) {
int l, r; cin >> l >> r;
l--, r--;
wrzuc(1, 0, n - 1, l, r, i);
}
const int oo = 1e9 + 7;
auto sssp = [&](vector<int> ini) {
auto dist = ini;
auto tree_save = tree;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
for (int i = 0; i < n; i++) if (ini[i] != oo) q.emplace(ini[i], i);
while (!q.empty()) {
auto [_, u] = q.top();
q.pop();
if(_ != dist[u]) continue;
u = szuk(1, 0, n - 1, u);
while (u) {
while (!tree[u].empty()) {
int to = tree[u].back();
if (dist[to] > _ + 1) q.emplace(dist[to] = _ + 1, to);
tree[u].pop_back();
}
u /= 2;
}
}
tree = tree_save;
return dist;
};
vector<int> dist_0(n, oo); dist_0[0] = 0; dist_0 = sssp(dist_0);
vector<int> dist_n(n, oo); dist_n[n - 1] = 0; dist_n = sssp(dist_n);
vector<int> dist_merge(n);
dist_0[0]++; dist_n[n - 1]++;
for (int i = 0; i < n; i++) dist_merge[i] = dist_0[i] + dist_n[i];
dist_merge = sssp(dist_merge);
int q; cin >> q;
while (q--) {
int x; cin >> x;
x--;
cout << (dist_merge[x] >= oo ? -1 : dist_merge[x] - 1) << endl;
}
}
# | 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... |