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;
int dist[2505][2505], l[200005], r[200005], ans[200005];
queue<int> q;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n;
for (int i=1; i<=n; i++)
cin >> l[i] >> r[i];
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
dist[i][j]=1e8;
dist[i][i]=0;
q.push(i);
int L=i, R=i;
while (!q.empty())
{
int u=q.front();
q.pop();
for (int v=l[u]; v<L; v++)
{
dist[i][v]=dist[i][u]+1;
q.push(v);
}
for (int v=R+1; v<=r[u]; v++)
{
dist[i][v]=dist[i][u]+1;
q.push(v);
}
L=min(L, l[u]);
R=max(R, r[u]);
}
}
for (int i=1; i<=n; i++)
{
ans[i]=min(dist[i][1]+dist[1][n], dist[i][n]+dist[n][1]);
for (int j=2; j<n; j++)
ans[i]=min(ans[i], dist[i][j]+dist[j][1]+dist[j][n]-1);
}
cin >> m;
for (int i=1; i<=m; i++)
{
int x;
cin >> x;
if (ans[x]<5e7)
cout << ans[x] << '\n';
else
cout << -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... |