이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
forr(i , 1 , N)
{
adj[i] = {i , i};
}
fore(i , M)
{
int A , B;
cin>>A>>B;
if(A < B)
{
forr(j , A , min(B-1 , A + K - 1))
{
adj[j][1] = max(B , adj[j][1]);
}
}
else
{
forn(j , A , max(B + 1 , A - K + 1))
{
adj[j][0] = min(adj[j][0] , B);
}
}
}
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;
int mn = INF , mx = -INF;
int mnL , mxR;
for(int u = adj[cur][0] ; u <= adj[cur][1]; u++)
{
if(dist[u] > dist[cur] + 1)
dist[u] = dist[cur] + 1;
if(adj[u][0] < mn)
{
mn = adj[u][0];
mnL = u;
}
if(adj[u][1] > mx)
{
mx = adj[u][1];
mxR = u;
}
}
q.push(mnL);
q.push(mxR);
}
cout<<(dist[T] == INF ? -1 : dist[T])<<nl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |