Posters

http://acm.hdu.edu.cn/showproblem.php?pid=3265

Posters

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


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
 
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <string>
#include <iostream>
#include <string>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
const int SIZE=5e4+10;
using namespace std;
struct sge{
    int l,r,h,s;
    sge(){}
    sge(int a,int b,int c,int d):l(a),r(b),h(c),s(d){}
    bool operator<(const sge &other)const{
        return h<other.h;
    }
}ss[SIZE<<2];
int sum[SIZE<<2];
int X[SIZE<<2];
int col[SIZE<<2];
void calc(int l,int r,int rt){
    if(col[rt])sum[rt]=X[r+1]-X[l];
    else if(l==r)sum[rt]=0;
    else sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void pushup(int l,int r,int rt){
    if(col[rt<<1]&&col[rt<<1|1]){
        int temp=col[rt<<1]<col[rt<<1|1]?col[rt<<1]:col[rt<<1|1];
        col[rt]=temp;
        col[rt<<1]-=temp;
        col[rt<<1|1]-=temp;
        int m=(l+r)>>1;
        calc(lson);
        calc(rson);
    }
}
void pushdown(int l,int r,int rt){
    if(col[rt]){
        col[rt<<1]+=col[rt];
        col[rt<<1|1]+=col[rt];
        int m=(l+r)>>1;
        calc(lson);
        calc(rson);
        col[rt]=0;
        calc(l,r,rt);
    }
}
int Bin(int key,int n,int X[]){
    int l=0,r=n-1;
    while(l<=r){
        int m=(l+r)>>1;
        if(X[m]==key)return m;
        else if(X[m]>key)r=m-1;
        else l=m+1;
    }
    return -1;
}
void update(int L,int R,int c,int l,int r,int rt){
    if(L<=l&&r<=R){
        col[rt]+=c;
        calc(l,r,rt);
        return ;
    }
    pushdown(l,r,rt);
    int m=(l+r)>>1;
    if(L<=m)update(L,R,c,lson);
    if(R>m)update(L,R,c,rson);
    pushup(l,r,rt);
    calc(l,r,rt);
}
int main()
{
    //freopen("F://in.txt","r",stdin);
    int n,x1,x2,x3,x4,y1,y2,y3,y4;
    while(scanf("%d",&n)&&n){
        int tot=0;
        __int64 cnt=0;
        for(int i=0;i<n;i++){
            scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
            X[tot]=x1;
            ss[tot++]=sge(x1,x2,y1,1);
            X[tot]=x2;
            ss[tot++]=sge(x3,x4,y3,-1);
            X[tot]=x3;
            ss[tot++]=sge(x3,x4,y4,1);
            X[tot]=x4;
            ss[tot++]=sge(x1,x2,y2,-1);
        }
        sort(ss,ss+tot);
        sort(X,X+tot);
        int k=1;
        memset(sum,0,sizeof(sum));
        memset(col,0,sizeof(col));
        for(int i=1;i<tot;i++)
            if(X[i]!=X[i-1])X[k++]=X[i];
        for(int i=0;i<tot-1;i++){
            int l=Bin(ss[i].l,k,X);
            int r=Bin(ss[i].r,k,X)-1;
            if(l<=r)update(l,r,ss[i].s,0,k-2,1);
            cnt+=(__int64)sum[1]*(ss[i+1].h-ss[i].h);//不知道为啥不装换就wa了,用unsinged能过,但不用转
            //printf("%d\n",sum[1]);
        }
        printf("%I64d\n",cnt);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值