HDU 3265——Posters(线段树+面积并+矩形分割)

来源:互联网 发布:es7 js 注解功能 编辑:程序博客网 时间:2024/05/16 17:28

Posters

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


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
 


——————————————————————分割线——————————————


题目大意:

有n张矩形海报,在每张海报上减掉一个小矩形,每张海报都是水平或者垂直的,求这n个矩形的并面积


思路:

在这里我不写什么思路了,我想写些对于面积并,周长并的线段树的理解


对于面积并和周长并的线段树,区间更新并没有用到push_down,每次用的是总区间的信息,不需要查询。而如果要用push_down来维护区间有什么意义呢?我们又不需要查询,并且如果用到push_down的话,又应该怎么上传标记呢?

对不同的题目应该有不同的方式


对于面积并和周长并这类的,在格内是或运算,格间是与运算。

push_up操作很直观简单地实现了面积并这类格内是或运算的操作



再来看这题,那么我们需要将矩形进行分割成4个小矩形,不管怎么分都行,只要分的矩形下底边和上底边之间不为空。 这也是这类题型的性质,才使得push_up能够实现我们需要的功能


再好好理解

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define ll long longconst int maxn=55555;using namespace std;int cnt[maxn<<2],sum[maxn<<2];int m;struct Seg{    int l,r,h;    int s;    Seg(){};    Seg(int a,int b,int c,int d):l(a),r(b),h(c),s(d){};    bool operator<(const Seg&cmp)const{        return h<cmp.h;    }}ss[maxn<<3];void push_up(int rt,int l,int r){    if(cnt[rt])        sum[rt]=r-l+1;    else if(l==r)        sum[rt]=0;    else        sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void update(int L,int R,int c,int l,int r,int rt){    if(L<=l&&r<=R){        cnt[rt]+=c;        push_up(rt,l,r);        return ;    }    int m=(l+r)>>1;    if(L<=m) update(L,R,c,lson);    if(m<R) update(L,R,c,rson);    push_up(rt,l,r);}void solve(){    memset(cnt,0,sizeof(cnt));    memset(sum,0,sizeof(sum));    sort(ss,ss+m);    ll ret=0;    for(int i=0;i<m-1;++i){        int l=ss[i].l;        int r=ss[i].r-1;        if(l<=r)            update(l,r,ss[i].s,0,maxn,1);        ret+=(ll)sum[1]*(ss[i+1].h-ss[i].h);    }    printf("%I64d\n",ret);}void add_seg(int a,int b,int c,int d){//    if(a==c||b==d) return;    ss[m++]=Seg(a,c,b,1);    ss[m++]=Seg(a,c,d,-1);}int main(){    int n;    while(scanf("%d",&n),n){        int x1,y1,x2,y2,x3,y3,x4,y4;        m=0;        while(n--){            scanf("%d %d %d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);            add_seg(x1,y1,x3,y4);            add_seg(x3,y1,x2,y3);            add_seg(x1,y4,x4,y2);            add_seg(x4,y3,x2,y2);        }        solve();    }    return 0;}


0 0