//#pragma GCC optimize("Ofast,unroll-loops,O3")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define N 100015
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
int n,k,m;
int vis1[N],vis2[N];
vector<pair<int,pii> >g[N];
void bfs(int a,int b){
for (int i=0;i<=n;i++){
vis1[i]=0;
// vis2[i]=0;
}
vis1[a]=1;
queue<int>q;
q.push(a);
while (!q.empty()){
int np=q.front(); q.pop();
for (auto i:g[np]){
for (int j=i.y.x;j<=i.y.y;j++){
if (!vis1[j]){
vis1[j]=vis1[np]+1;
q.push(j);
// cout<<np<<" -> "<<j<<" "<<vis1[j]<<" "<<vis1[np]<<'\n';
}
}
}
}
if (!vis1[b]) cout<<-1<<'\n';
else cout<<vis1[b]-1<<'\n';
}
signed main(){
fast
cin>>n>>k>>m;
for (int i=1;i<=m;i++){
int a,b; cin>>a>>b;
if (a>b){
for (int j=a;j>=a-k+1;j--){
g[j].push_back({i,{b,j-1}});
}
}
else {
for (int j=a;j<=a+k-1;j++){
g[j].push_back({i,{j+1,b}});
}
}
}
int q; cin>>q;
while (q--){
int a,b; cin>>a>>b;
bfs(a,b);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
2636 KB |
Output is correct |
2 |
Correct |
18 ms |
2696 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Runtime error |
4 ms |
5228 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
2636 KB |
Output is correct |
2 |
Correct |
18 ms |
2696 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Runtime error |
4 ms |
5228 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2069 ms |
7024 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
35 ms |
33020 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
729 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
2636 KB |
Output is correct |
2 |
Correct |
18 ms |
2696 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Runtime error |
4 ms |
5228 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |