HDU1828线段树(扫描线)

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.

HDU1828线段树(扫描线)

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

HDU1828线段树(扫描线)

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
 

题意:

给出n个矩形的右下和左上角的坐标,问这n个矩形组成的图形的边长

代码:

参考博客

http://blog.csdn.net/u013480600/article/details/22548393

http://blog.csdn.net/u012860063/article/details/43163949

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=;
int cnt[maxn*],segnum[maxn*],sum[maxn*],pre[maxn*],suf[maxn*];
struct node{
int l,r,h,d;
node(){}
node(int a,int b,int c,int d):l(a),r(b),h(c),d(d){}
bool operator < (node&p){
if(h==p.h) return d>p.d;
return h<p.h;
}
}nodes[maxn];
void pushup(int l,int r,int rt){
if(cnt[rt]){//是下底边
segnum[rt]=;
pre[rt]=suf[rt]=;
sum[rt]=r-l+;
}
else if(l==r)//是叶子
segnum[rt]=sum[rt]=pre[rt]=suf[rt]=;
else{
segnum[rt]=segnum[rt<<]+segnum[rt<<|];
if(suf[rt<<]&&pre[rt<<|]) segnum[rt]-=;//有竖边重合
sum[rt]=sum[rt<<]+sum[rt<<|];
pre[rt]=pre[rt<<];//用两个儿子的两头更新父亲的两头
suf[rt]=suf[rt<<|];
}
}
void build(int l,int r,int rt){//本题中可以不用build递归,直接memset数组就行
cnt[rt]=;suf[rt]=;pre[rt]=;segnum[rt]=;sum[rt]=;
if(l==r) return;
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
}
void update(int L,int R,int c,int l,int r,int rt){
if(L<=l&&R>=r){
cnt[rt]+=c;
pushup(l,r,rt);
return;
}
int m=(l+r)>>;
if(L<=m) update(L,R,c,l,m,rt<<);
if(R>m) update(L,R,c,m+,r,rt<<|);
pushup(l,r,rt);
}
int main()
{
int t,x1,x2,y1,y2;
while(scanf("%d",&t)==){
int lbd=-,rbd=,m=;
while(t--){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
lbd=min(lbd,x1);
rbd=max(rbd,x2);
nodes[m++]=node(x1,x2,y1,);
nodes[m++]=node(x1,x2,y2,-);
}
build(lbd,rbd-,);
sort(nodes,nodes+m);
int ans=,last=;
for(int i=;i<m;i++){
if(nodes[i].l<=nodes[i].r-) //这个判断要有!
update(nodes[i].l,nodes[i].r-,nodes[i].d,lbd,rbd-,);
ans+=abs(sum[]-last);//算横向边
last=sum[];//更新前一个边
if(i!=m-)//算纵向边
ans+=segnum[]*(nodes[i+].h-nodes[i].h);
}
printf("%d\n",ans);
}
return ;
}
上一篇:RelativeLayout用代码兑现布局


下一篇:TableInputFormat分片及分片数据读取源码级分析