HDU_1828 Picture 线段树

http://acm.hdu.edu.cn/showproblem.php?pid=1828

题意 :

给你N个矩形,求其轮廓线的周长。

思路:

线段树。这个题目其实和求矩形并面积是类似的,但是两者有一个地方不同。求矩形的面积的时候, 我们将每个矩形投影到X轴上,用扫描线的方法求出每个超元线段区间内,X轴方向上的有效长度,然后 ( p[i+1].h - p[i].h ) * sum[1] 就是该超元线段区间内的有效面积, 其中的sum[1]表示的是整个区间内的有效线段的的长度。这样我们就很容易会想到求矩形周长的方法了,类似地我们可以将矩形都投影到X 轴上,其中周长分成X方向上的周长和Y方法上的周长,我们先求X方向上的周长,X方向上的周长就是sum[1]  - last, 其中last是上次求出的sum[1]的值,这个值就是在该超元线段区间内X方向上的线段长度。 这样做的正确性,我们是可想而知的。 这样X方向上的线段长度就已经求出来了,接下去就是求Y方向上的线段的长度,这里我们可以在线段树的结点内增加一个信息,就是该区间包含的线段的条数用num[] 数组来记录 ,这样最后Y方向上的线段长度就是num[1] * ( p[i+1].h - p[i].h ) , 这个也是可以通过直观的想象就可以看出来的。到这里本题就可以解决了。本题中线段树中的结点并不是坐标抽上的点,而是坐标轴上的一个区间, 这是和一般的线段树不一样的地方。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
using namespace std ;
#define LL(a) ( (a)<<1 )
#define RR(a) ( (a)<<1|1 )
#define MIN(a , b) ( (a) > (b) ? (b) : (a) )
#define MAX(a , b) ( (a) > (b) ? (a) : (b) )

const int MAXN = 20010 ;
int N ;
struct Seg{
    int l , r , h ,  s;
    Seg(){}
    Seg(int a, int b ,int c , int d)
    :l(a) , r(b) , h(c) , s(d) {}
    bool operator < ( const Seg& cmp ) const {
        return h < cmp.h ;
    }
}p[MAXN] ;

int col[MAXN<<2] ;
int set[MAXN<<2] ;
int sum[MAXN<<2] ;
int num[MAXN<<2] ;
bool cl[MAXN<<2] ;
bool cr[MAXN<<2] ;


void build(int l ,int r , int idx){
    col[idx] = set[idx] = sum[idx] = num[idx] = 0 ;
    cl[idx] = cr[idx] = 0 ;
    if(l == r)  return ;
    int mid = (l + r) >> 1 ;
    build(l , mid, LL(idx) ) ;
    build(mid+1,r , RR(idx) ) ;
}

void down(int l ,int r, int idx){
    int mid = (l + r) >> 1 ;

    if( set[idx] != 0 ){
        set[ LL(idx) ] += set[idx] ;
        set[ RR(idx) ] += set[idx] ;
        col[ LL(idx) ] += set[idx] ;
        col[ RR(idx) ] += set[idx] ;

        if( col[ LL(idx) ] > 0 ){
            sum[ LL(idx) ] = mid - l + 1 ;
            num[ LL(idx) ] = 1 ;
            cl[ LL(idx) ] = cr[ LL(idx) ] = 1 ;
        }
        else{
            sum[ LL(idx) ] = num[ LL(idx) ] = 0 ;
            cl[ LL(idx) ] = cr[ LL(idx) ] = 0 ;
        }

        if( col[ RR(idx) ] > 0 ){
            sum[ RR(idx) ] = r - mid ;
            num[ RR(idx) ] = 1 ;
            cl[ RR(idx) ] = cr[ RR(idx) ] = 1 ;
        }
        else{
            sum[ RR(idx) ] = num[ RR(idx) ] = 0 ;
            cl[ RR(idx) ] = cr[ RR(idx) ] = 0 ;
        }
        set[idx] = 0 ;
    }
}

void up(int l, int r, int idx ){
    int ls = LL(idx) , rs = RR(idx) ;
    if( col[ls] == col[rs] ) col[idx] = col[ls] ;
    else    col[idx] = -1 ;

    cl[idx] = cl[ls] ;
    cr[idx] = cr[rs] ;

    sum[idx] = sum[ ls ] + sum[ rs ] ;
    num[idx] = num[ls] + num[rs] ;
    if( cr[ls] && cl[rs] )  num[idx] -- ;
}

void update(int l ,int r, int idx,  int a, int b, int v){
    if(l==a && b==r && col[idx] != -1 ){
        col[idx] += v ;
        set[idx] += v ;
        if( col[idx] > 0 ){
            num[idx] = 1 ; sum[idx] = r-l+ 1 ;
            cl[idx] = cr[idx] = 1 ;
        }
        else{
            num[idx] = sum[idx] = 0 ;
            cl[idx] = cr[idx] = 0;
        }
        return ;
    }
    down(l ,r , idx) ;
    int mid = (l + r) >> 1 ;
    if( b<=mid )    update(l , mid , LL(idx) , a , b , v);
    else if( mid<a )    update(mid+1, r, RR(idx) , a, b, v );
    else{
        update(l,mid,LL(idx) ,a, mid, v ) ;
        update(mid+1, r, RR(idx) , mid+1, b , v );
    }
    up(l, r, idx );
}

int main(){
    int a, b, c, d ;
    while( scanf("%d",&N) == 1){
        int m = 0 ;
        int L ,R  ;
        L = 1000000 ; R = -1000000 ;
        for(int i=0 ; i<N ; i++){
            scanf("%d %d %d %d",&a,&b,&c,&d);
            p[m++] = Seg(a ,c , b , 1 );
            p[m++] = Seg(a, c , d , -1);
            L = MIN( L , a );
            R = MAX( R , c );
        }
        sort(p, p+m);
        int ans = 0 , last = 0 ;

        build(L,R-1,1) ;
        for(int i=0;i<m;i++){
            int s = p[i].l , e =p[i].r -1 ;
            if( s<=e )
                update(L, R, 1, s , e , p[i].s );
            ans += abs( sum[1] - last ) ;   //x方向
            last = sum[1] ;
            ans += num[1]*( p[i+1].h - p[i].h ) * 2 ;
        }
        printf("%d\n",ans);
    }
    return 0 ;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值