HDOJ 题目1828 Picture (线段树+扫描线)

Picture

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3646    Accepted Submission(s): 1884


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
 

Source
 

Recommend
linle   |   We have carefully selected several similar problems for you:   2871  1823  3016  1543  3458 

 求矩形周长并

ac代码

#include<stdio.h>   
#include<stdlib.h>   
#include<string.h>   
#include<algorithm> 
#include<math.h>
#define N 25010  
#define INF 0x3f3f3f3f
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
using namespace std;  
struct s  
{  
    double x1,x2,y;  
    int flag;  
}a[N<<2];  
double __hash[N<<2],sum[N<<2];  
int col[N<<2],lb[N<<2],rb[N<<2],num[N<<2];  
int cmp(s a,s b)  
{  
    return a.y<b.y;  
}  
int bseach(double key,int n)  
{  
    int l=1,r=n;  
    while(l<=r)  
    {  
        int mid=(l+r)>>1;  
        if(__hash[mid]==key)  
            return mid;  
        if(__hash[mid]<key)  
            l=mid+1;  
        else  
            r=mid-1;  
    }  
    return l;  
}
void build(int l,int r,int tr)
{
	
}  
void pushup(int tr,int l,int r)  
{  
    if(col[tr])  
	{
        sum[tr]=__hash[r+1]-__hash[l];
		lb[tr]=rb[tr]=1;
		num[tr]=2;
	}
    else  
	{
        if(l==r)  
		{
            sum[tr]=0;
			lb[tr]=rb[tr]=0;
			num[tr]=0;
		}
        else
		{
            sum[tr]=sum[tr<<1]+sum[tr<<1|1];
			lb[tr]=lb[tr<<1];
			rb[tr]=rb[tr<<1|1];
			num[tr]=num[tr<<1]+num[tr<<1|1];
			if(lb[tr<<1|1]&&rb[tr<<1])
				num[tr]-=2;
		}
	}
}  
void update(int L,int R,int l,int r,int tr,int flag)  
{  
    if(L<=l&&r<=R)  
    {  
        col[tr]+=flag;  
        pushup(tr,l,r);  
        return;  
    }  
    int mid=(l+r)>>1;  
    if(L<=mid)  
        update(L,R,l,mid,tr<<1,flag);  
    if(R>mid)  
        update(L,R,mid+1,r,tr<<1|1,flag);  
    pushup(tr,l,r);  
}  
int main()  
{  
    int n;  
    while(scanf("%d",&n)!=EOF)  
    {   
        double x1,x2,y1,y2,lx=INF,rx=-INF;  
        int i,k=1;  
        for(i=1;i<=n;i++)  
        {  
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);  
            a[k].x1=x1;  
            a[k].x2=x2;  
            a[k].y=y1;  
            a[k].flag=1;  
            __hash[k++]=x1;  
            a[k].x1=x1;  
            a[k].x2=x2;  
            a[k].y=y2;  
            a[k].flag=-1;  
            __hash[k++]=x2;  
        }  
        sort(a+1,a+k,cmp);  
        sort(__hash+1,__hash+k); 
		memset(sum,0,sizeof(sum));
		memset(col,0,sizeof(col));
		memset(lb,0,sizeof(lb));
		memset(rb,0,sizeof(rb));
		memset(num,0,sizeof(num));
        double ans=0,last=0;  
        for(i=1;i<=k-1;i++)  
        {  
            int x,y;  
            x=bseach(a[i].x1,k-1);  
            y=bseach(a[i].x2,k-1)-1;  
            if(x<=y)  
                update(x,y,1,k-1,1,a[i].flag);  
            ans+=num[1]*(a[i+1].y-a[i].y);
			ans+=fabs(sum[1]-last);
			last=sum[1];
        }  
        printf("%.lf\n",ans);  
    }  
}  


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值