# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
736093 | nguyentunglam | Passport (JOI23_passport) | C++17 | 34 ms | 22920 KiB |
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>
#define fi first
#define se second
#define endl "\n"
#define ii pair<int, int>
using namespace std;
const int N = 1e5 + 10;
int f[N], g[N], h[N];
vector<int> adj[N];
void bfs(int start, int d[]) {
queue<int> q;
d[start] = 1;
q.push(start);
while (!q.empty()) {
int u = q.front(); q.pop();
for(int &v : adj[u]) if (!d[v]) d[v] = d[u] + 1, q.push(v);
}
}
int main() {
#define task ""
cin.tie(0) -> sync_with_stdio(0);
if (fopen ("task.inp", "r")) {
freopen ("task.inp", "r", stdin);
freopen ("task.out", "w", stdout);
}
if (fopen (task".inp", "r")) {
freopen (task".inp", "r", stdin);
freopen (task".out", "w", stdout);
}
int n; cin >> n;
for(int i = 1; i <= n; i++) {
int l, r; cin >> l >> r;
for(int j = l; j <= r; j++) adj[j].push_back(i);
}
bfs(1, f);
bfs(n, g);
priority_queue<ii, vector<ii>, greater<ii> > Q;
for(int i = 1; i <= n; i++) {
if (f[i] > 0 && g[i] > 0) {
if (i == 1 || i == n) h[i] = f[i] + g[i] - 2;
else h[i] = f[i] + g[i] - 3;
}
else h[i] = 1e9;
Q.push({h[i], i});
}
while (!Q.empty()) {
int cost, u; tie(cost, u) = Q.top(); Q.pop();
if (cost != h[u]) continue;
for(int &v : adj[u]) if (h[v] > h[u] + 1) {
h[v] = h[u] + 1;
Q.push({h[v], v});
}
}
for(int i = 1; i <= n; i++) if (h[i] == 1e9) h[i] = -1;
int q; cin >> q;
while (q--) {
int x; cin >> x;
cout << h[x] << endl;
}
}
Compilation message (stderr)
# | 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... |