hdu 1828 Picture 切割线求周长

Picture

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2144    Accepted Submission(s): 1139


Problem Description
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.

Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.



The corresponding boundary is the whole set of line segments drawn in Figure 2.



The vertices of all rectangles have integer coordinates.
 

 

Input
Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.

0 <= number of rectangles < 5000
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.

Please process to the end of file.
 

 

Output
Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.
 

 

Sample Input
 
7
-15 0 5 10
-5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
30 10 36 20
34 0 40 16
 
Sample Output
228
 
/*
统计周长
*/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define N 5003
#define hxl 10003
using namespace std;


struct node
{
    int begin;
    int end;
    int val;
    int flag;
}Linex[N<<1],Liney[N<<1];
int Hash[30000];


bool cmp(node n1,node n2)
{
    return n1.val<n2.val;
}


int make_x(int NN)
{
    int sum=0,i,j;
    for(i=1;i<=NN;i++)
    {
        if(Linex[i].flag==1)
        {
            for(j=Linex[i].begin;j<Linex[i].end;j++)
            {
                if(Hash[j]==0)
                sum++;
                Hash[j]++;
            }
        }
        else
        {
            for(j=Linex[i].begin;j<Linex[i].end;j++)
            {
                if(Hash[j]==1)
                sum++;
                Hash[j]--;
            }

        }
    }
    return sum;
}

int make_y(int NN)
{
    int sum=0,i,j;
    for(i=1;i<=NN;i++)
    {
        if(Liney[i].flag==1)
        {
            for(j=Liney[i].begin;j<Liney[i].end;j++)
            {
                if(Hash[j]==0)
                sum++;
                Hash[j]++;
            }
        }
        else
        {
            for(j=Liney[i].begin;j<Liney[i].end;j++)
            {
                if(Hash[j]==1)
                sum++;
                Hash[j]--;
            }
        }
    }
    return sum;
}

void make_ini(int NN)
{
    int x1,y1,x2,y2,i,len=0,sum=0;
    for(i=1;i<=NN;i++)
    {
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        x1+=hxl;y1+=hxl;y2+=hxl;x2+=hxl;

        Linex[++len].val=x1;
        Linex[len].begin=y1;
        Linex[len].end=y2;
        Linex[len].flag=1;

        Liney[len].val=y1;
        Liney[len].begin=x1;
        Liney[len].end=x2;
        Liney[len].flag=1;

        Linex[++len].val=x2;
        Linex[len].begin=y1;
        Linex[len].end=y2;
        Linex[len].flag=-1;

        Liney[len].val=y2;
        Liney[len].begin=x1;
        Liney[len].end=x2;
        Liney[len].flag=-1;
    }
    sort(Linex+1,Linex+1+len,cmp);
    sort(Liney+1,Liney+1+len,cmp);
    memset(Hash,0,sizeof(Hash));
    sum+=make_x(len);
    memset(Hash,0,sizeof(Hash));
    sum+=make_y(len);
    printf("%d\n",sum);
}

int main()
{
    int NN;
    while(scanf("%d",&NN)>0)
    {
        make_ini(NN);
    }
    return 0;
}

 

posted @ 2013-08-03 10:44  芷水  阅读(219)  评论(0编辑  收藏  举报