Submission #863880

#TimeUsernameProblemLanguageResultExecution timeMemory
863880prairie2022Railway Trip 2 (JOI22_ho_t4)C++17
11 / 100
2058 ms10708 KiB
#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); for(int i=0; i<m; i++){ if(near(s, rail[i])) way[near(s, rail[i])].push_back(i); } vector<int> dp(n+1, -1); dp[s] = 0; int l = s, r = s; queue<int> qu; for(qu.push(s); !qu.empty() || qu.front()==t; qu.pop()){ //cout << qu.front() << " : "; for(const int &w: way[qu.front()]){ //cout << w << ' '; if(rail[w].S>r){ for(int i=r+1; i<=rail[w].S; i++){ dp[i] = dp[qu.front()]+1; qu.push(i); } r = rail[w].S; } else if(rail[w].S<l){ for(int i=l-1; i>=rail[w].S; i--){ dp[i] = dp[qu.front()]+1; qu.push(i); } l = rail[w].S; } } //cout << '\n'; } cout << dp[t] << '\n'; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...