码迷,mamicode.com
首页 > 其他好文 > 详细

HDU3265 线段树(扫描线)

时间:2017-02-16 13:27:28      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:number   contains   paste   table   tail   ble   lines   osi   and   

Posters

Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 14 Accepted Submission(s): 6
 
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
2009 Asia Ningbo Regional Contest Hosted by NIT
 

题意:

用n个”回“字形的纸贴窗子,问覆盖面积。

代码:

//把回字拆成4段再计算就行了,要用longlong
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=50004;
int cnt[maxn*8],sum[maxn*8];
struct node{
    int l,r,h,d;
    node(){}
    node(int a,int b,int c,int d):l(a),r(b),h(c),d(d){}
    bool operator < (const node&p)const{
        if(h==p.h) return d>p.d;
        return h<p.h;
    }
}nodes[maxn*8];
void pushup(int l,int r,int rt){
    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 build(int l,int r,int rt){
    cnt[rt]=sum[rt]=0;
    if(l==r) return;
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,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(l,r,rt);
        return;
    }
    int m=(l+r)>>1;
    if(L<=m) update(L,R,c,l,m,rt<<1);
    if(R>m) update(L,R,c,m+1,r,rt<<1|1);
    pushup(l,r,rt);

}
int main()
{
    int t,x1,x2,x3,x4,y1,y2,y3,y4;
    while(scanf("%d",&t)&&t){
        int m=0,lbd=50000,rbd=0;
        while(t--){
            scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
            nodes[m++]=node(x1,x2,y1,1);
            nodes[m++]=node(x1,x2,y3,-1);
            nodes[m++]=node(x1,x2,y4,1);
            nodes[m++]=node(x1,x2,y2,-1);
            nodes[m++]=node(x1,x3,y3,1);
            nodes[m++]=node(x1,x3,y4,-1);
            nodes[m++]=node(x4,x2,y3,1);
            nodes[m++]=node(x4,x2,y4,-1);
            lbd=min(lbd,x1);
            rbd=max(rbd,x2);
        }
        build(lbd,rbd-1,1);
        sort(nodes,nodes+m);
        long long ans=0;
        for(int i=0;i<m-1;i++){
            if(nodes[i].l<=nodes[i].r-1)
            update(nodes[i].l,nodes[i].r-1,nodes[i].d,lbd,rbd-1,1);
            ans+=(long long)sum[1]*(nodes[i+1].h-nodes[i].h);//注意 long long
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

 

HDU3265 线段树(扫描线)

标签:number   contains   paste   table   tail   ble   lines   osi   and   

原文地址:http://www.cnblogs.com/--ZHIYUAN/p/6404732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!