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 int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);
int n;
cin>>n;
vector <int> l(n+1),r(n+1),mx(n+1);
for(int i=1;i<=n;i++){
cin>>l[i]>>r[i];
mx[i]=max(mx[i-1],r[i]);
}
int query;
cin>>query;
while(query--){
int s;
cin>>s;
if(s==1){
int x=r[s];
int ans=1;
while(x!=n){
if(mx[x]==x){
ans=-1;
break;
}
ans++;
x=mx[x];
}
cout<<ans<<"\n";
continue;
}
queue < pair <int,int> > q;
q.push({l[s],r[s]});
vector <vector <int> > dis(n+1,vector <int> (n+1,1e18));
dis[l[s]][r[s]]=1;
while(!q.empty()){
int left=q.front().ff;
int right=q.front().ss;
q.pop();
for(int i=left;i<=right;i++){
if(dis[min(left,l[i])][max(right,r[i])]>dis[left][right]+1){
dis[min(left,l[i])][max(right,r[i])]=dis[left][right]+1;
q.push({min(left,l[i]),max(right,r[i])});
}
}
}
if(dis[1][n]==1e18)cout<<-1<<"\n";
else cout<<dis[1][n]<<"\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... |