TLE 求卡常
查看原帖
TLE 求卡常
515129
TLEWA楼主2024/11/4 20:05
#include<bits/stdc++.h>

using namespace std;

const int N=300005,M=1000005,INF=1e9;

// SGT
struct Tre {
	int now,l,r;
	int max;
}tre[4*M];

#define mid (tre[now].l+tre[now].r>>1)
#define lson (now*2)
#define rson (now*2+1)

inline void pushup(int now) {
	if(tre[now].l==tre[now].r) return;
	tre[now].max=max(tre[lson].max,tre[rson].max); 
}

inline void build(int now,int l,int r) {
	tre[now].l=l,tre[now].r=r,tre[now].max=-INF*2;
	if(l==r) return;
	build(lson,l,mid);build(rson,mid+1,r);
}

inline void change(int now,int p,int k,bool flg) {
	if(tre[now].l>p || tre[now].r<p) return;
	if(tre[now].l==tre[now].r) {
		if(flg) tre[now].max=k;
		else tre[now].max=max(tre[now].max,k);
		return;
	}
	change(lson,p,k,flg);change(rson,p,k,flg);
	pushup(now);
}

inline int query(int now,int l,int r) {
	if(tre[now].l>r || tre[now].r<l) return -INF*2;
	if(tre[now].l>=l && tre[now].r<=r) return tre[now].max;
	return max(query(lson,l,r),query(rson,l,r));
} 

int n,m;
struct Node {
	int t,op,x,y;
}arr[2*N];

int ans[2*N];

inline void cdq(int l,int r) {
	int Mid=l+r>>1;
	if(l==r) return;
	cdq(l,Mid);cdq(Mid+1,r);
	sort(arr+l,arr+Mid+1,[&](Node a,Node b) {return a.x<b.x;});
	sort(arr+Mid+1,arr+r+1,[&](Node a,Node b) {return a.x<b.x;});
	
	int p=l;
	for(int i=Mid+1;i<=r;++i) {
		while(p<=Mid && arr[p].x<=arr[i].x) {
			if(arr[p].op==1) change(1,arr[p].y,arr[p].x+arr[p].y,0);
			++p;
		}
		if(arr[i].op==2) {
			ans[arr[i].t]=min(ans[arr[i].t],arr[i].x+arr[i].y-query(1,0,arr[i].y));
		}
	}
	for(int i=p-1;i>=l;--i) change(1,arr[i].y,-INF*2,1);
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	
	cin >> n >> m;
	
	int op,x,y;
	for(int i=1;i<=n;++i) {
		cin >> arr[i].x >> arr[i].y;
		arr[i].op=1,arr[i].t=i;
	}
	
	for(int i=n+1;i<=n+m;++i) {
		cin >> arr[i].op >> arr[i].x >> arr[i].y;
		arr[i].t=i;
	}
	
	memset(ans,63,sizeof(ans));
	build(1,0,M);
	
	for(auto deltax:{0,1}) {
		for(auto deltay:{0,1}) {
			for(int i=1;i<=n+m;++i) {
				if(deltax) arr[i].x=M-arr[i].x;
				if(deltay) arr[i].y=M-arr[i].y;
			}
			
			sort(arr+1,arr+1+n+m,[&](Node a,Node b) {return a.t<b.t;});
			cdq(1,n+m); 
			
			for(int i=1;i<=n+m;++i) {
				if(deltax) arr[i].x=M-arr[i].x;
				if(deltay) arr[i].y=M-arr[i].y;
			}
		}
	}
	
	for(int i=n+1;i<=n+m;++i)
		if(ans[i]<INF) cout << ans[i] << '\n';
	
	return 0;
}
2024/11/4 20:05
加载中...