WA#9 扫描线细节求调
查看原帖
WA#9 扫描线细节求调
324350
xiaomuyun楼主2022/2/3 11:18

rt

Link

#include<algorithm>
#include<cstring>
#include<cstdio>
#define int long long
using namespace std;
const int maxn=1e4+1;
const int maxn2=2e4+1;
int t,n,w,h,p[8*maxn],sum[8*maxn],tot=0,len,lazy[8*maxn],res=0;
struct segment{
	int y,x1,x2,io,v;
}l[maxn2];
inline bool cmp(const segment &a,const segment &b){
	if(a.y!=b.y) return a.y<b.y;
	return a.v>b.v;
}
inline int Discrete(){
	sort(p+1,p+1+tot);
	return unique(p+1,p+1+tot)-(p+1);
}
inline void downtag(int o,int l,int r){
	sum[o]+=lazy[o];
	if(l!=r) lazy[o*2]+=lazy[o],lazy[o*2+1]+=lazy[o];
	lazy[o]=0;
	return ;
}
inline void initialize(){
	memset(lazy,0,sizeof lazy);
	memset(sum,0,sizeof sum);
	tot=0;
	return ;
}
inline void update(int o,int l,int r,int x,int y,int io,int val){
	if(x<=l&&r<=y){
		lazy[o]+=io*val;
		return ;
	}
	int mid=(l+r)>>1;
	if(x<=mid) update(o*2,l,mid,x,y,io,val);
	if(y>mid) update(o*2+1,mid+1,r,x,y,io,val);
	downtag(o*2,l,mid);
	downtag(o*2+1,mid+1,r);
	sum[o]=max(sum[o*2],sum[o*2+1]);
	return ;
}
signed main(){
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld%lld",&n,&w,&h);
		initialize();
		for(int i=1,x,y,val;i<=n;++i){
			scanf("%lld%lld%lld",&x,&y,&val);
			l[++tot]=(segment){y,x,x+w-1,1,val};
			l[++tot]=(segment){y+h-1,x,x+w-1,-1,val};
			p[tot-1]=x,p[tot]=x+w-1;
		}
		sort(l+1,l+1+tot,cmp);
		len=Discrete(),res=0;
		for(int i=1;i<=tot;++i){
			int ll=lower_bound(p+1,p+1+len,l[i].x1)-p;
			int r=lower_bound(p+1,p+1+len,l[i].x2)-p;
			update(1,1,len-1,ll,r,l[i].io,l[i].v);
			res=max(sum[1],res);
		}
		printf("%lld\n",res);
	}
	return 0;
}
2022/2/3 11:18
加载中...