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>
typedef long long ll;
#define fastio cin.tie(0), cout.tie(0), ios::sync_with_stdio(0);
using namespace std;
#define pii pair<int, int>
#define F first
#define S second
inline bool between(int b, int a, int c){
if(a<=b && b<=c) return 1;
if(a>=b && b>=c) return 1;
return 0;
}
int k;
inline int near(int s, pii p){
if(between(s, p.F, p.S)){
if(abs(s-p.F)<k) return s;
if(p.F<s) return p.F+k-1;
if(p.F>s) return p.F-k+1;
}
if(between(p.F, s, p.S)) return p.F;
return 0;
}
int main(){
fastio
int n, m;
cin >> n >> k >> m;
vector<pii> rail(m);
for(int i=0; i<m; i++)
cin >> rail[i].F >> rail[i].S;
int q, s, t;
for(cin >> q; q; q--){
cin >> s >> t;
vector<vector<int>> way(n+1, vector<int>(0));
for(int i=0; i<m; i++)
way[near(s, rail[i])].push_back(rail[i].S);
vector<int> dp(n+1, -1);
dp[s] = 0;
int l = s, r = s;
queue<int> qu;
while(!qu.empty()) qu.pop();
for(qu.push(s); !qu.empty() || qu.front()==t; qu.pop()){
//cout << qu.front() << " : ";
for(const int &w: way[qu.front()]){
//cout << w << ' ';
if(w>r){
for(int i=r+1; i<=w; i++){
dp[i] = dp[qu.front()]+1;
qu.push(i);
}
r = w;
}
else if(w<l){
for(int i=l-1; i>=w; i--){
dp[i] = dp[qu.front()]+1;
qu.push(i);
}
l = w;
}
}
//cout << '\n';
}
cout << dp[t] << '\n';
}
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |