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
#define down cout<<'\n';
#define NHP ios_base::sync_with_stdio(0);cout.tie(0);cin.tie(0);
#define modwwe int t;cin>>t; while(t--)
#define bit(i,j) (i>>j&1)
#define sobit(a) __builtin_popcountll(a)
#define task "teamwork"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".out","w",stdout)
#define pb push_back
#define checktime cerr << (double)clock() / CLOCKS_PER_SEC * 1000 << " ms";
using namespace std;
void phongbeo();
const int mod2=998244353;
const int mod1=998244353;
struct ib
{
int a;
int b;
};
struct icd
{
int a,b;
};
struct ic
{
int a,b,c;
};
struct id
{
int a,b,c,d;
};
struct ie
{
int a,b,c, d,e;
};
int n,m,s1,s2,s4,s3,sf,k,r,mid,s5,s6,mx,s7,s8,s9,mx2,res,dem2=0,dem=0,l;
int i,s10,s12,c;
int el=29;
main()
{
///#ifndef ONLINE_JUDGE
/// fin(task),fou(task);
///#endif
NHP
/// cin>>s1;
/// modwwe
phongbeo();
}
ib lazy[17][400001];
ib t[17][400001];
ib st[17][100001];
void build(int node,int l,int r)
{
for(int f=0;f<17;f++){
lazy[f][node].a=n+1;
lazy[f][node].b=0;}
if(l==r){
for(int f=0;f<17;f++){
t[f][node].a=l;
t[f][node].b=r;}
return;
}
int mid=l+r>>1;
build(node<<1,l,mid),build(node<<1|1,mid+1,r);
for(int f=0;f<17;f++){
t[f][node].a=min(t[f][node<<1].b,t[f][node<<1|1].a);
t[f][node].b=max(t[f][node<<1].b,t[f][node<<1|1].b);
}
}
void ff(int x,int f)
{
for(int i=x*2;i<=x*2+1;i++)
t[f][i].a=min(t[f][i].a,lazy[f][x].a),
t[f][i].b=max(t[f][i].b,lazy[f][x].b),
lazy[f][i].a=min(lazy[f][i].a,lazy[f][x].a),
lazy[f][i].b=max(lazy[f][i].b,lazy[f][x].b);
}
void upd(int node,int l,int r,int l1,int r1,int c,int e,int f)
{
if(l>r1||r<l1) return;
if(l>=l1&&r<=r1)
{
if(e==0) t[f][node].a=min(t[f][node].a,c),lazy[f][node].a=min(lazy[f][node].a,c);
if(e==1) t[f][node].b=max(t[f][node].b,c),lazy[f][node].b=max(lazy[f][node].b,c);
return; }
int mid=l+r>>1;
ff(node,f);
upd(node<<1,l,mid,l1,r1,c,e,f);
upd(node<<1|1,mid+1,r,l1,r1,c,e,f);
t[f][node].a=min(t[f][node<<1].b,t[f][node<<1|1].a);
t[f][node].b=max(t[f][node<<1].b,t[f][node<<1|1].b);
}
void setup(int node,int l,int r,int f)
{
if(l==r)
{ st[0][l]=t[0][node];
return;
}
int mid=l+r>>1;
ff(node,f);
setup(node<<1,l,mid,f);
setup(node<<1|1,mid+1,r,f);
}
int get(int node,int l,int r,int l1,int r1,int f,int c)
{ ///cout<<c,down
if(l>r1||r<l1)
{
if(c==0)return n+1;
if(c==1) return 0;
}
if(l>=l1&&r<=r1)
{
if(c==0)return t[f][node].a;
if(c==1) return t[f][node].b;
}
int mid=l+r>>1;
ff(node,f);
if(c==0) return min(get(node<<1,l,mid,l1,r1,f,c),get(node<<1|1,mid+1,r,l1,r1,f,c));
else return max(get(node<<1,l,mid,l1,r1,f,c),get(node<<1|1,mid+1,r,l1,r1,f,c));
}
void phongbeo()
{
cin>>n>>k;
cin>>m;
build(1,1,n);
for(int i=1;i<=m;i++)
{
cin>>l>>r;
if(l>r)
{
s2=max(l-k+1,r);
upd(1,1,n,s2,l,r,0,0);
}
else
{
s2=min(l+k-1,r);
upd(1,1,n,l,s2,r,1,0);
}
}
setup(1,1,n,0);
for(int j=1;j<17;j++)
for(int i=1;i<=n;i++)
{
st[j][i].a=min(get(1,1,n,i,st[j-1][i].b,j-1,0),get(1,1,n,st[j-1][i].a,i,j-1,0));
st[j][i].b=max(get(1,1,n,i,st[j-1][i].b,j-1,1),get(1,1,n,st[j-1][i].a,i,j-1,1));
/// if(j==1&&i==3) cout<<st[1][3].b<<" "<<get(1,1,n,st[j-1][i].a,i,j-1,1)<<" "<<st[j-1][i].a<<" "<<i;
upd(1,1,n,i,i,st[j][i].a,0,j);
upd(1,1,n,i,i,st[j][i].b,1,j);
}
cin>>m;
while(m--)
{
cin>>l>>r;
s2=0;
s3=l;
for(int j=16;j>=0;--j)
{
if(l<r)
{s4=get(1,1,n,l,s3,j,1);
if(s4<r ){
s3=s4;
s2=s2+(1<<j);}
else
if(s4==s3)
{
s4=get(1,1,n,l,s3,j,0);
l=s4;
s2+=(1<<j);
}
}
else
{s4=get(1,1,n,s3,l,j,0);
if(s4>r&&s4!=s3){
s3=s4;
s2=s2+(1<<j);}
else
{ ///if(r==2&&
if(s4==s3)
{ ///if(r==2) cout<<1,down
s4=get(1,1,n,s3,l,j,1);
l=s4;
s2=s2+(1<<j);
}
}
}
}
if(l<r)
{
s4=get(1,1,n,l,s3,0,1);
if(s4<r){cout<<-1; }
else cout<<s2+1;
}
else
{ ///if(r==2)
/// cout<<s3<<" "<<s2<<" "<<get(1,1,n,s3,l,0,0)<<" "<<l,down
s4=get(1,1,n,s3,l,0,0);
if(s4>r){cout<<-1;}
else cout<<s2+1;
}
down
}
}
Compilation message (stderr)
Main.cpp:43:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
43 | main()
| ^~~~
Main.cpp: In function 'void build(int, int, int)':
Main.cpp:67:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
67 | int mid=l+r>>1;
| ~^~
Main.cpp: In function 'void upd(int, int, int, int, int, int, int, int)':
Main.cpp:92:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
92 | int mid=l+r>>1;
| ~^~
Main.cpp: In function 'void setup(int, int, int, int)':
Main.cpp:105:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
105 | int mid=l+r>>1;
| ~^~
Main.cpp: In function 'int get(int, int, int, int, int, int, int)':
Main.cpp:122:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
122 | int mid=l+r>>1;
| ~^~
# | 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... |