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

来源:互联网 发布:手机淘宝怎么开通直播 编辑:程序博客网 时间:2024/05/05 15:15

题目链接:点击打开链接

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


0 0
原创粉丝点击