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;
typedef long long ll;
typedef vector<ll> vii;
typedef pair<ll,ll> pii;
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define pb push_back
#define pow poww
const int M=1e6+10;
const ll mod=998244353;
const ll inf=2e18;
ll pow(ll x,ll y){
ll res=1;
x%=mod;
while(y>0){
if(y%2==1){
res*=x,res%=mod;
}
x*=x,x%=mod;
y/=2;
}
return res;
}
ll n,dis[M],diss[M],ans[M];
bool vis[M];
vii adj[M];
void bfs(ll x){
queue<ll>q;
for(int i=1;i<=n;i++)
vis[i]=0,dis[i]=inf;
vis[x]=1;
dis[x]=0;
q.push(x);
while(!q.empty()){
ll y=q.front();
q.pop();
for(auto it:adj[y]){
if(!vis[it])
q.push(it),vis[it]=1,dis[it]=dis[y]+1;
}
}
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
ll TC=1;
//cin>>TC;
while(TC--){
cin>>n;
for(int i=1;i<=n;i++){
ll x,y;
cin>>x>>y;
for(int j=x;j<=y;j++){
if(j==i)
continue;
adj[j].pb(i);
}
}
bfs(1);
for(int i=1;i<=n;i++)
swap(dis[i],diss[i]);
bfs(n);
for(int i=1;i<=n;i++)
dis[i]+=diss[i];
priority_queue<pii,vector<pii>,greater<pii>>q;
for(int i=1;i<=n;i++)
ans[i]=inf;
for(int i=1;i<=n;i++){
if(i!=1&&i!=n)
q.push({dis[i]-1,i});
else
q.push({dis[i],i});
}
for(int i=1;i<=n;i++)
vis[i]=0;
while(!q.empty()){
pii p=q.top();
q.pop();
if(vis[p.S])
continue;
vis[p.S]=1;
ans[p.S]=p.F;
for(auto it:adj[p.S]){
q.push({p.F+1,it});
}
}
ll querycnt;
cin>>querycnt;
while(querycnt--){
ll x;
cin>>x;
if(ans[x]==inf)
ans[x]=-1;
cout<<ans[x]<<'\n';
}
}
return 0;
}
/*
*/
# | 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... |