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 ll int
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define vll vector<ll>
#define bc back()
#define arr array
#define pll vector<pair<ll,ll>>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>
template<class S,class T>
bool chmin(S &a,const T &b) {
return a>b?(a=b)==b:false;
}
template<class S,class T>
bool chmax(S &a,const T &b) {
return a<b?(a=b)==b:false;
}
//void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=1e9;
const ll mod=1e9+7;
const ll N=2e5+7;
const ld eps=1e-9;
pair<ll,ll> t[20][N*4],add[N*4];
ll a[N],b[N];
ll n,k,m;
ll d[N];
ll l[N][20],r[N][20];
void push(ll v,ll tl,ll tr){
if(add[v] == make_pair(inf,-inf))return;
if(tl!=tr){
chmin(add[v*2].fr,add[v].fr);
chmax(add[v*2].sc,add[v].sc);
chmin(add[v*2+1].fr,add[v].fr);
chmax(add[v*2+1].sc,add[v].sc);
}
chmin(t[0][v].fr,add[v].fr);
chmax(t[0][v].sc,add[v].sc);
add[v]={inf,-inf};
}
void upds(ll l,ll r,ll val,ll v=1,ll tl=1,ll tr=n){
push(v,tl,tr);
if(tl>r || tr<l)return;
if(l<=tl && tr<=r){
add[v]={val,val};
push(v,tl,tr);
return;
}ll tm=(tl+tr)/2;
upds(l,r,val,v*2,tl,tm);
upds(l,r,val,v*2+1,tm+1,tr);
}
void upd(ll id,ll pos,ll l,ll r,ll v=1,ll tl=1,ll tr=n){
if(tl>pos || tr<pos) return;
if(tl==tr){
t[id][v]={l,r};
return;
}ll tm=(tl+tr)/2;
upd(id,pos,l,r,v*2,tl,tm);
upd(id,pos,l,r,v*2+1,tm+1,tr);
t[id][v].fr=min(t[id][v*2].fr,t[id][v*2+1].fr);
t[id][v].sc=max(t[id][v*2].sc,t[id][v*2+1].sc);
}
pair<ll,ll> get(ll id,ll l,ll r,ll v=1,ll tl=1,ll tr=n){
push(v,tl,tr);
if(tl>r || tr<l) return {inf,-inf};
if(l<=tl && tr<=r) return t[id][v];
ll tm=(tl+tr)/2;
pair<ll,ll> a,b;
a=get(id,l,r,v*2,tl,tm);
b=get(id,l,r,v*2+1,tm+1,tr);
return {min(a.fr,b.fr),max(a.sc,b.sc)};
}
void solve(){
ll i,j;
cin>>n>>k>>m;
for(i=1;i<=n;i++) {
for(j=0;j<20;j++)l[i][j]=r[i][j]=i;
}
for(i=0;i<20;i++){
for(j=0;j<n*4;j++)t[i][j]={inf,-inf};
}
for(i=1;i<=n*4;i++)add[i]={inf,-inf};
for(i=1;i<=m;i++){
cin>>a[i]>>b[i];
ll l,r;
if(a[i]<b[i]){
l=a[i];
r=min(b[i]-1,a[i]+k-1);
}else{
l=max(b[i]+1,a[i]-k+1);
r=a[i];
}
upds(l,r,b[i]);
}
for(i=1;i<=n;i++){
pair<ll,ll> p=get(0,i,i);
chmin(l[i][0],p.fr);
chmax(r[i][0],p.sc);
}
for(i=0;i<n*4;i++)t[0][i]=add[i]={inf,-inf};
for(i=1;i<=n;i++){
upd(0,i,l[i][0],r[i][0]);
}
for(j=1;j<20;j++){
for(i=1;i<=n;i++){
pair<ll,ll> p=get(j-1,l[i][j-1],r[i][j-1]);
chmin(l[i][j],p.fr);
chmax(r[i][j],p.sc);
upd(j,i,l[i][j],r[i][j]);
}
}
ll qq;
cin>>qq;
for(i=0;i<qq;i++){
ll s,t;
cin>>s>>t;
ll x,y;
x=y=s;
ll ans=0;
for(j=19;j>=0;j--){
pair<ll,ll> p=get(j,x,y);
ll L=p.fr,R=p.sc;
if(L>t || R<t){
x=L;
y=R;
ans+=(1ll<<j);
}
}
pair<ll,ll> p=get(0,x,y);
ll L=p.fr,R=p.sc;
if(s<t){
if(R>=t)cout<<ans+1<<endl;
else cout<<-1<<endl;
}else{
if(L<=t)cout<<ans+1<<endl;
else cout<<-1<<endl;
}
}
}
signed main(){
start();
ll t=1;
//cin>>t;
while(t--) solve();
return 0;
}
/*
3 1 5
2 2
3 2
2 2
3 2
10
*/
# | 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... |