[离散化+线段树+扫描线]POJ_3832_Posters

来源:互联网 发布:迈卡威新秀数据 编辑:程序博客网 时间:2024/05/15 17:44

 

题意:给定N个海报,海报中间被挖掉了一个矩形的孔,所有的海报贴在(0,0)-(50000,50000)的矩形区域里,求海报覆盖的面积。

 

思路:把1个海报分成四个小矩形,求矩形面积并。(很裸)

 

各种蛋疼:

                1.建立段树一定要记得加判断,对于插入一个点的情况不予考虑(比如插入区间为0,0)或者在线段树里面加判断(l==r-1)return;(不加的话等着StackOverFlow吧)

                2.不知为何,这个题一定要unsigned int才能过。。(long long __int64)都不可以,我蛋碎了。

 

代码:

       

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN = 55555;struct Line{    int l,r,h,s;    Line(){}    Line(int ll,int rr,int hh,int ss):l(ll),r(rr),h(hh),s(ss){}    bool operator < (const Line &l) const{        return h<l.h;    }}L[MAXN<<4];#define lson l,m,rt<<1#define rson m,r,rt<<1|1int sum[MAXN<<3],cnt[MAXN<<3];void build(){    memset(sum,0,sizeof(sum));    memset(cnt,0,sizeof(cnt));}void pushUP(int rt,int l,int r){    if(cnt[rt]){        sum[rt] = r-l;    }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;        pushUP(rt,l,r);        return;    }    //if(l==r-1)return;//如果L==R。。。。建立段树的话需要注意这个    int m = (l+r)>>1;    if(m>L)update(L,R,c,lson);    if(m<R)update(L,R,c,rson);    pushUP(rt,l,r);}int main(){    int x[4],y[4],n,tot;    while(scanf("%d",&n),n){        tot = 0;        for(int i=0;i<n;i++){            for(int j=0;j<4;j++){                scanf("%d%d",&x[j],&y[j]);            }            L[tot++] = Line(x[0],x[1],y[0],1);            L[tot++] = Line(x[0],x[1],y[2],-1);            L[tot++] = Line(x[0],x[2],y[2],1);            L[tot++] = Line(x[0],x[2],y[3],-1);            L[tot++] = Line(x[3],x[1],y[2],1);            L[tot++] = Line(x[3],x[1],y[3],-1);            L[tot++] = Line(x[0],x[1],y[3],1);            L[tot++] = Line(x[0],x[1],y[1],-1);        }        sort(L,L+tot);        build();        unsigned int ans = 0;        for(int i=0;i<tot;i++){            if(i==0){                if(L[i].l<L[i].r)update(L[i].l,L[i].r,L[i].s,0,50000,1);            }else{                ans += sum[1]*(L[i].h-L[i-1].h);                if(L[i].l<L[i].r)update(L[i].l,L[i].r,L[i].s,0,50000,1);            }        }        printf("%u\n",ans);    }    return 0;}


 

 

 

原创粉丝点击