Picture(hdu1828,求周长并,线段树+离散化+扫描)

/

/http://acm.hdu.edu.cn/showproblem.php?pid=1828

http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1003&ojid=0&cid=5361&hide=0

Picture

Time Limit : 6000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)

Total Submission(s) : 2   Accepted Submission(s) : 1

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

IOI 1998

 

Statistic | Submit | Back

线段树+扫描线;

如果坐标是double 类型的话需要将其变离散化

总体思想:看了诸多大神的博客之后才有一点点感觉

1,将每个矩形用两条线段表示,并且这些线段按照x坐标从小到大顺序排列好,这样就把一个个矩形变成了一条条线段

2.y坐标存入一组数据中,并且按照从小到大顺序排序之后,构建树状数组;

3,最重要的就是对树状数组进行维护和查询工作,主要是维护,覆盖次数和有效长度

这是难点也是重点

Submit Time Judge Status Exe.Time Exe.Memory Language

2013-07-27 15:46:44 Accepted 15 MS 1404 KB GNU C++

解析:

求周长并:

线段树+离散话+扫描;

下面是搬了模板的

*/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include <iostream>
using namespace std;
const int maxn=5000+10;
const int inf=100000000;
struct node
{
int s,t,m,lb,rb;//m表示有效投影长度
int sl;
int cnt;//表示被覆盖次数
}tr[maxn*8];
struct Line
{
int x,y1,y2;
bool d;//标记矩形的左右边
}line[maxn*2];
void build(int k,int s,int t)//建树
{
tr[k].s=s;
tr[k].t=t;
tr[k].m=tr[k].lb=tr[k].rb=0;
tr[k].sl=tr[k].cnt=0;//附加值初始化为0
if(t-s>1)
{
int mid=(s+t)/2;
build(k*2+1,s,mid);
build(k*2+2,mid,t);
}
}
bool cmp(Line a,Line b)//为了便于将坐标从左到右排列
{
return a.x<b.x;
}
void update(int k).//更新结点k
{
if(tr[k].cnt>0)//当存在覆盖时
 {
 	tr[k].m=tr[k].t-tr[k].s;
 	tr[k].lb=tr[k].rb=1;
 	tr[k].sl=1;
 	return;
 }
 if(tr[k].t-tr[k].s==1)//到达叶子结点时清0
 {
 	tr[k].m=0;
 	tr[k].lb=tr[k].rb=0;
 	tr[k].sl=0;
 }
 else
 {//当前结果为左右子树之和
 	int ll=k*2+1;
 	int rr=k*2+2;
 	tr[k].m=tr[ll].m+tr[rr].m;
tr[k].sl=tr[ll].sl+tr[rr].sl-(tr[ll].rb&tr[rr].lb);
 	tr[k].lb=tr[ll].lb;
 	tr[k].rb=tr[rr].rb;
 }
}
void insert(int s,int t,int k)
{
if(s<=tr[k].s&&t>=tr[k].t)//当区间被覆盖时
{
tr[k].cnt++;
update(k);//一旦发生变化需要进行维护
return;
}
int mid=(tr[k].s+tr[k].t)/2;
if(s<mid)insert(s,t,k*2+1);
if(t>mid)insert(s,t,k*2+2);
update(k);
}
void Delete(int s,int t,int k)
{
if(s<=tr[k].s&&t>=tr[k].t)//同插入操作
{
tr[k].cnt--;
update(k);
return;
}
int mid=(tr[k].s+tr[k].t)/2;
if(s<mid)Delete(s,t,k*2+1);
if(t>mid)Delete(s,t,k*2+2);
update(k);
}
void cal(int n)//求周长并
{
int i,t2,sum=0;
t2=0;
line[n]=line[n-1];
for(i=0;i<n;i++)
{
if(line[i].d==1)//右边时加入
insert(line[i].y1,line[i].y2,0);
else//当属于矩形左边点时删除
Delete(line[i].y1,line[i].y2,0);
sum+=tr[0].sl*(line[i+1].x-line[i].x)*2;
sum+=abs(tr[0].m-t2);
t2=tr[0].m;
}
printf("%d\n",sum);
}
int main()
{
int i,j,n;
while(scanf("%d",&n)!=EOF)
{ int x1,x2,y1,y2;
j=0;
int max=-inf,min=inf;
for(i=0;i<n;i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line[j].x=x1;//线段的坐标与线段树每个结点的区间范围相对应
line[j].y1=y1;
line[j].y2=y2;
line[j].d=1;
line[++j].x=x2;
line[j].y1=y1;
line[j].y2=y2;
line[j].d=0;
j++;
if(min>y1)min=y1;
if(max<y2)max=y2;
}
sort(line,line+j,cmp);
build(0,min,max);
cal(j);
}
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值