码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2015-07-11 18:34:02      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:点击打开链接

链接题目大意:给出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 ;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:

原文地址:http://blog.csdn.net/winddreams/article/details/46842845

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!