This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3","unroll-loops","Ofast")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
const int INF=1e9;
#define pii pair<int,int>
#define endl '\n'
vector<pii> tmp;
struct seg{
int l,m,r;
int maxval=-INF,minval=INF;
int maxlz=-INF,minlz=INF;
seg *lc,*rc;
int rmax(){
return max(maxlz,maxval);
}
int rmin(){
return min(minlz,minval);
}
void pull(){
maxval=max(lc->rmax(),rc->rmax());
minval=min(lc->rmin(),rc->rmin());
}
void push(){
lc->maxlz=max(lc->maxlz,maxlz);
rc->maxlz=max(rc->maxlz,maxlz);
lc->minlz=min(lc->minlz,minlz);
rc->minlz=min(rc->minlz,minlz);
}
seg(int l1,int r1){
l=l1,r=r1;
m=(l+r)>>1;
if(r-l==1){
maxval=minval=maxlz=minlz=l1;
return;
}
lc=new seg(l,m);
rc=new seg(m,r);
pull();
}
void add(int a,int b,int x){
if(a<=l&&b>=r){
maxlz=max(maxlz,x);
minlz=min(minlz,x);
return;
}
push();
if(a<m){
lc->add(a,b,x);
}
if(b>m){
rc->add(a,b,x);
}
pull();
}
pii ask(int a,int b){
if(a<=l&&b>=r){
return {rmin(),rmax()};
}
pii ans={INF,-INF};
push();
if(a<m){
pii p=lc->ask(a,b);
if(p.first<ans.first)ans.first=p.first;
if(p.second>ans.second)ans.second=p.second;
}
if(b>m){
pii p=rc->ask(a,b);
if(p.first<ans.first)ans.first=p.first;
if(p.second>ans.second)ans.second=p.second;
}
pull();
return ans;
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,k;cin>>n>>k;
vector<seg*> v(17);
for(int i=0;i<17;i++){
v[i]=new seg(0,n);
}
vector<pii> dp0(n),dp1(n);
int m;cin>>m;
for(int i=0;i<m;i++){
int a,b;cin>>a>>b;a--;b--;
if(a<b){
int c=min(a+k,b);
v[0]->add(a,c,b);
}
else{
int c=max(b+1,a-k+1);
v[0]->add(c,a+1,b);
}
}
for(int j=0;j<n;j++){
dp0[j]=v[0]->ask(j,j+1);
}
for(int i=1;i<17;i++){
if(i&1){
for(int j=0;j<n;j++){
v[i]->add(j,j+1,dp0[j].first);
v[i]->add(j,j+1,dp0[j].second);
}
for(int j=0;j<n;j++){
int a,b;tie(a,b)=dp0[j];
dp1[j]=v[i]->ask(a,b+1);
}
}
else{
for(int j=0;j<n;j++){
v[i]->add(j,j+1,dp1[j].first);
v[i]->add(j,j+1,dp1[j].second);
}
for(int j=0;j<n;j++){
int a,b;tie(a,b)=dp1[j];
dp0[j]=v[i]->ask(a,b+1);
}
}
}
int q;cin>>q;
for(int i=0;i<q;i++){
int a,b;cin>>a>>b;a--;b--;
if(a<b){
int ans=0;
int l=a,r=a;
bool flag=0;
for(int j=16;j>0;j--){
int c,d;
tie(c,d)=v[j]->ask(l,r+1);
if(d<b){
ans+=(1<<(j-1));
l=c,r=d;
}
else{
flag=1;
}
}
if(flag==0){
cout<<-1<<endl;
}
else{
cout<<ans+1<<endl;
}
}
else{
int ans=0;
int l=a,r=a;
bool flag=0;
for(int j=16;j>0;j--){
int c,d;
tie(c,d)=v[j]->ask(l,r+1);
if(c>b){
ans+=(1<<(j-1));
l=c,r=d;
}
else{
flag=1;
}
}
if(flag==0){
cout<<-1<<endl;
}
else{
cout<<ans+1<<endl;
}
}
}
return 0;
}
# | 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... |