线段树14pts求调
查看原帖
线段树14pts求调
1423269
ini_____楼主2025/1/19 16:18

rt,写了三个小时了qwq

样例都能过,但是交上去大部分都Wa了,小部分Re

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+5,inf=1e18,maxr=1e18;
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 t>tmp.type;
	}
	void init(int _t,int _id,int _type,int _x){
		t=_t,id=_id,type=_type,x=_x;
	}
}e[N*3];
struct Node{
	int Min,val;
}tr[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;
}
void update(int l,int r,int index,int pos,int val){
	if(l==r){
		tr[index].val=tr[index].Min=val;
		return;
	}
	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);
	tr[index].Min=min(tr[index*2].Min,tr[index*2+1].Min);
}	
int query(int l,int r,int x,int y,int index){
	if(x<=l and r<=y)return tr[index].Min;
	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;
}	
bool check(int x,int mid){
	int L=max((long long)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(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(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(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(int i=1;i<=n;i++)update(1,n+k,1,i,inf);
	for(int i=n+1;i<=n+k;i++)pre[i]=0,update(1,n+k,1,i,0);
	for(int i=1;i<=k;i++)st[i].insert(0),st[i].insert(inf+i);
	for(int i=1;i<=all;i++){
		if(e[i].type==-1){
			int L=0,R=maxr;
			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!=maxr)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].erase(x);
		}
	}
	for(int i=1;i<=q;i++)cout<<ans[i]<<endl;
}
2025/1/19 16:18
加载中...