hdu3265-Posters 线段树+离散化 求矩形面积并

来源:互联网 发布:linux 启动文件丢失 编辑:程序博客网 时间:2024/05/21 08:14

Posters

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


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
题目大意:给你一些矩形,从中间扣掉矩形贴在窗户上,求最后这些贴的地方的面积,即求面积并。
解题思路:这道题和 hdu1542求面积并很像,只是这里的矩形变成了一个中间有矩形空洞的矩形,所以我们把这个矩形变成4个矩形就可以了,然后再用相同的方法就可以求出总的面积并了。
ac代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>using namespace std;#define  lz  2*u,l,mid#define  rz  2*u+1,mid+1,rconst int maxn=50005;typedef long long lld; lld sum[4*maxn];int flag[4*maxn];struct Node{    int lx, rx, h, s;    Node(){}    Node(int lx_, int rx_, int h_, int s_)    {        lx=lx_, rx=rx_, h=h_, s=s_;    }    bool operator<(const Node &S)const    {        return h<S.h;    }}line[8*maxn];void push_up(int u, int l, int r){    if(flag[u]) sum[u]=r-l+1;    else if(l==r) sum[u]=0;    else sum[u]=sum[2*u]+sum[2*u+1];}void Update(int u, int l, int r, int tl, int tr, int c){    if(tl>tr) return ;  ///!!!注意 当x1==x2时,会导致RE,因为算区间的时候x2会减-1    if(tl<=l&&r<=tr)    {        flag[u]+=c;        push_up(u,l,r);        return ;    }    int mid=(l+r)>>1;    if(tr<=mid) Update(lz,tl,tr,c);    else if(tl>mid) Update(rz,tl,tr,c);    else    {        Update(lz,tl,mid,c);        Update(rz,mid+1,tr,c);    }    push_up(u,l,r);}int main(){    int n;    while(cin >> n,n)    {        memset(flag,0,sizeof(flag));        memset(sum,0,sizeof(sum));        int num=0;        int x1, y1, x2, y2, x3, y3, x4, y4;        int lbd=maxn, rbd=-1;        for(int i=0; i<n; i++)        {            scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x4,&y4,&x2,&y2,&x3,&y3);            line[++num]=Node(x1,x4,y1,1);            line[++num]=Node(x1,x4,y2,-1);            line[++num]=Node(x1,x4,y3,1);            line[++num]=Node(x1,x4,y4,-1);            line[++num]=Node(x1,x2,y2,1);            line[++num]=Node(x1,x2,y3,-1);            line[++num]=Node(x3,x4,y2,1);            line[++num]=Node(x3,x4,y3,-1);            lbd=min(x1,lbd);            rbd=max(x4,rbd);        }        sort(line+1,line+num+1);        lld ans=0;        for(int i=1; i<num; i++)        {            Update(1,lbd,rbd,line[i].lx,line[i].rx-1,line[i].s);            ans+=sum[1]*(lld)(line[i+1].h-line[i].h); //要使用long long         }        printf("%lld\n", ans);    }    return 0;}


题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=3265

hdu1542解题报告:点击打开链接http://blog.csdn.net/wang_heng199/article/details/75647032

线段树专题网址:点击打开链接http://blog.csdn.net/wang_heng199/article/details/74938672

阅读全文
0 0
原创粉丝点击