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 long long
#define ntr "\n"
#define mod (ll)(1e9+7)
#define taskname ""
#define frep freopen(taskname".inp","r",stdin); freopen(taskname".out","w",stdout);
using namespace std;
vector<ll> adj[3001];
ll dist[3001];
ll vis[3001];
ll n;
ll q;
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//frep;
cin>>n;
for(int i=1;i<=n;i++){
ll l,r;
cin>>l>>r;
for(int j=l;j<=r;j++){
adj[i].push_back(j);
}
}
cin>>q;
for(int i=1;i<=q;i++){
ll x;
cin>>x;
fill(dist+1,dist+n+1,1e18);
fill(vis+1,vis+n+1,0);
dist[x]=1;
queue<ll> q;
q.push(x);
while(!q.empty()){
ll u=q.front();
q.pop();
if(vis[u]) continue;
vis[u]=1;
for(auto v:adj[u]){
if(dist[v]>dist[u]+1){
dist[v]=dist[u]+1;
q.push(v);
}
}
}
ll mx=max(dist[1],dist[n]);
if(mx==1e18) cout<<-1;
else cout<<mx;
cout<<ntr;
}
}
# | 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... |