hud 3265 posters(扫描线求镂空矩形面积模板)

Problem Description

Ted has a new house with a huge window. In this big summer, Ted decides to decorate the window with some posters to prevent the glare outside. All things that Ted can find are rectangle posters. 

However, Ted is such a picky guy that in every poster he finds something ugly. So before he pastes a poster on the window, he cuts a rectangular hole on that poster to remove the ugly part. Ted is also a careless guy so that some of the pasted posters may overlap when he pastes them on the window. 

Ted wants to know the total area of the window covered by posters. Now it is your job to figure it out.

To make your job easier, we assume that the window is a rectangle located in a rectangular coordinate system. The window’s bottom-left corner is at position (0, 0) and top-right corner is at position (50000, 50000). The edges of the window, the edges of the posters and the edges of the holes on the posters are all parallel with the coordinate axes. 

Input

The input contains several test cases. For each test case, the first line contains a single integer N (0<N<=50000), representing the total number of posters. Each of the following N lines contains 8 integers x1, y1, x2, y2, x3, y3, x4, y4, showing details about one poster. (x1, y1) is the coordinates of the poster’s bottom-left corner, and (x2, y2) is the coordinates of the poster’s top-right corner. (x3, y3) is the coordinates of the hole’s bottom-left corner, while (x4, y4) is the coordinates of the hole’s top-right corner. It is guaranteed that 0<=xi, yi<=50000(i=1…4) and x1<=x3<x4<=x2, y1<=y3<y4<=y2.

The input ends with a line of single zero.

Output

For each test case, output a single line with the total area of window covered by posters.

Sample Input

 

2 0 0 10 10 1 1 9 9 2 2 8 8 3 3 7 7 0

Sample Output

 

56

Source

2009 Asia Ningbo Regional Contest Hosted by NIT

Recommend

lcy   |   We have carefully selected several similar problems for you:  3268 3264 3262 3269 3263 

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define maxn 50005
#define lson 2*i,l,m
#define rson 2*i+1,m+1,r
int cnt[maxn*4],sum[maxn*4];
struct node
{
    int l,r,h,d;
    node()
    {

    }
    node(int a,int b,int c,int d):l(a),r(b),h(c),d(d){}
    bool operator<(const node&b)const
    {
        if(b.h==h)
            return d>b.d;
        return h<b.h;
    }
}nodes[maxn*8];
void pushup(int i,int l,int r)
{
    if(cnt[i])
        sum[i]=r-l+1;
    else   if(l==r)
    sum[i]=0;
    else sum[i]=sum[i*2]+sum[i*2+1];


}
void update(int ql,int qr,int v,int i,int l,int r)
{
    if(ql<=l&&r<=qr)
    {
        cnt[i]+=v;
        pushup(i,l,r);
        return ;
    }
    int m=(l+r)>>1;
    if(ql<=m)
        update(ql,qr,v,lson);
    if(m<qr)
        update(ql,qr,v,rson);
        pushup(i,l,r);
}
int main()
{
    int t;
    while(~scanf("%d",&t)&&t)
    {
        int m=0;
        int lbd=50000;
        int rbd=0;
        for(int i=1;i<=t;i++)
        {int x1,y1,x2,y2,x3,y3,x4,y4;
            scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
            lbd=min(lbd,x1);
            rbd=max(rbd,x2);
            nodes[++m]=node(x1,x3,y1,1);
            nodes[++m]=node(x1,x3,y2,-1);
            nodes[++m]=node(x4,x2,y1,1);
            nodes[++m]=node(x4,x2,y2,-1);
            nodes[++m]=node(x3,x4,y1,1);
            nodes[++m]=node(x3,x4,y3,-1);
            nodes[++m]=node(x3,x4,y4,1);
            nodes[++m]=node(x3,x4,y2,-1);

    }
    sort(nodes+1,nodes+m+1);
memset(cnt,0,sizeof(cnt));
memset(sum,0,sizeof(sum));
    long long ans=0;
    for(int i=1;i<=m;i++)
    {
        int ql=nodes[i].l;
        int qr=nodes[i].r-1;
        if(ql<=qr)
            update(ql,qr,nodes[i].d,1,lbd,rbd-1);
        ans+=(long long)sum[1]*(nodes[i+1].h-nodes[i].h);
    }
    printf("%lld\n",ans);
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/sdauguanweihong/article/details/83870511
HUD