题目
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
#define lc o<<1
#define rc o<<1|1
const int N=2e4+10;
int n,w,h,tot;
struct node{
ll x1,x2,y,c;
bool operator <(const node&rhs)const{
return y<rhs.y||(y==rhs.y&&c<rhs.c);}
}star[N<<1];
ll hash[N<<1],tr[N<<2],lazy[N<<2];
int find(ll x,int l,int r)
{
while(l<r)
{
int mid=l+r>>1;
if(hash[mid]>=x)r=mid;
else l=mid+1;
}
return l;
}
void push_up(int o){tr[o]=max(tr[lc],tr[rc])+lazy[o];}
void update(int o,int l,int r,int nl,int nr,ll val)
{
if(nl<=l&&r<=nr)tr[o]+=val,lazy[o]+=val;
else
{
int mid=l+r>>1;
if(nl<=mid)update(lc,l,mid,nl,nr,val);
if(nr>mid)update(rc,mid+1,r,nl,nr,val);
push_up(o);
}
}
int main()
{
//freopen("in.txt","r",stdin);
ll x,y,c;
while(~scanf("%d%d%d",&n,&w,&h))
{
memset(tr,0,sizeof(tr));
memset(lazy,0,sizeof(lazy));
tot=0;
for(int i=1;i<=n;i++)
{
scanf("%lld%lld%lld",&x,&y,&c);
hash[++tot]=x;star[tot]=(node){x,x+w,y,c};
hash[++tot]=x+w;star[tot]=(node){x,x+w,y+h,-c};
}
sort(hash+1,hash+tot+1);
sort(star+1,star+tot+1);
ll ans=0;
for(int i=1;i<=tot;i++)
{
int l=find(star[i].x1,1,tot),r=find(star[i].x2,1,tot)-1;
if(l<=r)update(1,1,tot,l,r,star[i].c);
ans=max(ans,tr[1]);
}
printf("%lld\n",ans);
}
return 0;
}
总结
根据本题的题意,一个矩形可以由它的任意一个顶点唯一确定,我们可以考虑将矩形的右上角放到什么位置使得亮度和最大,而问题又可以转化为求在平面上哪个坐标矩形重叠区域的权值和最大,可以采用扫描线算法。
这题作为扫描线算法的例题,除了求面积并以外原来还可以这样玩。