Posters (HDU-3265)

Posters (HDU-3265)

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

题目翻译:

泰德有一所新房子,窗户很大。在这个大夏天,特德决定用一些海报装饰窗户,以防止外面耀眼的阳光。泰德能找到的所有东西都是长方形的海报。

然而,泰德是一个如此挑剔的人,他在每一张海报上都发现一些丑陋的东西。所以在他把海报贴在窗户上之前,他在海报上剪了一个长方形的洞,把难看的部分去掉。泰德也是个粗心大意的人,所以当他把一些贴好的海报贴在窗户上时,它们可能会重叠。

泰德想知道被海报覆盖的窗户的总面积。现在你的工作就是找出答案。

为了使您的工作更容易,我们假设窗口是位于直角坐标系中的一个矩形。窗口的左下角位于位置(0,0),右上角位于位置(50000,50000)。窗户的边缘、海报的边缘、海报上孔的边缘都与坐标轴平行。

输入

输入包含几个测试用例。对于每个测试用例,第一行包含一个整数N (0下面的N行每一行包含8个整数x1、y1、x2、y2、x3、y3、x4、y4,显示一张海报的细节。(x1, y1)是海报左下角的坐标,(x2, y2)是海报右上角的坐标。(x3, y3)是孔左下角的坐标,(x4, y4)是孔右上角的坐标。保证0<=xi, yi<=50000(i=1…4),x1<=x3

输入以一行0结束。

输出

对于每个测试用例,输出一行带有海报覆盖的窗口总面积。

思路:将一个图形差分成至多4个小的矩形表示,然后直接扫描线,要注意用__int64,不能用long long,否则会wa的很惨的。。。。。。

AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define PI 3.1415926
#define inf 0x3f3f3f3f
const int maxn=50500;
using namespace std;
struct s
{
    int x1,x2,y;
    int f;
} a[maxn<<3];
int A[maxn<<5];
__int64 Sum[maxn<<5];
int k;
int S[maxn<<5];
int cmp(s a,s b)
{
    return a.y<b.y;
}
void Add(int x1,int y1,int x2,int y2)
{
    if(x1==x2 || y1==y2)
        return;
    a[k].x1=x1;
    a[k].x2=x2;
    a[k].y=y1;
    a[k].f=1;
    A[k++]=x1;
    a[k].x1=x1;
    a[k].x2=x2;
    a[k].y=y2;
    a[k].f=-1;
    A[k++]=x2;
}
int KT(double key,int n)
{
    int l=1,r=n;
    while(l<=r)
    {
        int m=(l+r)>>1;
        if(A[m]==key)
            return m;
        if(A[m]<key)
            l=m+1;
        else
            r=m-1;
    }
    return l;
}
void PushUp(int rt,int l,int r)
{
    if(S[rt])
        Sum[rt]=A[r+1]-A[l];
    else if(l==r)
        Sum[rt]=0;
    else
        Sum[rt]=Sum[rt<<1]+Sum[rt<<1|1];
}
void Update(int x,int y,int l,int r,int rt,int f)
{
    if(x<=l && r<=y)
    {
        S[rt]+=f;
        PushUp(rt,l,r);
        return;
    }
    int m=(l+r)>>1;
    if(x<=m)
        Update(x,y,l,m,rt<<1,f);
    if(y>m)
        Update(x,y,m+1,r,rt<<1|1,f);
    PushUp(rt,l,r);
}
int main()
{
    int n,c=0;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        memset(S,0,sizeof(S));
        memset(Sum,0,sizeof(Sum));
        int x1,x2,y1,y2,x3,y3,x4,y4;
        int i;
        k=1;
        for(i=1; i<=n; i++)
        {
            scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
            Add(x1,y2,x3,y1);
            Add(x3,y3,x4,y1);
            Add(x3,y2,x4,y4);
            Add(x4,y2,x2,y1);
        }
        sort(a+1,a+k,cmp);
        sort(A+1,A+k);
        __int64 ans=0;
        for(i=1; i<k-1; i++)
        {
            int x,y;
            x=KT(a[i].x1,k-1);
            y=KT(a[i].x2,k-1)-1;
            if(x<=y)
                Update(x,y,1,k-1,1,a[i].f);
            ans+=Sum[1]*(a[i+1].y-a[i].y);
        }
        printf("%I64d\n",ans);
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值