//#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 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);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
2636 KB |
Output is correct |
2 |
Correct |
18 ms |
2684 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Runtime error |
5 ms |
5196 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
2636 KB |
Output is correct |
2 |
Correct |
18 ms |
2684 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Runtime error |
5 ms |
5196 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2068 ms |
6440 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
13 ms |
13656 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1048 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
2636 KB |
Output is correct |
2 |
Correct |
18 ms |
2684 KB |
Output is correct |
3 |
Correct |
2 ms |
2636 KB |
Output is correct |
4 |
Runtime error |
5 ms |
5196 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |