数据结构中的判定转状态+扫描线:P1502

https://www.luogu.com.cn/problem/P1502

发现正常扫描线很难维护恰好大小为 W W W 的区间

反过来,对于每个星星维护合法的左下角下标

把原先的判定转成了和点有关的状态,把点变成矩形后求并即可

#include 
#include
#include 
using namespace std;
#define int long long
inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'||
ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
//mt19937 rand(time(0));
//mt19937_64 rand(time(0));
//srand(time(0));
#define N 20010
//#define M
//#define mo
struct node {
	int l, r, x, w; 
}a[N];
int n, m, i, j, k, T;
int ans, sum, l, r, x, y, w, Lx, Rx; 
int W, H, rt, b[N]; 

bool cmp(node x, node y) {
	if(x.x==y.x) return x.w<y.w; 
	return x.x<y.x; 
}

struct Segment_tree {
	int tot, ls[N<<2], rs[N<<2]; 
	int mx[N<<2], tag[N<<2]; 
	void build(int &k, int l, int r) {
		if(!k) k=++tot, mx[k]=tag[k]=ls[k]=rs[k]=0; 
		if(l==r) return ; 
		int mid=(l+r)>>1; 
		build(ls[k], l, mid); 
		build(rs[k], mid+1, r); 
	}
	void push_down(int k) {
		tag[ls[k]]+=tag[k]; mx[ls[k]]+=tag[k]; 
		tag[rs[k]]+=tag[k]; mx[rs[k]]+=tag[k]; 
		tag[k]=0; 
	}
	void add(int k, int l, int r, int x, int y, int z) {
		if(l>=x && r<=y) return mx[k]+=z, tag[k]+=z, void(); 
		int mid=(l+r)>>1; 
		push_down(k); 
		if(x<=mid) add(ls[k], l, mid, x, y, z); 
		if(y>=mid+1) add(rs[k], mid+1, r, x, y, z); 
		mx[k]=max(mx[ls[k]], mx[rs[k]]); 
	}
}Seg;

signed main()
{
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
	T=read(); 
	while(T--) {
//	while(~scanf("%lld%lld%lld", &n, &W, &H)) {
		n=read(); W=read(); H=read(); 
	
		ans=k=j=0; 
		for(i=1; i<=n; ++i) {
			x=read(); y=read(); w=read();
			Lx=x; Rx=x+W-1; //[Lx, Rx]
			b[++k]=Lx; b[++k]=Rx; 
			a[++j].l=Lx; a[j].r=Rx; a[j].x=y; a[j].w=w; 
			a[++j].l=Lx; a[j].r=Rx; a[j].x=y+H; a[j].w=-w; 
		}
		sort(b+1, b+2*n+1); 
		for(i=1; i<=2*n; ++i) a[i].l=lower_bound(b+1, b+2*n+1, a[i].l)-b; 
		for(i=1; i<=2*n; ++i) a[i].r=lower_bound(b+1, b+2*n+1, a[i].r)-b; 
		Seg.tot=rt=ans=0; Seg.build(rt, 1, 2*n); 
		
		sort(a+1, a+2*n+1, cmp); 
		
		for(i=1; i<=2*n; ++i) {
			Seg.add(1, 1, 2*n, a[i].l, a[i].r, a[i].w); 
			ans=max(ans, Seg.mx[1]); 
		}
		printf("%lld\n", ans); 
	}
	return 0;
}


你可能感兴趣的:(数据结构,线段树,扫描线)