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 F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
#define int long long
#define ll long long
#define y1 YONE
typedef unsigned long long ull;
using namespace std;
const int N=2505;
int dis[N][N],L[N],R[N],n,q;
pair<int,int> par[N][N];
void bfs(int s){
for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) dis[i][j]=INT_MAX;
dis[L[s]][R[s]]=1;par[L[s]][R[s]]={s,s};
queue<pair<int,int>> q;
q.push({L[s],R[s]});
while (!q.empty()){
int l=q.front().F,r=q.front().S;
q.pop();
for (int i=l;i<par[l][r].F;i++){
int l1=min(l,L[i]),r1=max(r,R[i]);
if (dis[l1][r1]!=INT_MAX) continue;
dis[l1][r1]=dis[l][r]+1;
par[l1][r1]={l,r};
q.push({l1,r1});
}
for (int i=r;i>par[l][r].S;i--){
int l1=min(l,L[i]),r1=max(r,R[i]);
if (dis[l1][r1]!=INT_MAX) continue;
dis[l1][r1]=dis[l][r]+1;
par[l1][r1]={l,r};
q.push({l1,r1});
}
}return;
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n;
for (int i=1;i<=n;i++) cin>>L[i]>>R[i];
cin>>q;
while (q--){
int x;cin>>x;
bfs(x);
if (dis[1][n]==INT_MAX) cout<<-1<<endl;
else cout<<dis[1][n]<<endl;
}
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... |