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 ll int
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
using namespace std;
const ll maxn=200005, inf=1e9;
ll le[maxn], ri[maxn], L[maxn], R[maxn], dis[maxn];
priority_queue <pll, vector <pll>, greater<pll>> q;
vector <pll> A[maxn];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n; cin >> n;
for (ll i=1; i<=n; i++)
{
cin >> L[i] >> R[i];
le[i]=ri[i]=dis[i]=inf;
}
for (ll i=1; i<=n; i++)
for (ll j=L[i]; j<=R[i]; j++)
A[j].pb({i, 1});
queue <ll> q1, q2;
for (ll i=1; i<=n; i++)
{
if (L[i]==1) le[i]=0, q1.push(i);
if (R[i]==n) ri[i]=0, q2.push(i);
}
while (q1.size())
{
ll u=q1.front(); q1.pop();
for (pll v:A[u])
if (le[v.fi]==inf)
le[v.fi]=le[u]+1, q1.push(v.fi);
}
while (q2.size())
{
ll u=q2.front(); q2.pop();
for (pll v:A[u])
if (ri[v.fi]==inf)
ri[v.fi]=ri[u]+1, q2.push(v.fi);
}
for (ll i=1; i<=n; i++)
A[n+1].pb({i, le[i]+ri[i]});
dis[n+1]=0; q.push({0, n+1});
while (!q.empty())
{
ll u=q.top().se, disu=q.top().fi; q.pop();
if (disu!=dis[u])
continue;
for (pll child:A[u])
{
ll v=child.fi, w=child.se;
if (dis[v]>disu+w)
{
dis[v]=disu+w;
q.push({dis[v], v});
}
}
}
ll q; cin >> q;
for (ll i=1; i<=q; i++)
{
ll x; cin >> x;
if (dis[x]==inf) cout << "-1\n";
else cout << dis[x]+1 << "\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... |