您的位置:首页 > 其它

hdu 1828 Picture(线段树 || 普通hash标记)

2013-08-01 10:54 423 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1828

Picture

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

[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

228

[align=left]Source[/align]
IOI 1998

【题解1】:
普通哈市标记:
分别按x,y方向用以下结构体构造线段:
struct Line
{
int s,t; //存始末位置
int x;
int flag; //标记是否是起始边 1 -1
}linex[N<<1],liney[N<<1]; //分别安x与y构造线段
int visit[N<<2]; //判断是否访问过
再分别按x,y方向遍历标记

如下图:
按x方向的遍历线段1,2,3,4



【代码1】:

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#define N 5010
using namespace std;

struct Line
{
int s,t;   //存始末位置
int x;
int flag;  //标记是否是起始边 1  -1
}linex[N<<1],liney[N<<1];  //分别安x与y构造线段

int visit[N<<2];  //判断是否访问过

bool cmp(Line a,Line b)  //sort排序用
{
return a.x<b.x;
}

int cal_x(int n)  //统计按x排序的平行于y的线段长
{
int i,j,cnt=0;
for(i=1;i<=n;i++)
{
if(linex[i].flag == 1)  //如果是起始边
{
for(j=linex[i].s;j<linex[i].t;j++)
{
if(visit[j]==0) cnt++;  //起始边,没访问过cnt++
visit[j]++;  //标记+1
}
}
else  //如果是结束边
{
for(j=linex[i].s;j<linex[i].t;j++)
{
if(visit[j]==1) cnt++;  //结束边,访问过cnt++
visit[j]--;  //标记-1,等待下一个起始边的进入
}
}
}
return cnt;
}

int cal_y(int n)  //同上,统计按y排序的平行于x的线段长
{
int i,j,cnt=0;
for(i=1;i<=n;i++)
{
if(liney[i].flag == 1)
{
for(j=liney[i].s;j<liney[i].t;j++)
{
if(visit[j]==0) cnt++;
visit[j]++;
}
}
else
{
for(j=liney[i].s;j<liney[i].t;j++)
{
if(visit[j]==1) cnt++;
visit[j]--;
}
}
}
return cnt;
}

int main()
{
int n;
while(~scanf("%d",&n))
{
int i,cnt=0;
for(i=0;i<n;i++)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1+=N<<1;
x2+=N<<1;
y1+=N<<1;
y2+=N<<1;   //平移N<<1,排除负数情况

cnt++;
linex[cnt].x = x1;
linex[cnt].s = y1;
linex[cnt].t = y2;
linex[cnt].flag = 1;

liney[cnt].x = y1;
liney[cnt].s = x1;
liney[cnt].t = x2;
liney[cnt].flag = 1;

cnt++;
linex[cnt].x = x2;
linex[cnt].s = y1;
linex[cnt].t = y2;
linex[cnt].flag = -1;

liney[cnt].x = y2;
liney[cnt].s = x1;
liney[cnt].t = x2;
liney[cnt].flag = -1;
}

sort(linex+1,linex+1+cnt,cmp);  //线段按x进行排序
sort(liney+1,liney+1+cnt,cmp);  //线段按y进行排序

int ans = 0;
memset(visit,0,sizeof(visit));
ans += cal_x(cnt);  //统计x
memset(visit,0,sizeof(visit));
ans += cal_y(cnt);  //统计y

printf("%d\n",ans);
}
return 0;
}


【题解2】:线段树

线段树代码是参照别人的

【code】:

#include<iostream>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<cmath>
using namespace std;

struct node
{
int l;
int r;
int len;   //该区间可与下一个将要插入的线段组成并面积的长度
int cover; //该区间覆盖了几根线段
int num;   //记录该区间由几个子区间组成了可与下一个将要插入的线段组成并面积,即可并子区间的数目
int r_cover; //记录该区间右节点是否被可并区间覆盖
int l_cover; //。。。。。坐。。。。。。。。。
};

node tree[100000];
int n;
int yy[10004],len;

struct Line
{
int l;
int r;
int x;
int cover;
};

Line line[10004];

int cmp(Line a,Line b)
{
return a.x<b.x;
}

int find(int x)
{
int l=0,r=len,mid;
while(l<=r)
{
mid=(l+r)/2;
if(yy[mid]==x)
return mid;
if(yy[mid]<x)
l=mid+1;
else
r=mid-1;
}
return l;
}

void build(int i,int l,int r)
{
tree[i].l=l;
tree[i].r=r;
tree[i].cover=tree[i].l_cover=tree[i].r_cover=tree[i].len=tree[i].num=0;
if(l+1==r)
return;
int mid=(l+r)/2;
build(2*i,l,mid);
build(2*i+1,mid,r);
}

void fun(int i)
{
if(tree[i].cover) //整个被覆盖
{
tree[i].len=yy[tree[i].r]-yy[tree[i].l]; //可用长度为整个区间
tree[i].l_cover=tree[i].r_cover=1; //左右节点都被覆盖了
tree[i].num=1;  //由一个区间组成,即该区间
}
else if(tree[i].l+1==tree[i].r) //叶子区间
{
tree[i].len=0;
tree[i].l_cover=tree[i].r_cover=0;
tree[i].num=0;
}
else
{
tree[i].len=tree[2*i].len+tree[2*i+1].len; //dp
tree[i].l_cover=tree[2*i].l_cover; //左节点是否覆盖,取决于左子区间的左节点是否被覆盖
tree[i].r_cover=tree[2*i+1].r_cover; //同理
tree[i].num=tree[2*i].num+tree[2*i+1].num-tree[2*i].r_cover*tree[2*i+1].l_cover; //该线段可用长度的区间组成数等于左右子区间的组成数
}                                                                                    //但是,当左右子区间是连续的时候,结果要减一
}

void updata(int i,int l,int r,int w)
{
if(tree[i].l>r || tree[i].r<l)
return;
if(tree[i].l>=l && tree[i].r<=r)
{
tree[i].cover+=w;
fun(i);
return;
}
updata(2*i,l,r,w);
updata(2*i+1,l,r,w);
fun(i);
}

int main()
{
int i,x1,x2,y1,y2,m,a,b;
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
m=0;
for(i=0;i<n;i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
yy[m]=y1;
line[m].cover=1;
line[m].x=x1;
line[m].l=y1;
line[m++].r=y2;

yy[m]=y2;
line[m].cover=-1;
line[m].x=x2;
line[m].l=y1;
line[m++].r=y2;
}
sort(yy,yy+m);
sort(line,line+m,cmp);
len=1;
for(i=1;i<m;i++)  //?
{
if(yy[i-1]!=yy[i])
yy[len++]=yy[i];
}
len--;
build(1,0,len);
int ans=0,pre=0;
for(i=0;i<m;i++)
{
a=find(line[i].l);
b=find(line[i].r);
updata(1,a,b,line[i].cover);
ans+=abs((tree[1].len-pre)); //加上y坐标上的周长
if(i==m-1)
break;
pre=tree[1].len;
ans+=tree[1].num*(line[i+1].x-line[i].x)*2;//加上x坐标上的周长,因为一个y边连着两个x边,所以乘二
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: