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;
const int MAXN = 1e6 + 25;
const int inf = MAXN << 4;
int n, q;
pair <int, int> a[MAXN];
vector <pair <int, int>> radj[MAXN];
int dist1[MAXN], dist2[MAXN], dist3[MAXN];
int main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first >> a[i].second;
for (int j = a[i].first; j <= a[i].second; j++) {
radj[j].push_back({i + n, 0});
}
}
for (int i = 1; i <= n; i++) {
radj[i + n].push_back({i, 1});
}
for (int i = 1; i <= 2 * n; i++) {
dist1[i] = dist2[i] = inf;
}
priority_queue <pair <int, int>, vector <pair <int, int>>, greater <>> cur;
cur.push({0, 1}); dist1[1] = 0;
while (!cur.empty()) {
auto k = cur.top();
cur.pop();
if (k.first > dist1[k.second]) continue;
for (auto j : radj[k.second]) {
if (k.first + j.second < dist1[j.first]) {
dist1[j.first] = k.first + j.second;
cur.push({dist1[j.first], j.first});
}
}
}
cur.push({0, n}); dist2[n] = 0;
while (!cur.empty()) {
auto k = cur.top();
cur.pop();
if (k.first > dist2[k.second]) continue;
for (auto j : radj[k.second]) {
if (k.first + j.second < dist2[j.first]) {
dist2[j.first] = k.first + j.second;
cur.push({dist2[j.first], j.first});
}
}
}
for (int i = 1; i <= 2 * n; i++) {
dist3[i] = dist1[i] + dist2[i];
cur.push({dist3[i], i});
}
while (!cur.empty()) {
auto k = cur.top();
cur.pop();
if (k.first > dist3[k.second]) continue;
for (auto j : radj[k.second]) {
if (k.first + j.second < dist3[j.first]) {
dist3[j.first] = k.first + j.second;
cur.push({dist3[j.first], j.first});
}
}
}
for (int i = 1; i <= 2 * n; i++) if (dist3[i] > n) dist3[i] = -1;
cin >> q;
while (q--) {
int x; cin >> x;
cout << dist3[x] << '\n';
}
}
# | 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... |