이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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;
const int N=2005;
vector <int> g[N];
int L[N],R[N];
queue <int> q;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n,k;
cin>>n>>k;
int m;
cin>>m;
for(int i=1;i<=n;i++){
L[i]=i;R[i]=i;
}
for(int i=1;i<=m;i++){
int a,b;
cin>>a>>b;
if(a<b){
for(int j=a;j<=min(b-1,a+k-1);j++){
R[j]=max(R[j],b);
}
}
else{
for(int j=a;j>=max(b+1,a-k+1);j--){
L[j]=min(L[j],b);
}
}
}
int Q;
cin>>Q;
while(Q--){
int s,t;
cin>>s>>t;
vector <int> d(n+1,-1),vis(n+1);
d[s]=0;
q.push(s);
vis[s]=1;
while(!q.empty()){
int v=q.front();
q.pop();
int mn=1e9,mx=0;
int mxr=-1,mnl=-1;
for(int i=L[v];i<=R[v];i++){
if(d[i]==-1)d[i]=d[v]+1;
if(L[i]<mn){
mn=L[i];
mnl=i;
}
if(R[i]>mx){
mx=R[i];
mxr=i;
}
}
if(!vis[mxr]){
vis[mxr]=1;
q.push(mxr);
}
if(!vis[mnl]){
vis[mnl]=1;
q.push(mnl);
}
}
cout<<d[t]<<"\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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |