【POJ】Stars in Your Window-线段树+扫描线

传送门: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 <<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值