POJ 1177 Picture(线段树+离散化 求周长)

Picture
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 11720   Accepted: 6181

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. 
POJ 1177 Picture(线段树+离散化 求周长)_第1张图片
The corresponding boundary is the whole set of line segments drawn in Figure 2. 
POJ 1177 Picture(线段树+离散化 求周长)_第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.

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

Source

IOI 1998

这题是很经典的线段树题目了。


抄一下别人的思路,按照这个实现的:
总体思路:

1.沿X轴离散化建树

2.按Y值从小到大排序平行与X轴的边,然后顺序处理

如果遇到矩形下面那条边则插入到线段树中,遇到矩形上面的边则将相应的边删除掉

根据线段树当前的状态统计长度



第二点是本题的核心思想,偶再举个例:

POJ 1177 Picture(线段树+离散化 求周长)_第3张图片


第一次求出的部分很好理解.

第二次求出的为什么会少了中间那部分.那是因为插入的新线段覆盖了第一条,此时线段树返回的长度是新的那一条的长度,将这个值再减去上次的就少了中间那部分

第三次因为是矩形的上边,所以要删除在那条长的线段.此时的线段树返回的则是第一次的长度,将此值减去第二次返回值,再取其负值就是红色X轴那部分了

最后那条X轴的,再补上就行了.


需要注意的就是离散化以后一定要去掉重复元素,否则会出错的。

/*
关于面积的关键是写calen函数!!!
*/
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAXN=10010;
struct Node
{
    int l,r;
    int cnt;//有效长度
    int lf,rf;//实际的左右端点
    int numseg;//分支数,一个分支对应两条竖线
    int c;//记录覆盖情况
    bool lcover,rcover;
}segTree[MAXN*4];
struct Line
{
    int y;
    int x1,x2;
    int c;
}line[MAXN];
bool cmp(Line a,Line b)
{
    return a.y<b.y;
}
int x[MAXN];
void Build(int i,int l,int r)
{
    segTree[i].l=l;
    segTree[i].r=r;
    segTree[i].lf=x[l];  ///离散化
    segTree[i].rf=x[r];  ///离散化
    segTree[i].cnt=0;
    segTree[i].numseg=0;
    segTree[i].c=0;
    segTree[i].lcover=segTree[i].rcover=false;

    if(l+1==r)return;
    int mid=(l+r)/2;
    Build(i<<1,l,mid);
    Build((i<<1)|1,mid,r);
}
void calen(int i)
{
    if(segTree[i].c>0)
    {
        segTree[i].cnt=segTree[i].rf-segTree[i].lf;
        segTree[i].numseg=1;
        segTree[i].lcover=segTree[i].rcover=true;
        return;
    }
    if(segTree[i].l+1==segTree[i].r)  ///叶子节点
    {
        segTree[i].cnt=0;
        segTree[i].numseg=0;
        segTree[i].lcover=segTree[i].rcover=false;
    }
    else
    {
        segTree[i].cnt=segTree[i<<1].cnt+segTree[(i<<1)|1].cnt;
        segTree[i].lcover=segTree[i<<1].lcover;
        segTree[i].rcover=segTree[(i<<1)|1].rcover;
        segTree[i].numseg=segTree[i<<1].numseg+segTree[(i<<1)|1].numseg;
        if(segTree[i<<1].rcover&&segTree[(i<<1)|1].lcover)segTree[i].numseg--;
    }
}
void update(int i,Line e)
{
    if(segTree[i].lf==e.x1&&segTree[i].rf==e.x2)
    {
        segTree[i].c+=e.c;
        calen(i);
        return;
    }
    if(e.x2<=segTree[i<<1].rf)update(i<<1,e);
    else if(e.x1>=segTree[(i<<1)|1].lf)update((i<<1)|1,e);
    else
    {
        Line temp=e;
        temp.x2=segTree[i<<1].rf;
        update(i<<1,temp);
        
        temp=e;
        temp.x1=segTree[(i<<1)|1].lf;
        update((i<<1)|1,temp);
    }
    calen(i);
}
int main()
{
    int x1,y1,x2,y2;
    int n;
    while(scanf("%d",&n)==1)
    {
        int t=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            line[t].x1=x1;  ///沿x轴离散化
            line[t].x2=x2;
            line[t].y=y1;
            line[t].c=1;
            x[t++]=x1;


            line[t].x1=x1;
            line[t].x2=x2;
            line[t].y=y2;
            line[t].c=-1;
            x[t++]=x2;
        }
        sort(line,line+t,cmp);

        sort(x,x+t);
        int m=unique(x,x+t)-x;//合并相同元素,这里一点要合并相同元素,否则会WA.
        Build(1,0,m-1);
        int ans=0;
        int last=0;
        for(int i=0;i<t-1;i++)
        {
            update(1,line[i]);
            ans+=segTree[1].numseg*2*(line[i+1].y-line[i].y);
            ans+=abs(segTree[1].cnt-last);
            last=segTree[1].cnt;
        }
        update(1,line[t-1]);
        ans+=abs(segTree[1].cnt-last);

        printf("%d\n",ans);
    }
    return 0;
}



你可能感兴趣的:(POJ 1177 Picture(线段树+离散化 求周长))