题目链接:https://www.luogu.org/problem/P1502

扫描线的板子题,把每个点看成矩形,存下边(x,y,y+h-1,li)和(x+w-1,y,y+h-1),在按横坐标扫线段更新y区间,线段树维护区间最大值

#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
#define maxn 20005
struct seg{
ll x,l,r,s,val;
seg(){}
seg(ll x,ll l,ll r,ll s,ll val):x(x),l(l),r(r),s(s),val(val){};
bool operator <(const seg &w)const{
if(x==w.x)return s>w.s;
return x<w.x;
}
}se[maxn<<];
struct node{
ll x,y,val,x1,y1;
}a[maxn<<];
ll n,w,h,ly[maxn<<],ny;
ll sum[maxn<<],lazy[maxn<<];
void pushup(int rt)
{
sum[rt]=max(sum[rt<<],sum[rt<<|]);
}
void build(int l,int r,int rt)
{
sum[rt]=lazy[rt]=;
if(l==r)
{
return ;
}
int mid=l+r>>;
build(ls);build(rs);
pushup(rt);
}
void pushdown(int rt)
{
if(lazy[rt])
{
sum[rt<<]+=lazy[rt];
sum[rt<<|]+=lazy[rt];
lazy[rt<<]+=lazy[rt];
lazy[rt<<|]+=lazy[rt];
lazy[rt]=;
}
}
void update(int L,int R,int val,int l,int r,int rt)
{
if(L<=l&&R>=r)
{
sum[rt]+=val;
lazy[rt]+=val;
return ;
}
pushdown(rt);
int mid=l+r>>;
if(L<=mid)update(L,R,val,ls);
if(R>mid)update(L,R,val,rs);
pushup(rt);
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>w>>h;
int cnt=;
for(int i=;i<=n;i++)
{
cin>>a[i].x>>a[i].y>>a[i].val;
a[i].y1=a[i].y+h-;
ly[++cnt]=a[i].y;ly[++cnt]=a[i].y+h-;
se[cnt-]=seg(a[i].x,a[i].y,a[i].y1,,a[i].val);
se[cnt]=seg(a[i].x+w-,a[i].y,a[i].y1,-,a[i].val);
}
sort(ly+,ly++cnt);
sort(se+,se++cnt);
ny=unique(ly+,ly++cnt)-ly-;
build(,ny,);
int l,r;
ll ans=;
for(int i=;i<=cnt;i++)
{
l=lower_bound(ly+,ly++ny,se[i].l)-ly;
r=lower_bound(ly+,ly++ny,se[i].r)-ly;
if(se[i].s==)update(l,r,se[i].val,,ny,);
else update(l,r,-se[i].val,,ny,);
ans=max(ans,sum[]);
}
cout<<ans<<endl;
}
return ;
}
05-26 10:36