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

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

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,r
const 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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值