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 icd
{
int a,b;
};
struct ib
{
int a;
int 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();
}
struct IT{
ib t[400007];
int lf[100007];
int rf[100007];
void build(int node,int l,int r)
{
if(l==r)
{
t[node]={lf[l],rf[l]};
return;
}
int mid=l+r>>1;
build(node<<1,l,mid);
build(node<<1|1,mid+1,r);
t[node].a=min(t[node<<1].a,t[node<<1|1].a);
t[node].b=max(t[node<<1].b,t[node<<1|1].b);
}
ib get(int node,int l,int r,int l1,int r1)
{
if(l>r1||r<l1) return{n+1,0};
if(l>=l1&&r<=r1) return t[node];
int mid=l+r>>1;
ib aa=get(node<<1,l,mid,l1,r1);
ib ab=get(node<<1|1,mid+1,r,l1,r1);
ab.a=min(ab.a,aa.a);
ab.b=max(ab.b,aa.b);
return ab;
}
}seg[20];
vector<pair<int,int>> v[100001],v2[100001];
void phongbeo()
{
cin>>n>>k;
cin>>m;
for(int i=1;i<=m;i++)
{
cin>>l>>r;
if(l>r)
{
s2=max(l-k,r);
v[l].pb({r,1});
v[s2].pb({r,-1});
//cout<<s2,down
}
else
{
s2=min(l+k,r);
v2[l].pb({r,1});
v2[s2].pb({r,-1});
}
}
set<pair<int,int>>s;
for(int i=1;i<=n;i++)
{
for(auto x:v2[i])
{
if(x.second==1)s.insert(x);
else s.erase({x.first,1});
}
seg[0].rf[i]=i;
if(!s.empty()) seg[0].rf[i]=s.rbegin()->first;
}
s.clear();
for(int i=n;i>=1;--i)
{
for(auto x:v[i])
{ ///cout<<x.first<<" "<<x.second,down
if(x.second==1)s.insert(x);
else {
s.erase({x.first,1});}
}
seg[0].lf[i]=i;
if(!s.empty()) seg[0].lf[i]=s.begin()->first;
}
seg[0].build(1,1,n);
for(int i=1;i<20;i++){
for(int j=1;j<=n;j++)
{
ib s22=seg[i-1].get(1,1,n,seg[i-1].lf[j],seg[i-1].rf[j]);
seg[i].lf[j]=s22.a;
seg[i].rf[j]=s22.b;
}
seg[i].build(1,1,n);
}
cin>>m;
while(m--)
{
cin>>l>>r;
s2=l;
s3=l;
s5=1;
for(int j=19;j>=0;--j)
{
ib ss4=seg[j].get(1,1,n,s2,s3);
if(ss4.a>r||ss4.b<r)
{
s2=ss4.a;
s3=ss4.b;
s5+=(1<<j);
///cout<<ss4.a<<" "<<ss4.b<<" "<<r<<"cucu ",down
}
}
ib ss4=seg[0].get(1,1,n,s2,s3);
if(ss4.a>r||ss4.b<r)
{
cout<<-1;
}
else cout<<s5;
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 member function 'void IT::build(int, int, int)':
Main.cpp:65:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
65 | int mid=l+r>>1;
| ~^~
Main.cpp: In member function 'ib IT::get(int, int, int, int, int)':
Main.cpp:75:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
75 | 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... |