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++)
{
if (L[i]==1) le[i]=0;
if (R[i]==n) ri[i]=0;
}
for (ll i=2; i<=n; i++)
for (ll j=L[i]; j<i; j++)
le[i]=min(le[i], le[j]+1);
for (ll i=n-1; i>=1; i--)
for (ll j=R[i]; j>i; j--)
ri[i]=min(ri[i], ri[j]+1);
for (ll i=1; i<=n; i++)
A[n+1].pb({i, le[i]+ri[i]});
for (ll i=1; i<=n; i++)
for (ll j=L[i]; j<=R[i]; j++)
A[j].pb({i, 1});
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... |