hdu 1828 Picture 线段树+扫描线

Picture

Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12 Accepted Submission(s): 10
 
Problem Description
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter. 

Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1. 



The corresponding boundary is the whole set of line segments drawn in Figure 2. 



The vertices of all rectangles have integer coordinates.
 
Input
Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate. 

0 <= number of rectangles < 5000 
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.

Please process to the end of file.
 
Output
Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.
 
Sample Input
7
-15 0 5 10
-5 8 20 25
15 -4 24 14
0 -6 16 4
2 15 10 22
30 10 36 20
34 0 40 16
 
Sample Output
228
 
大值题意:

给出几个矩形的左下点,和右上点。
求这些矩形轮廓的周长。

大致思路:

这个要用到扫描线,还木有接触过。
过几天 要整理一下 扫描线的内容。

ac代码:
#include<bits/stdc++.h>
#define INF 99999999  
using namespace std;  
  
const int MAX=20000+10;  
int sum[MAX<<2];//统计下底边总长度  
int segnum[MAX<<2];//统计没有被覆盖的竖边个数  
bool lseg[MAX<<2],rseg[MAX<<2];//表示某个区间最左边和最右边是否有下底边多于上底边  
int mark[MAX<<2];//表示下底边比上底边多的个数   
  
struct seg{  
    int l,r,h,d;  
    seg(){}  
    seg(int x1,int x2,int H,int c):l(x1),r(x2),h(H),d(c){}  
    bool operator <(const seg&a)const{  
        if(h == a.h)return d>a.d;//这里一定不能去掉,比如这组数据:2 0 0 4 4 0 4 4 8  
        return h<a.h;  
    }  
}s[MAX];  
  
void Upfather(int n,int left,int right){  
    if(mark[n]){  
        sum[n]=right-left+1;  
        lseg[n]=rseg[n]=true;  
        segnum[n]=2;//该区间被一条底边全部覆盖,可用竖边为2   
    }  
    else if(left == right)sum[n]=lseg[n]=rseg[n]=segnum[n]=0;  
    else{  
        sum[n]=sum[n<<1]+sum[n<<1|1];  
        segnum[n]=segnum[n<<1]+segnum[n<<1|1];  
        lseg[n]=lseg[n<<1];  
        rseg[n]=rseg[n<<1|1];  
        if(rseg[n<<1] && lseg[n<<1|1])segnum[n]-=2;//两个矩形相交(底边区域有重合)则减少两条竖边   
    }  
}  
  
void Update(int L,int R,int d,int n,int left,int right){  
    if(L<=left && right<=R){  
        mark[n]+=d;  
        Upfather(n,left,right);  
        return;  
    }  
    int mid=left+right>>1;  
    if(L<=mid)Update(L,R,d,n<<1,left,mid);  
    if(R>mid)Update(L,R,d,n<<1|1,mid+1,right);  
    Upfather(n,left,right);  
}  
  
int main(){  
    int n,x1,x2,y1,y2;  
    while(cin>>n){  
        int k=0,left=INF,right=-INF;  
        for(int i=0;i<n;++i){  
            cin>>x1>>y1>>x2>>y2;  
            s[k++]=seg(x1,x2,y1,1);  
            s[k++]=seg(x1,x2,y2,-1);  
            left=min(left,x1);  
            right=max(right,x2);  
        }  
        sort(s,s+k);  
        int ans=0,last=0;  
        for(int i=0;i<k;++i){  
            if(s[i].l<s[i].r)Update(s[i].l,s[i].r-1,s[i].d,1,left,right);  
            ans+=segnum[1]*(s[i+1].h-s[i].h);//竖边增加的总长  
            ans+=abs(sum[1]-last);//底边增加的总长  
            last=sum[1];   
        }  
        printf("%d\n",ans);  
    }  
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值