#include <bits/stdc++.h>
#define pb push_back
#define fst first
#define snd second
#define fore(i,a,b) for(int i=a,pao=b;i<pao;++i)
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(),x.end()
#define me(a,v) memset((a),(v),sizeof(a))
#define FIN ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
using namespace std;
typedef long long ll;
const int MAXM=2e5+5,MAXN=1e5+5,INF=1e9;
int A[MAXM],B[MAXM],L[MAXN],R[MAXN];
void chmin(int &a,int b){
if(a>b)a=b;
}
void chmax(int &a,int b){
if(a<b)a=b;
}
int main(){FIN;
int n,k;cin>>n>>k;
int m;cin>>m;
fill(L,L+n,INF);
fill(R,R+n,-INF);
fore(i,0,m){
cin>>A[i]>>B[i];
A[i]--;B[i]--;
if(A[i]<B[i]){
fore(j,A[i],min(A[i]+k,B[i]+1)){
chmax(R[j],B[i]);
chmin(L[j],j);
}
}else{
for(int j=A[i];j>max(A[i]-k,B[i]-1);j--){
chmax(R[j],j);
chmin(L[j],B[i]);
}
}
}
int Q;cin>>Q;
while(Q--){
int s,t;cin>>s>>t;s--;t--;
queue<pair<int,int>>q;q.push({s,s});
int steps=0;
while(!q.empty()){
auto prev=q.front();q.pop();
if(prev.fst<=t&&t<=prev.snd){
cout<<steps<<endl;
break;
}
steps++;
pair<int,int>next=prev;
for(int i=prev.fst;i<=prev.snd;i++){
chmin(next.fst,L[i]);
chmax(next.snd,R[i]);
}
if(next==prev){
cout<<-1<<endl;
break;
}
q.push(next);
}
}
}
| # | 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... |