#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
using namespace std;
#define int long long
#define vi vector<int>
#define vl vector<long long>
#define vii vector<pair<int,int>>
#define vll vector<pair<long long,long long>>
#define pb push_back
#define ll long long
#define ld long double
#define nl '\n'
#define boost ios::sync_with_stdio(false)
#define mp make_pair
#define se second
#define fi first
#define fore(i, y) for(int i = 0; i < y; i++)
#define forr(i,x,y) for(int i = x;i<=y;i++)
#define forn(i,y,x) for(int i = y; i >= x; i--)
#define all(v) v.begin(),v.end()
#define sz(v) (int)v.size()
#define clr(v,k) memset(v,k,sizeof(v))
#define rall(v) v.rbegin() , v.rend()
#define pii pair<int,int>
#define pll pair<ll , ll>
const ll MOD = 1e9 + 7;
const ll INF = 1e18 + 1;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (gcd)
ll lcm(ll a , ll b) {return a * (b / gcd(a , b));} // least common multiple (lcm)
// HERE IS THE SOLUTION
signed main()
{
boost;
cin.tie(0);
cout.tie(0);
int N , K , M;
cin>>N>>K>>M;
vector<vi> adj(N + 1);
fore(i , M)
{
int A , B;
cin>>A>>B;
if(A < B)
{
forr(j , A , min(B-1 , A + K - 1))
{
forr(k , j + 1 , B)
{
adj[j].pb(k);
}
}
}
else
{
forn(j , A , max(B + 1 , A - K + 1))
{
forn(k , j - 1 , B)
{
adj[j].pb(k);
}
}
}
}
int Q;
cin>>Q;
while(Q--)
{
int S , T;
cin>>S>>T;
queue<int> q;
q.push(S);
vi dist(N + 1 , INF);
vector<bool> vis(N + 1 , 0);
dist[S] = 0;
while(!q.empty())
{
int cur = q.front();
q.pop();
if(vis[cur])
continue;
vis[cur] = 1;
if(cur == T)
break;
for(auto u : adj[cur])
{
if(dist[u] > dist[cur] + 1)
{
dist[u] = dist[cur] + 1;
q.push(u);
}
}
}
cout<<(dist[T] == INF ? -1 : dist[T])<<nl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
604 KB |
Output is correct |
2 |
Correct |
5 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
72 ms |
7004 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1195 ms |
69348 KB |
Output is correct |
7 |
Correct |
265 ms |
22872 KB |
Output is correct |
8 |
Correct |
19 ms |
24752 KB |
Output is correct |
9 |
Correct |
255 ms |
24792 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
548 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
604 KB |
Output is correct |
2 |
Correct |
5 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
72 ms |
7004 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1195 ms |
69348 KB |
Output is correct |
7 |
Correct |
265 ms |
22872 KB |
Output is correct |
8 |
Correct |
19 ms |
24752 KB |
Output is correct |
9 |
Correct |
255 ms |
24792 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
548 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1205 ms |
11524 KB |
Output is correct |
14 |
Correct |
387 ms |
4700 KB |
Output is correct |
15 |
Correct |
3 ms |
4188 KB |
Output is correct |
16 |
Runtime error |
583 ms |
524288 KB |
Execution killed with signal 9 |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
366 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
462 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
371 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
604 KB |
Output is correct |
2 |
Correct |
5 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
72 ms |
7004 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1195 ms |
69348 KB |
Output is correct |
7 |
Correct |
265 ms |
22872 KB |
Output is correct |
8 |
Correct |
19 ms |
24752 KB |
Output is correct |
9 |
Correct |
255 ms |
24792 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
548 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1205 ms |
11524 KB |
Output is correct |
14 |
Correct |
387 ms |
4700 KB |
Output is correct |
15 |
Correct |
3 ms |
4188 KB |
Output is correct |
16 |
Runtime error |
583 ms |
524288 KB |
Execution killed with signal 9 |
17 |
Halted |
0 ms |
0 KB |
- |