rt,只有三个点WA了qwq
#include<bits/stdc++.h>
using namespace std;
const int N=6e5+5,inf=2e8,Maxans=1e8;
int n,k,q;
struct Shop{
int x,type,a,b;
bool operator<(const Shop tmp)const{
return x<tmp.x;
}
}s[N];
int X[N];
struct Event{
int t,id,type,x;
bool operator<(const Event tmp)const{
if(t!=tmp.t)return t<tmp.t;
return type>tmp.type;
}
void init(int _t,int _id,int _type,int _x){
t=_t,id=_id,type=_type,x=_x;
}
}e[N*3];
int Min[N*8];
int pre[N*2],ans[N];
multiset<int> st[N];
inline int find(int x){
if(!x)return x;
if(x>inf)return x-inf+n;
return lower_bound(X+1,X+1+n,x)-X;
}
inline void update(int l,int r,int index,int pos,int val){
if(l==r)return (void)(Min[index]=val);
int mid=(l+r)>>1;
if(mid >=pos)update(l ,mid,index*2 ,pos,val);
if(mid+1<=pos)update(mid+1,r,index*2+1,pos,val);
Min[index]=min(Min[index*2],Min[index*2+1]);
}
inline int query(int l,int r,int x,int y,int index){
if(x<=l and r<=y)return Min[index];
int mid=(l+r)>>1,res=inf;
if(mid >=x)res=min(res,query(l,mid,x,y,index*2));
if(mid+1<=y)res=min(res,query(mid+1,r,x,y,index*2+1));
return res;
}
inline bool check(int x,int mid){
int L=max(1,x-mid),R=upper_bound(X+1,X+1+n,x+mid)-X;
return (query(1,n+k,R,n+k,1)>=L);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
memset(pre,0x3f,sizeof pre);
cin>>n>>k>>q;
for(register int i=1;i<=n;i++)cin>>s[i].x>>s[i].type>>s[i].a>>s[i].b;
sort(s+1,s+1+n);
for(register int i=1;i<=n;i++){
X[i]=s[i].x;
e[i*2-1].init(s[i].a,i,1,0);
e[i*2 ].init(s[i].b+1,i,0,0);
}
for(register int i=1;i<=q;i++){
int l,y;cin>>l>>y;
e[n*2+i].init(y,i,-1,l);
}
int all=q+2*n;
sort(e+1,e+1+all);
for(register int i=1;i<=n;i++)update(1,n+k,1,i,inf);
for(register int i=n+1;i<=n+k;i++)pre[i]=0,update(1,n+k,1,i,0);
for(register int i=1;i<=k;i++)st[i].insert(0),st[i].insert(inf+i);
for(register int i=1;i<=all;i++){
//cout<<e[i].type<<" "<<e[i].id<<" "<<e[i].t<<" "<<e[i].x<<endl;
if(e[i].type==-1){
int L=0,R=Maxans;
if(query(1,n+k,n+1,n+k,1)<1)L=R;
while(L<R){
int Mid=(L+R)>>1;
if(check(e[i].x,Mid))R=Mid;
else L=Mid+1;
}
ans[e[i].id]=-1;
if(L!=Maxans)ans[e[i].id]=L;
}
else{
int type=s[e[i].id].type,x=s[e[i].id].x;
int Sub=inf,Pre=inf,Now=find(x);
multiset<int>::iterator it_1=st[type].upper_bound(x);
if(it_1 !=st[type].end())Sub=*it_1;
multiset<int>::iterator it_2=st[type].lower_bound(x);
if(it_2 !=st[type].end())it_2--,Pre=*it_2;
Sub=find(Sub);
if(e[i].type)pre[Now]=Pre,update(1,n+k,1,Now,Pre),pre[Sub]=x,update(1,n+k,1,Sub,x);
else pre[Now]=inf,pre[Sub]=Pre,update(1,n+k,1,Now,inf),update(1,n+k,1,Sub,Pre);
if(e[i].type)st[type].insert(x);
else{
multiset<int>::iterator it_3=st[type].lower_bound(x);
if(it_3!=st[type].end())st[type].erase(it_3);
}
}
}
for(register int i=1;i<=q;i++)cout<<ans[i]<<endl;
}