UESTC Training for Data Structures——D

Problem  D

Problem Description

  God KuFeng has a new house,in this big summer he is so boring.One day he noticed that there is a huge window in the house,, God KuFeng decides to decorate the window with some posters to prevent the glare outside. All things that God KuFeng can find are rectangle posters.
  However, God KuFeng 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. He is also a careless guy so that some of the pasted posters may overlap when he pastes them on the window.
  God KuFeng 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 first line is an integer T(T<=10)--The number of test cases.
For each of the test case,the first line contains a single integer N (0<N<=10000), 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.

Output

For each test case, output a single line with the total area of window covered by posters.

Sample Input

1
2
0 0 10 10 1 1 9 9
2 2 8 8 3 3 7 7

Sample Output

56

 

/*扫描线求矩形面积的并
  刚开始的不会,后来看了会资料,又请教了几个大神,终于是学会了
  我把矩形分解成x方向的两条线段,然后从y轴开始由小到大扫描*/


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#define N 50550
#define INF 10000000
using namespace std;
struct data1  //记录线段
{
    int x1,x2,y;
    int fg;  //出边 -1 ; 入边  1
} line[8*N];
struct data2  //记录线段树
{
    int be,en,len,bj;
} tree[4*N];
void qsort(int st,int en)   //对线段从小到大排序
{
    int i=st,j=en;
    line[0]=line[i];
    while(i<j)
    {
        while(i<j && line[0].y<=line[j].y) j--;
        if(i<j) { line[i]=line[j]; i++; }
        while(i<j && line[0].y>=line[i].y) i++;
        if(i<j) { line[j]=line[i]; j--; }
    }
    line[i]=line[0];
    if(st<i-1) qsort(st,i-1);
    if(i+1<en) qsort(i+1,en);
}
void add_line(int x1,int x2,int y,int fg,int *tot)  //在线段集合中添加线段
{
    if(x1==x2) return;
    (*tot)++;
    line[*tot].x1=x1,line[*tot].x2=x2,line[*tot].y=y,line[*tot].fg=fg;
}
void make_tree(int v,int be,int en)  //建树
{
    tree[v].be=be;
    tree[v].en=en;
    tree[v].bj=tree[v].len=0;
    if(be==en) return;
    int mid=(be+en)>>1;
    make_tree(v<<1,be,mid);
    make_tree((v<<1)+1,mid+1,en);
}
void clear(int v)  //对线段树的节点的标记域进行传递
{
    if(tree[v].bj!=0) tree[v].len=tree[v].en-tree[v].be+1;
    else if(tree[v].be==tree[v].en) tree[v].len=0;
    else tree[v].len=tree[v<<1].len+tree[(v<<1)+1].len;
}
void update(int v,int x1,int x2,int fg)  //更新线段树中 [x1,x2] 的线段
{
    if(x1<=tree[v].be && x2>=tree[v].en)
    {
        tree[v].bj+=fg;
        clear(v);
        return;
    }
    int mid=(tree[v].be+tree[v].en)>>1;
    if(x2<=mid) update(v<<1,x1,x2,fg);
    else if(x1>mid) update((v<<1)+1,x1,x2,fg);
    else
    {
        update(v<<1,x1,x2,fg);
        update((v<<1)+1,x1,x2,fg);
    }
    clear(v);
}
int main()
{

    int t;
    int n,x1,x2,x3,x4,y1,y2,y3,y4;
    scanf("%d",&t);
    while(t--)
    {
        memset(line,0,sizeof(line));
        memset(tree,0,sizeof(tree));
        int tot=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)  //将回字形的区域分割成四个矩形
        {
            scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
            add_line(x1,x3,y1,1,&tot); add_line(x1,x3,y2,-1,&tot);
            add_line(x3,x4,y1,1,&tot); add_line(x3,x4,y3,-1,&tot);
            add_line(x3,x4,y4,1,&tot); add_line(x3,x4,y2,-1,&tot);
            add_line(x4,x2,y1,1,&tot); add_line(x4,x2,y2,-1,&tot);
        }
        qsort(1,tot);
        make_tree(1,1,N-1);
        __int64 ans=0;
        line[0]=line[1];
        for(int i=1;i<=tot;i++)  //统计面积的和
        {
            ans+=(__int64)tree[1].len*(line[i].y-line[i-1].y);
            update(1,line[i].x1+1,line[i].x2,line[i].fg);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值