#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;
vector<vector<int>> way(n+1);
vector<int> dp(n+1);
queue<int> qu;
for(cin >> q; q; q--){
cin >> s >> t;
way.assign(n+1, vector<int>(0));
for(int i=0; i<m; i++)
way[near(s, rail[i])].push_back(rail[i].S);
dp.assign(n+1, -1);
dp[s] = 0;
int l = s, r = s;
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 |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
5980 KB |
Output is correct |
2 |
Correct |
16 ms |
6092 KB |
Output is correct |
3 |
Correct |
19 ms |
6488 KB |
Output is correct |
4 |
Correct |
15 ms |
5980 KB |
Output is correct |
5 |
Correct |
30 ms |
6732 KB |
Output is correct |
6 |
Correct |
31 ms |
8300 KB |
Output is correct |
7 |
Correct |
31 ms |
8544 KB |
Output is correct |
8 |
Correct |
17 ms |
5592 KB |
Output is correct |
9 |
Correct |
17 ms |
5592 KB |
Output is correct |
10 |
Correct |
32 ms |
7124 KB |
Output is correct |
11 |
Correct |
31 ms |
7120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2045 ms |
159452 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2107 ms |
114108 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |