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"
#ifdef mlocal
#include "./Competitive-Code-Library/snippets/prettyprint.h"
#endif
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
#define endl '\n'
const int INF = 1e9;
int main() {
#ifdef mlocal
freopen("test.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
int n; cin >> n;
vector<ii> seg(n);
for_(i, 0, n) cin >> seg[i][0] >> seg[i][1], seg[i][0]--, seg[i][1]--;
function<vi(int)> bfs = [&] (int s) {
vi dist(n, INF);
dist[s] = 0;
queue<int> q;
q.push(s);
while (q.size()) {
auto p = q.front(); q.pop();
for_(i, 0, n) if (dist[i] == INF and seg[i][0] <= p and seg[i][1] >= p) {
dist[i] = dist[p] + 1;
q.push(i);
}
}
return dist;
};
vi ans = bfs(0);
{
vi ans2 = bfs(n - 1);
for_(i, 0, n) ans[i] += ans2[i];
}
for_(i, 1, n - 1) if (ans[i] < INF) ans[i]--;
priority_queue<ii, vector<ii>, greater<ii>> pq;
for_(i, 0, n) pq.push({ans[i], i});
while (pq.size()) {
auto [dist, p] = pq.top(); pq.pop();
if (dist != ans[p]) continue;
for_(i, 0, n) if (seg[i][0] <= p and seg[i][1] >= p) {
if (ans[i] > ans[p] + 1) {
ans[i] = ans[p] + 1;
pq.push({ans[i], i});
}
}
}
int q; cin >> q;
while (q--) {
int p; cin >> p;
p--;
cout << (ans[p] >= INF ? -1 : ans[p]) << 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... |