poj2482--Stars in Your Window(扫描线)

题目链接:点击打开链接

链接题目大意:给出n个星星的坐标,每个星星有一个亮度,给出一个矩形的长和宽,问矩形能包括的星星的最大亮度和(不包括边框)。

假设每个星星都是矩形的最左下点,那么每一个星星都可以得到一个矩形,(x,y)->(x,y,x+w,y+h),这个矩形的两条高边的值也就是星星的亮度k和-k,对于不同的矩形来说,如果高线出现重合部分,那么也就是说这两个星是可以出现在同一个矩形中的,扫描线求出可能出现的最大的亮度和。

注意:因为不包括边框,所以扫描时应先扫描值为负的高线,并且仅对值为正的高线统计最大值。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define LL __int64
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2,r,rt<<1|1
#define root 1,num_y,1
#define int_rt int l,int r,int rt
struct node{
    LL x , y1 , y2 ;
    LL k ;
}p[30000];
LL y[30000] ;
int cnt , num_y ;
struct node1{
    LL max1 , l , r , lazy ;
}cl[200000];
int cmp(node a,node b) {
    return a.x < b.x || ( a.x == b.x && a.k < 0 ) ;
}
void push_up(int rt) {
    cl[rt].max1 = max(cl[rt<<1].max1,cl[rt<<1|1].max1) + cl[rt].lazy;
}
void create(int_rt) {
    cl[rt].max1 = cl[rt].lazy = 0 ;
    cl[rt].l = y[l] , cl[rt].r = y[r] ;
    if( r - l == 1 ) {
        return ;
    }
    create(lson) ;
    create(rson) ;
    push_up(rt) ;
}
void update(LL ll,LL rr,LL k,int rt) {
    if( cl[rt].l >= ll && cl[rt].r <= rr ) {
        cl[rt].lazy += k ;
        cl[rt].max1 += k ;
        return ;
    }
    if( ll < cl[rt<<1].r )
        update(ll,min(rr,cl[rt<<1].r),k,rt<<1) ;
    if( rr > cl[rt<<1|1].l )
        update(max(cl[rt<<1|1].l,ll),rr,k,rt<<1|1) ;
    push_up(rt) ;
}
int main() {
    LL n , w , h , xx , yy , k , max1 ;
    int i , j ;
    while( scanf("%I64d %I64d %I64d", &n, &w, &h) != EOF ) {
        cnt = 0 , num_y = 1 ;
        max1 = 0 ;
        for(i = 1 ; i <= n ; i++) {
            scanf("%I64d %I64d %I64d", &xx, &yy, &k) ;
            p[cnt].x = xx ; p[cnt].y1 = yy ; p[cnt].y2 = yy+h ;
            p[cnt++].k = k ;
            p[cnt].x = xx+w ; p[cnt].y1 = yy ; p[cnt].y2 = yy+h ;
            p[cnt++].k = -k ;
            y[num_y++] = yy ;
            y[num_y++] = yy+h ;
        }
        sort(y+1,y+num_y) ;
        num_y = unique(y+1,y+num_y) - (y+1) ;
        sort(p,p+cnt,cmp) ;
        create(root) ;
        for(i = 0 ; i < cnt ; i++) {
            update(p[i].y1,p[i].y2,p[i].k,1) ;
            if( p[i].k  > 0 )
                max1 = max(max1,cl[1].max1) ;
        }
        printf("%I64d\n", max1) ;
    }
    return 0 ;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值