【poj1177】Picture(矩形周长并+线段树+扫描线)

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.
【poj1177】Picture(矩形周长并+线段树+扫描线)_第1张图片
    The corresponding boundary is the whole set of line segments drawn in Figure 2.
【poj1177】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

I think

    题意:给出多个矩形左下角与右上角端点的横纵坐标,求矩形周长并。
    算法:线段树+扫描线+离散化
    思路:扫描线的基本思路与矩形面积并一致,详见 【poj1151】Atlantis(矩形面积并+线段树+扫描线)
    实现:以下代码将y轴坐标离散化。
    扫描当前边之前,用扫过上一条边之后的总区间包含不连续段数×2×两条边横坐标差值,更新ans,此即该段沿X轴方向的周长的长度。扫过后,用扫过当前线段后整个区间的被覆盖长度与扫过上一条线段后的整个区间的被覆盖长度的差值(abs)更新ans,此即沿Y轴方向的周长。
    维护区间包含段数需新建数组ll[],rr[],分别表示区间左/右端点是否被覆盖,合并两个区间信息时,当且仅当该区间的左半区间的右端点与右半区间的左端点同时被覆盖的情况下,左右区间不连续段数和-1。

Code

#include
#include
#include
#include
using namespace std;
const int sm = 1e4+50;
int n,t,T,x1,y1,x2,y2,ans,pre,linesum;
int val[sm<<2],c[sm<<2],y[sm<<2];
int cnt[sm<<2],ll[sm<<2],rr[sm<<2];
//ll记最左端的区间是否被覆盖 
//rr记最右端的区间是否被覆盖
//cnt记该区间总不连续段数 
//用于计算未被离散化的轴方向的边的条数
struct line {
    int x,y1,y2,flag;
}a[sm];
bool cmp(line a,line b) { return a.xvoid calen(int x,int l,int r) {
    if(c[x]>0) { 
        val[x]=y[r]-y[l];
        cnt[x]=ll[x]=rr[x]=1;
    }
    else if(l+1==r) val[x]=cnt[x]=ll[x]=rr[x]=0;
    else { 
        int ls=x<<1,rs=x<<1|1;
        ll[x]=ll[ls],rr[x]=rr[rs];
        val[x]=val[ls]+val[rs];
        cnt[x]=cnt[ls]+cnt[rs]-rr[ls]*ll[rs];
    }
}

void update(int k,int l,int r,line x) {
    if(x.y1<=y[l]&&y[r]<=x.y2) {
        c[k]+=x.flag;calen(k,l,r);
        return;
    }
    if(l+1==r)return;
    int m=(l+r)>>1;
    if(x.y1<=y[m])update(k<<1,l,m,x);
    if(x.y2>=y[m])update(k<<1|1,m,r,x);
    calen(k,l,r);
}
int main() {
    scanf("%d",&n);
    for(int i=1;i<=n;++i) {
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        y[++t]=y1,a[t].x=x1,a[t].y1=y1,a[t].y2=y2,a[t].flag=1;
        y[++t]=y2,a[t].x=x2,a[t].y1=y1,a[t].y2=y2,a[t].flag=-1;
    }
    sort(y+1,y+t+1);
    sort(a+1,a+t+1,cmp);
    T=unique(y+1,y+t+1)-y-1;
    pre=linesum=0;
    for(int i=1;i<=t;++i) {
        if(i>1)ans+=2*linesum*(a[i].x-a[i-1].x);
        update(1,1,T,a[i]);
        ans+=abs(val[1]-pre);
        pre=val[1],linesum=cnt[1];
    }       
    printf("%d\n",ans);
    return 0;
}

你可能感兴趣的:(线段树)