POJ1177_Picture

Picture
Time Limit: 2000MS   Memory Limit: 10000K
Total Submissions: 11643  
Accepted:
 6141

计算几何,扫描线,看了好几天看懂一点,

推荐博客:http://www.cppblog.com/abilitytao/archive/2010/07/21/120927.html

http://www.cnblogs.com/shuaiwhu/archive/2012/04/22/2464876.html

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.

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

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int N=1e4+6;
struct node
{
    int l,r;
    int cover;
    int lcover,rcover;
    int sum;
    int len;
    int seg;

};
node ST[N<<2];
int A[N];
int n,cnt;
struct Line
{
    int x,y1,y2;
    int Inline;
    bool operator<(const Line& J)const
    {
        return x1)
    {
        int mid=(l+r)>>1;
        build(l,mid,rt<<1);
        build(mid,r,rt<<1|1);
    }
}
void getsum(int rt)
{
    if(ST[rt].cover>0)
        ST[rt].sum=ST[rt].len;
    else if(ST[rt].r-ST[rt].l>1)
        ST[rt].sum=ST[rt<<1].sum+ST[rt<<1|1].sum;
    else
        ST[rt].sum=0;
}
void getseg(int rt)
{
    if(ST[rt].cover>0)
    {
        ST[rt].lcover=ST[rt].rcover=1;
        ST[rt].seg=1;
    }
    else if(ST[rt].r-ST[rt].l>1)
    {
        ST[rt].lcover=ST[rt<<1].lcover;
        ST[rt].rcover=ST[rt<<1|1].rcover;
        ST[rt].seg=ST[rt<<1].seg+ST[rt<<1|1].seg-ST[rt<<1].rcover*ST[rt<<1|1].lcover;
    }
    else
    {
        ST[rt].rcover=ST[rt].lcover=0;
        ST[rt].seg=0;
    }
}
void Insert(int l,int r,int rt)
{
    if(l==ST[rt].l&&r==ST[rt].r)
        ST[rt].cover++;
    else
    {
        int mid=(ST[rt].l+ST[rt].r)>>1;
        if(l>=mid)
            Insert(l,r,rt<<1|1);
        else if(r<=mid)
            Insert(l,r,rt<<1);
        else
        {
            Insert(l,mid,rt<<1);
            Insert(mid,r,rt<<1|1);
        }
    }
    getsum(rt);
    getseg(rt);
}
void Delete(int l,int r,int rt)
{
    if(l==ST[rt].l&&r==ST[rt].r)
        ST[rt].cover--;
    else
    {
        int mid=(ST[rt].l+ST[rt].r)>>1;
        if(l>=mid)
            Delete(l,r,rt<<1|1);
        else if(r<=mid)
            Delete(l,r,rt<<1);
        else
        {
          Delete(l,mid,rt<<1);
        Delete(mid,r,rt<<1|1);
        }
    }
    getsum(rt);
    getseg(rt);
}
int getx(int x)
{
    return lower_bound(A,A+cnt,x)-A;
}
int main()
{
    while(cin>>n)
    {
        cnt=0;
        int x1,x2,y1,y2;
        for(int i=0;i


你可能感兴趣的:(几何!)