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>
//#define int long long
#define ll long long
using namespace std;
const int N = 1e6 + 5;
const int oo = 1e9;
int n, m, k, q, a[N], b[N], S[N], T[N];
namespace sub3{
int le[N], ri[N], f[N];
int st[2][N << 2];
void build(int i,int l,int r){
if(l == r){
st[0][i] = st[1][i] = l;
return;
}
int mid = (l + r) / 2;
build(i << 1, l, mid);
build(i << 1|1, mid + 1, r);
st[0][i] = n + 1;
st[1][i] = 0;
}
void dolz(int i){
if(st[0][i] != n + 1){
int x = st[0][i]; st[0][i] = n + 1;
st[0][i << 1] = min(st[0][i << 1], x);
st[0][i << 1|1] = min(st[0][i << 1|1], x);
}
if(st[1][i] != 0){
int x = st[1][i]; st[1][i] = 0;
st[1][i << 1] = max(st[1][i << 1], x);
st[1][i << 1|1] = max(st[1][i << 1|1], x);
}
}
void update(int i,int l,int r,int u,int v,int val,int type){
if(l > r || r < u || v < l) return;
if(u <= l && r <= v){
if(!type) st[0][i] = min(st[0][i], val);
else st[1][i] = max(st[1][i], val);
return;
}
dolz(i);
int mid = (l + r) / 2;
update(i << 1, l, mid, u, v, val, type);
update(i << 1|1, mid + 1, r, u, v, val, type);
}
void done(int i,int l,int r){
if(l == r){
le[l] = st[0][i];
ri[l] = st[1][i];
return;
}
dolz(i);
int mid = (l + r) / 2;
done(i << 1, l, mid);
done(i << 1|1, mid + 1, r);
}
void solve(){
build(1, 1, n);
for(int i = 1; i <= m; i ++){
if(a[i] < b[i]){
update(1, 1, n, a[i], min(a[i] + k - 1, b[i] - 1), b[i], 1);
}else update(1, 1, n, max(a[i] - k + 1, b[i] + 1), a[i], b[i], 0);
}
done(1, 1, n);
for(int k = 1; k <= q; k ++){
int x = S[k], y = T[k];
queue<int> q;
for(int i = 1; i <= n; i ++) f[i] = oo;
for(int i = le[x]; i <= ri[x]; i ++){
f[i] = 1;
q.push(i);
}
int pl = le[x], pr = ri[x];
f[x] = 0;
while(!q.empty()){
int u = q.front(); q.pop();
if(le[u] < pl){
for(int i = le[u]; i <= pl - 1; i ++){
f[i] = f[u] + 1;
q.push(i);
}
pl = le[u];
}
if(ri[u] > pr){
for(int i = pr + 1; i <= ri[u]; i ++){
f[i] = f[u] + 1;
q.push(i);
}
pr = ri[u];
}
}
if(f[y] == oo){
cout << -1 << "\n";
continue;
}else cout << f[y] << "\n";
}
}
}
//namespace sub5{
//
//}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "v"
if(fopen(task ".inp","r")){
freopen(task ".inp","r",stdin);
freopen(task ".out","w",stdout);
}
cin >> n >> k >> m;
for(int i = 1; i <= m; i ++) cin >> a[i] >> b[i];
cin >> q;
for(int i = 1; i <= q; i ++) cin >> S[i] >> T[i];
sub3 :: solve();
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:118:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | freopen(task ".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:119:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
119 | freopen(task ".out","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... |