HDU3265 线段树 线扫描

来源:互联网 发布:淘宝网卖什么赚钱 编辑:程序博客网 时间:2024/06/08 18:57

传送门:点击打开链接

Posters

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5003    Accepted Submission(s): 1154


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
20 0 10 10 1 1 9 92 2 8 8 3 3 7 70
 


Sample Output
56
 


Source
2009 Asia Ningbo Regional Contest Hosted by NIT 

题意:矩形的海报,海报上有一个矩形的洞。将一些这样的海报贴在窗户上,问最后能看到的海报的面积并。
思路:用线段树进行线扫描,根据线扫面的方法,我们需要将一张海报分解为如下四个矩形。
这样分格后就可以进行线扫面了,当然,其他的分割为矩形的方法也可以。
代码:
#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#define LL __int64using namespace std;struct node{    int x1,x2,h,c;    inline bool operator <(const node &b) const    {        return h<b.h;    }}T[402025];int cnt[202025],len[202025];void op(int id,int L,int R,int l,int r,int c){    if(r<l) return;    if(l<=L&&R<=r)    {        cnt[id]+=c;        if(cnt[id]>0) len[id]=R-L+1;        else        {            if(L==R) len[id]=0;            else len[id]=len[id<<1]+len[id<<1|1];        }    }    else    {        int mid=(L+R)>>1;        if(l<=mid) op(id<<1,L,mid,l,r,c);        if(mid<r) op(id<<1|1,mid+1,R,l,r,c);        if(cnt[id]>0) len[id]=R-L+1;        else        {            if(L==R) len[id]=0;            else len[id]=len[id<<1]+len[id<<1|1];        }    }}int main(){    int n;    while(scanf("%d",&n),n)    {        memset(cnt,0,sizeof(cnt));        memset(len,0,sizeof(len));        int x1,y1,x2,y2,x3,x4,y3,y4;        int i;        for(i=0;i<n;i++)        {            scanf("%d %d %d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);            T[i*8+0]=(node){x1,x4,y1,1};            T[i*8+1]=(node){x1,x4,y3,-1};            T[i*8+2]=(node){x4,x2,y1,1};            T[i*8+3]=(node){x4,x2,y4,-1};            T[i*8+4]=(node){x3,x2,y4,1};            T[i*8+5]=(node){x3,x2,y2,-1};            T[i*8+6]=(node){x1,x3,y3,1};            T[i*8+7]=(node){x1,x3,y2,-1};        }        sort(T,T+n*8);        LL ans=0;        for(i=0;i<n*8-1;i++)        {            op(1,0,50000,T[i].x1,T[i].x2-1,T[i].c);            ans+=(LL)(T[i+1].h-T[i].h)*(LL)len[1];        }        printf("%I64d\n",ans);    }    return 0;}

 

0 0