(中等) HDU 3265 Posters , 扫描线。

  Problem Description
  Ted has a new house with a huge window. In this big summer, Ted decides to decorate the window with some posters to prevent the glare outside. All things that Ted can find are rectangle posters. 

  However, Ted is such a picky guy that in every poster he finds something ugly. So before he pastes a poster on the window, he cuts a rectangular hole on that poster to remove the ugly part. Ted is also a careless guy so that some of the pasted posters may overlap when he pastes them on the window. 

  Ted wants to know the total area of the window covered by posters. Now it is your job to figure it out.

  To make your job easier, we assume that the window is a rectangle located in a rectangular coordinate system. The window’s bottom-left corner is at position (0, 0) and top-right corner is at position (50000, 50000). The edges of the window, the edges of the posters and the edges of the holes on the posters are all parallel with the coordinate axes. 
 
  题目就是求矩形的并的面积,典型的扫描线的题目,对于中间有个洞的矩形,可以拆分成四个来做。
  
  网上还有一种办法,就是对扫描线的线段树进行改进,在HDU这个题的Discuss里面有。
  
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

#define lc po*2
#define rc po*2+1
#define lson L,M,lc
#define rson M+1,R,rc

using namespace std;

struct BIAN
{
    int x,y1,y2;
    short state;
};

const int maxn=50005;

BIAN bian[50005*8];
int BIT[maxn*4];
int COL[maxn*4];

void pushUP(int L,int R,int po)
{
    if(COL[po])
        BIT[po]=R+1-L;
    else if(L==R)
        BIT[po]=0;
    else
        BIT[po]=BIT[lc]+BIT[rc];
}

void update(int ul,int ur,int ut,int L,int R,int po)
{
    if(ul<=L&&ur>=R)
    {
        COL[po]+=ut;
        pushUP(L,R,po);

        return;
    }

    int M=(L+R)/2;

    if(ul<=M)
        update(ul,ur,ut,lson);
    if(ur>M)
        update(ul,ur,ut,rson);

    pushUP(L,R,po);
}

bool cmp(BIAN a,BIAN b)
{
    return a.x<b.x;
}

int main()
{
    int N;
    long long ans;
    int x1,x2,x3,x4,y1,y2,y3,y4;

    while(~scanf("%d",&N))
    {
        if(!N)
            break;

        memset(COL,0,sizeof(COL));
        memset(BIT,0,sizeof(BIT));
        ans=0;

        for(int i=1;i<=N;++i)
        {
            scanf("%d %d %d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);

            bian[i*8-5].x=x1;
            bian[i*8-5].y1=y1;
            bian[i*8-5].y2=y2;
            bian[i*8-5].state=1;
            
            bian[i*8-4].x=x3;
            bian[i*8-4].y1=y1;
            bian[i*8-4].y2=y2;
            bian[i*8-4].state=-1;

            bian[i*8-3].x=x4;
            bian[i*8-3].y1=y1;
            bian[i*8-3].y2=y2;
            bian[i*8-3].state=1;

            bian[i*8-2].x=x2;
            bian[i*8-2].y1=y1;
            bian[i*8-2].y2=y2;
            bian[i*8-2].state=-1;

            bian[i*8-1].x=x3;
            bian[i*8-1].y1=y4;
            bian[i*8-1].y2=y2;
            bian[i*8-1].state=1;

            bian[i*8].x=x4;
            bian[i*8].y1=y4;
            bian[i*8].y2=y2;
            bian[i*8].state=-1;

            bian[i*8-7].x=x3;
            bian[i*8-7].y1=y1;
            bian[i*8-7].y2=y3;
            bian[i*8-7].state=1;

            bian[i*8-6].x=x4;
            bian[i*8-6].y1=y1;
            bian[i*8-6].y2=y3;
            bian[i*8-6].state=-1;
        }

        sort(bian+1,bian+8*N+1,cmp);

        for(int i=1;i<=8*N;++i)
        {
            ans+=(long long)BIT[1]*(bian[i].x-bian[i-1].x);
            
            if(bian[i].y2>bian[i].y1)
                update(bian[i].y1,bian[i].y2-1,bian[i].state,0,50000,1);
        }

        cout<<ans<<endl;
    }

    return 0;
}
View Code

 

  下面是学习了新的方法后做的,就是对线段树进行修改,用上pushDown,保证线段树的每一个节点的值都不会小于0。

  这样直接分成两个矩形,中间那个的左边的state为-1,右边的为1.

  PS:这个题在杭电上有讨论long long会wa但是unsigned int就可以的问题,其实是因为没有加强制转换导致的,long long可以ac。

 

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

#define lc po*2
#define rc po*2+1
#define lson L,M,lc
#define rson M+1,R,rc
#define ji1 i*4-3
#define ji2 i*4-2
#define ji3 i*4-1
#define ji4 i*4

using namespace std;

struct BIAN
{
    int x,y1,y2;
    short state;
};

const int maxn=50015;

BIAN bian[maxn*4];
long long BIT[maxn*4];
int COL[maxn*4];

void callUP(int L,int R,int po)
{
    if(COL[po]>=1)
        BIT[po]=R+1-L;
    else if(L==R)
        BIT[po]=0;
    else
        BIT[po]=BIT[lc]+BIT[rc];
}

void pushUP(int L,int R,int po)
{
    if(L!=R)                        //如果不判断会越界。
    {
        int temp=min(COL[lc],COL[rc]);

        COL[po]+=temp;
        COL[lc]-=temp;
        COL[rc]-=temp;

        callUP(L,(L+R)/2,lc);
        callUP((L+R)/2+1,R,rc);
    }

    callUP(L,R,po);
}

void pushDown(int L,int R,int po)
{
    if(COL[po])
    {
        COL[lc]+=COL[po];
        COL[rc]+=COL[po];

        callUP(L,(L+R)/2,lc);
        callUP((L+R)/2+1,R,rc);

        COL[po]=0;
        callUP(L,R,po);
    }
}

void update(int ul,int ur,int ut,int L,int R,int po)
{
    if(ul<=L&&ur>=R)
    {
        COL[po]+=ut;
        pushUP(L,R,po);

        return;
    }

    pushDown(L,R,po);

    int M=(L+R)/2;

    if(ul<=M)
        update(ul,ur,ut,lson);
    if(ur>M)
        update(ul,ur,ut,rson);

    pushUP(L,R,po);

}

bool cmp(BIAN a,BIAN b)
{
    return a.x<b.x;
}

int main()
{
    int N;
    int cas=1;
    long long ans;
    int x1,x2,y1,y2,x3,x4,y3,y4;

    ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(2);

    for(cin>>N;N;cin>>N)
    {
        memset(BIT,0,sizeof(BIT));
        memset(COL,0,sizeof(COL));

        for(int i=1;i<=N;++i)
        {
            cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;

            bian[ji1].state=1;
            bian[ji2].state=-1;
            bian[ji3].state=-1;
            bian[ji4].state=1;

            bian[ji1].x=x1;
            bian[ji2].x=x2;
            bian[ji3].x=x3;
            bian[ji4].x=x4;

            bian[ji1].y1=bian[ji2].y1=y1;
            bian[ji1].y2=bian[ji2].y2=y2;
            bian[ji3].y1=bian[ji4].y1=y3;
            bian[ji3].y2=bian[ji4].y2=y4;
        }

        sort(bian+1,bian+4*N+1,cmp);

        ans=0;

        for(int i=1;i<=4*N;++i)
        {
            ans+=(long long)BIT[1]*(bian[i].x-bian[i-1].x);            //要加强制转换。

            update(bian[i].y1,bian[i].y2-1,bian[i].state,0,49999,1);
        }
        
        cout<<ans<<endl;
    }

    return 0;
}
View Code

 

posted @ 2015-01-11 21:31  WhyWhy。  阅读(...)  评论(...编辑  收藏