/
/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 <&#61; 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
线段树&#43;扫描线&#xff1b;
如果坐标是double 类型的话需要将其变离散化
总体思想&#xff1a;看了诸多大神的博客之后才有一点点感觉
1&#xff0c;将每个矩形用两条线段表示&#xff0c;并且这些线段按照x坐标从小到大顺序排列好&#xff0c;这样就把一个个矩形变成了一条条“线段”
2.将y坐标存入一组数据中&#xff0c;并且按照从小到大顺序排序之后&#xff0c;构建树状数组&#xff1b;
3&#xff0c;最重要的就是对树状数组进行维护和查询工作&#xff0c;主要是维护&#xff0c;覆盖次数和有效长度
这是难点也是重点
Submit Time Judge Status Exe.Time Exe.Memory Language
2013-07-27 15:46:44 Accepted 15 MS 1404 KB GNU C&#43;&#43;
解析&#xff1a;
求周长并&#xff1a;
线段树&#43;离散话&#43;扫描&#xff1b;
下面是搬了模板的
*/
#include
#include
#include
#include
#include
using namespace std;
const int maxn&#61;5000&#43;10;
const int inf&#61;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&#61;s;
tr[k].t&#61;t;
tr[k].m&#61;tr[k].lb&#61;tr[k].rb&#61;0;
tr[k].sl&#61;tr[k].cnt&#61;0;//附加值初始化为0
if(t-s>1)
{
int mid&#61;(s&#43;t)/2;
build(k*2&#43;1,s,mid);
build(k*2&#43;2,mid,t);
}
}
bool cmp(Line a,Line b)//为了便于将坐标从左到右排列
{
return a.x}
void update(int k).//更新结点k
{
if(tr[k].cnt>0)//当存在覆盖时{tr[k].m&#61;tr[k].t-tr[k].s;tr[k].lb&#61;tr[k].rb&#61;1;tr[k].sl&#61;1;return;}if(tr[k].t-tr[k].s&#61;&#61;1)//到达叶子结点时清0{tr[k].m&#61;0;tr[k].lb&#61;tr[k].rb&#61;0;tr[k].sl&#61;0;}else{//当前结果为左右子树之和int ll&#61;k*2&#43;1;int rr&#61;k*2&#43;2;tr[k].m&#61;tr[ll].m&#43;tr[rr].m;
tr[k].sl&#61;tr[ll].sl&#43;tr[rr].sl-(tr[ll].rb&tr[rr].lb);tr[k].lb&#61;tr[ll].lb;tr[k].rb&#61;tr[rr].rb;}
}
void insert(int s,int t,int k)
{
if(s<&#61;tr[k].s&&t>&#61;tr[k].t)//当区间被覆盖时
{
tr[k].cnt&#43;&#43;;
update(k);//一旦发生变化需要进行维护
return;
}
int mid&#61;(tr[k].s&#43;tr[k].t)/2;
if(sif(t>mid)insert(s,t,k*2&#43;2);
update(k);
}
void Delete(int s,int t,int k)
{
if(s<&#61;tr[k].s&&t>&#61;tr[k].t)//同插入操作
{
tr[k].cnt--;
update(k);
return;
}
int mid&#61;(tr[k].s&#43;tr[k].t)/2;
if(sif(t>mid)Delete(s,t,k*2&#43;2);
update(k);
}
void cal(int n)//求周长并
{
int i,t2,sum&#61;0;
t2&#61;0;
line[n]&#61;line[n-1];
for(i&#61;0;i{
if(line[i].d&#61;&#61;1)//右边时加入
insert(line[i].y1,line[i].y2,0);
else//当属于矩形左边点时删除
Delete(line[i].y1,line[i].y2,0);
sum&#43;&#61;tr[0].sl*(line[i&#43;1].x-line[i].x)*2;
sum&#43;&#61;abs(tr[0].m-t2);
t2&#61;tr[0].m;
}
printf("%d\n",sum);
}
int main()
{
int i,j,n;
while(scanf("%d",&n)!&#61;EOF)
{ int x1,x2,y1,y2;
j&#61;0;
int max&#61;-inf,min&#61;inf;
for(i&#61;0;i{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line[j].x&#61;x1;//线段的坐标与线段树每个结点的区间范围相对应
line[j].y1&#61;y1;
line[j].y2&#61;y2;
line[j].d&#61;1;
line[&#43;&#43;j].x&#61;x2;
line[j].y1&#61;y1;
line[j].y2&#61;y2;
line[j].d&#61;0;
j&#43;&#43;;
if(min>y1)min&#61;y1;
if(max}
sort(line,line&#43;j,cmp);
build(0,min,max);
cal(j);
}
return 0;
}