传送门:poj2482-Stars in Your Window
Description
Assume the sky is a flat plane. All the stars lie on it with a location (x, y). for each star, there is a grade ranging from 1 to 100, representing its brightness, where 100 is the brightest and 1 is the weakest. The window is a rectangle whose edges are parallel to the x-axis or y-axis. Your task is to tell where I should put the window in order to maximize the sum of the brightness of the stars within the window. Note, the stars which are right on the edge of the window does not count. The window can be translated but rotation is not allowed.
Input
There are several test cases in the input. The first line of each case contains 3 integers: n, W, H, indicating the number of stars, the horizontal length and the vertical height of the rectangle-shaped window. Then n lines follow, with 3 integers each: x, y, c, telling the location (x, y) and the brightness of each star. No two stars are on the same point.
There are at least 1 and at most 10000 stars in the sky. 1<=W,H<=1000000, 0<=x,y<2^31.
Output
For each test case, output the maximum brightness in a single line.
Solution
一眼线段树。然而事实即证明了,本蒟蒻的扫描线知识算是白学了。
那么分析一下,我们可以按y升序排序,然后离散化以x建立线段树。
具体来说,由于y为升序,对于第i颗星星,我们假设它的光照有效y值区间为(yi,yi+h-1) (-1是因为在窗边上无效)扫描过去的时候,在扫到yi时加上它的brightness,扫到yi+h时减去它的brightness。
但这里对于x值也有w的范围限制,这里的考虑很巧妙:同样的,我们假设第i颗星星的光照有效x值区间为(xi,xi+w-1)。我们加入第i颗星星时,先在对应的区间加上这颗星星的亮度值。而求最大亮度值,其实可以转化为在某个相交区间内的点的最大值。所以我们在pushup的时候,在k结点上,它本身被覆盖的值加上它左右子结点的最大值,就是在k上的最大值。(可以参照代码自己再理解一下)
然而以上都不是本蒟蒻自己想出来的。
本蒟蒻打了个非常丑的暴力。TLE。
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int N=5e4+10;
ll cnt[N<<2];
ll sum[N<<2];
ll X[N],ans,n,w,h,m;
struct Seg
{
ll h,l,r;
ll s;
Seg(){}
Seg(ll a,ll b,ll c,ll d) : l(a) , r(b) , h(c) , s(d) {}
bool operator < (const Seg &cmp) const
{
if( h==cmp.h)return s<cmp.s;
return h < cmp.h;
}
}ss[N];
inline void pushup(int k)
{
sum[k]=max(sum[k<<1],sum[k<<1|1])+cnt[k];// 使权值最大
}
inline void update(ll L,ll R,ll c,ll l,ll r,ll rt)
{
if (L<=l && r<=R)
{
cnt[rt]+=c;
sum[rt]+=c;
return;
}
ll m =(l+r)>>1;
if(L<=m)
update(L,R,c,lson);
if(m<R)
update(L,R,c,rson);
pushup(rt);
}
int main()
{
scanf("%lld%lld%lld",&n,&w,&h);
while(n--){
ll a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
X[m]=a;
ss[m++]=Seg(a,a+w,b,c);
X[m]=a+w;
ss[m++]=Seg(a,a+w,b+h,-c);
}
sort(X,X+m);
sort(ss,ss+m);
int k=unique(X,X+m)-X;
for (int i=0;i<m-1;i++)
{
ll l=lower_bound(X,X+k,ss[i].l)-X;
ll r =lower_bound(X,X+k,ss[i].r)-X-1;//-1 ->边缘上不算
if (l<=r) update(l,r,ss[i].s,0,k-1,1);
ans= max(ans,sum[1]);
}
printf("%lld\n",ans);
return 0;
}
附上TLE暴力(请自行忽略)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cstdlib>
using namespace std;
typedef long long ll;
const int N=1e6+10;
int n,W,H,tot,dl=1;
ll sum[N<<1],ty[N],ans=0;
struct P{
ll x,y,p;
int tag;
}star[N];
struct Q{
ll val;
int num;
}t[N];
inline bool cmp(P a,P b)
{
return a.y<b.y;
}
inline bool cmpb(Q a,Q b)
{
return a.val<b.val;
}
inline void update(int k)
{
sum[k]=sum[k<<1]+sum[k<<1|1];
}
inline ll max(ll a,ll b)
{
return a>b? a:b;
}
inline void del(int k,int l,int r,int fin,ll dd)
{
if(l==r){
sum[k]-=dd;
return;
}
int mid=(l+r)>>1;
if(fin<=mid) del(k<<1,l,mid,fin,dd);
else del(k<<1|1,mid+1,r,fin,dd);
update(k);
}
inline void add(int k,int l,int r,int fin,ll dd){
if(l==r){
sum[k]+=dd;
return;
}
int mid=(l+r)>>1;
if(fin<=mid) add(k<<1,l,mid,fin,dd);
else add(k<<1|1,mid+1,r,fin,dd);
update(k);
}
inline ll getsum(int k,int l,int r,int L,int R)
{
if(l>=L && r<=R){
return sum[k];
}
int mid=(l+r)>>1;ll ret=0;
if(L<=mid) ret+=getsum(k<<1,l,mid,L,R);
if(R>mid) ret+=getsum(k<<1|1,mid+1,r,L,R);
return ret;
}
inline void solve()
{
ll now=0;int l=1,r;
for(int i=1;i<=tot;i++){
if(ty[l]+W<=ty[i]){
r=l;
while(ty[r+1]+W<=ty[i]) r++;
now-=getsum(1,1,tot,l,r);
l=r+1;
}
r=i;
while(ty[l]+W>ty[i+1] && i<tot)
i++;
now+=getsum(1,1,tot,r,i);
ans=max(ans,now);
}
}
int main(){
scanf("%d%d%d",&n,&W,&H);
for(int i=1;i<=n;i++){
scanf("%lld%lld%lld",&star[i].x,&star[i].y,&star[i].p);
}
sort(star+1,star+1+n,cmp);
for(int i=1;i<=n;i++){
t[i].val=star[i].x;t[i].num=i;
}
sort(t+1,t+n+1,cmpb);
for(int i=1;i<=n;i++){
if(t[i].val!=t[i-1].val) {
ty[++tot]=t[i].val;
}
star[t[i].num].tag=tot;
}
for(int i=1;i<=n;i++){
while(star[dl].y+H<=star[i].y){
del(1,1,tot,star[dl].tag,star[dl].p);
dl++;
}
add(1,1,tot,star[i].tag,star[i].p);
solve();
}
printf("%lld\n",ans);
return 0;
}