1. 程式人生 > >HDU 1828 Picture(線段樹+掃描線求周長並)

HDU 1828 Picture(線段樹+掃描線求周長並)

Picture

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


Problem DescriptionA 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.
InputYour 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.
OutputYour 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 Input7 -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 Output228
題解:
和求面積類似,不過這裡橫邊豎邊都要求。這裡要注意,
邊有重合的地方的情況,在求面積時是不影響的,但是
求周長不能重複相加,因此可以在排序的時候若x值相同
則先排入邊後排出邊(之前一直不知道怎麼去重,看了一位
大神的部落格是這樣搞的,實在太巧妙啦)。

程式碼:

#include<bits/stdc++.h>
using namespace std;
const int maxn=5007;
int x1[maxn],y11[maxn],x2[maxn],y2[maxn];
int x[maxn*2],y[maxn*2];
struct LINE
{
    int x,y_down,y_up;
    int flag;
} line[maxn*2];
bool cmp(const LINE& a,const LINE& b)
{
    if(a.x!=b.x)return a.x<b.x;
    return a.flag>b.flag;
}
struct TREE
{
    int x,y_down,y_up;
    int cover;
    bool flag;
} tree[maxn<<4];
int abs1(int x)
{
    if(x<0)return -x;
    return x;
}
void build(int l,int r,int rt)
{
    tree[rt].x=-1;
    tree[rt].y_down=y[l];
    tree[rt].y_up=y[r];
    tree[rt].flag=0;
    tree[rt].cover=0;
    if(l+1==r)
    {
        tree[rt].flag=1;
        return;
    }
    build(l,(l+r)>>1,rt<<1);
    build((l+r)>>1,r,rt<<1|1);
}
int query(int x,int l,int r,int rt,int flag)
{
    if(r<=tree[rt].y_down||l>=tree[rt].y_up)
        return 0;
    if(tree[rt].flag)
    {
        if(tree[rt].cover<=0)
        {
            tree[rt].cover+=flag;
            int ans=abs1(tree[rt].y_up-tree[rt].y_down);
            tree[rt].x=x;
            return ans;
        }
        else
        {
            tree[rt].cover+=flag;
            tree[rt].x=x;
            return 0;
        }
    }
    return query(x,l,r,rt<<1,flag)+query(x,l,r,rt<<1|1,flag);
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int i,j;
        if(n==0)
        {
            printf("0\n");
            continue;
        }
        for(i=1; i<=n; i++)
            scanf("%d%d%d%d",&x1[i],&y11[i],&x2[i],&y2[i]);
        int t=0;
        for(i=1; i<=n; i++)
        {
            t++;
            y[t]=y11[i];
            line[t].x=x1[i];
            line[t].y_down=y11[i];
            line[t].y_up=y2[i];
            line[t].flag=1;
            t++;
            y[t]=y2[i];
            line[t].x=x2[i];
            line[t].y_down=y11[i];
            line[t].y_up=y2[i];
            line[t].flag=-1;
        }
        sort(y+1,y+t+1);
        sort(line+1,line+t+1,cmp);
        build(1,t,1);
        int ans=0;
        for(i=1; i<=t; i++)
            ans+=query(line[i].x,line[i].y_down,line[i].y_up,1,line[i].flag);
        t=0;
        for(i=1; i<=n; i++)
        {
            t++;
            y[t]=x1[i];
            line[t].x=y11[i];
            line[t].y_down=x1[i];
            line[t].y_up=x2[i];
            line[t].flag=1;
            t++;
            y[t]=x2[i];
            line[t].x=y2[i];
            line[t].y_down=x1[i];
            line[t].y_up=x2[i];
            line[t].flag=-1;
        }
        sort(y+1,y+t+1);
        sort(line+1,line+t+1,cmp);
        build(1,t,1);
        for(i=1; i<=t; i++)
            ans+=query(line[i].x,line[i].y_down,line[i].y_up,1,line[i].flag);
        printf("%d\n",2*ans);
        //邊是對稱的,求的時候只求了一條。
    }
    return 0;
}