//#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 200015
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]=1e9;
}
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<=min(i.y.y,vis2[i.x]);j++){
if (!vis1[j]){
vis1[j]=vis1[np]+1;
q.push(j);
// cout<<np<<" -> "<<j<<" "<<vis1[j]<<" "<<vis1[np]<<'\n';
}
}
vis2[i.x]=min(np,vis2[i.x]);
}
}
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>=0;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 |
15 ms |
4940 KB |
Output is correct |
2 |
Correct |
22 ms |
5036 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
79 ms |
5452 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
164 ms |
6476 KB |
Output is correct |
7 |
Correct |
89 ms |
6176 KB |
Output is correct |
8 |
Correct |
5 ms |
6220 KB |
Output is correct |
9 |
Correct |
267 ms |
6176 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
7 ms |
4940 KB |
Output is correct |
12 |
Correct |
11 ms |
6476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
4940 KB |
Output is correct |
2 |
Correct |
22 ms |
5036 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
79 ms |
5452 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
164 ms |
6476 KB |
Output is correct |
7 |
Correct |
89 ms |
6176 KB |
Output is correct |
8 |
Correct |
5 ms |
6220 KB |
Output is correct |
9 |
Correct |
267 ms |
6176 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
7 ms |
4940 KB |
Output is correct |
12 |
Correct |
11 ms |
6476 KB |
Output is correct |
13 |
Execution timed out |
2069 ms |
5112 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2096 ms |
8368 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1094 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
999 ms |
524292 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
4940 KB |
Output is correct |
2 |
Correct |
22 ms |
5036 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
79 ms |
5452 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
6 |
Correct |
164 ms |
6476 KB |
Output is correct |
7 |
Correct |
89 ms |
6176 KB |
Output is correct |
8 |
Correct |
5 ms |
6220 KB |
Output is correct |
9 |
Correct |
267 ms |
6176 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
7 ms |
4940 KB |
Output is correct |
12 |
Correct |
11 ms |
6476 KB |
Output is correct |
13 |
Execution timed out |
2069 ms |
5112 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |