This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
/*#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")*/
#define vi vector<int>
#define ve vector
#define ll long long
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define onbit __builtin_popcount
#define ii pair<int,int>
#define vvi vector<vi>
#define vii vector<ii>
#define gii greater<ii>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF 1e18
#define eps 1e-7
#define eps1 1e-2
#define optimise ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX_A 1e5+5
using namespace std;
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll MOD = 1e9+7;
const int nax = 2e5+5;
const int MAX_VAL = 1e6+1;
double PI=3.14159265359;
int arx[8]={1,0,0,-1,-1,-1, 1, 1};
int ary[8]={0,1,-1, 0, 1,-1,-1, 1};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
vi adj[nax];
int main(){
optimise;
/*#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif*/
//setIO("dec");
int n,k;
cin>>n>>k;
int m;
cin>>m;
int a[n+1];
int b[n+1];
for (int i = 0; i <= n; ++i)
{
a[i]=0;
b[i]=1e9;
}
for (int i = 0; i < m; ++i)
{
int x,y;
cin>>x>>y;
if(x<y) a[x]=max(a[x],y);
else b[x]=min(b[x],y);
}
multiset<int> st;
for (int i = 0; i <= n; ++i)
{
if(i>=k){
st.erase(a[i-k]);
}
st.insert(a[i]);
for (int j = i+1; j <= (*--st.end()); ++j)
{
adj[i].pb(j);
//cout <<i<<" "<<j<<endl;
}
}
st.clear();
for (int i = n; i >= 0; --i)
{
if(i<=n-k){
st.erase(b[i+k]);
}
st.insert(b[i]);
//cout <<*st.begin()<<endl;
for (int j = i-1; j >= *st.begin(); --j)
{
//cout <<i<<" "<<j<<endl;
adj[i].pb(j);
}
}
int q;
cin>>q;
int dis[n+1];
while(q--){
int s,t;
cin>>s>>t;
queue<int> q;
q.push(s);
for (int i = 0; i <= n; ++i)
{
dis[i]=1e9;
}
dis[s]=0;
bool test=false;
while(!q.empty()){
int node=q.front();
q.pop();
if(node==t){
cout << dis[node]<<endl;
test=true;
break;
}
for(auto u:adj[node]){
if(dis[u]<=dis[node]+1) continue;
q.push(u);
dis[u]=dis[node]+1;
}
}
if(!test){
cout <<-1<<endl;
}
}
}
Compilation message (stderr)
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:35:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |