题目:
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.
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.
解题思路:
首先要注意的是,如果在一个图形中有一片空白的部分,这篇面积的周长其实也是算的。!
然后这里和求面积的扫描线主要不同的地方就在于lb,rb的使用。详情看代码把~
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std;
const int MAX_N =1e5+9;
int y[MAX_N];
struct Line
{
int x,y1,y2;
int flag;
} line[MAX_N<<2];
struct Node
{
int l,r,cover,num; //num:现在有多少条线段
int lf,rf,len; // len:总长度
int lb,rb; // 代表左右端点有没有被覆盖
} node[MAX_N<<2];
bool cmp(const Line &A,const Line &B)
{
return A.x < B.x;
}
void build(int rt,int l,int r)
{
node[rt].l = l;
node[rt].r = r;
node[rt].lf = y[l];
node[rt].rf = y[r];
node[rt].len = node[rt].cover = 0;
node[rt].num = node[rt].lb = node[rt].rb = 0;
if(l+1 == r) return ;
int mid = (l+r) >> 1;
build(rt<<1, l, mid);
build(rt<<1|1, mid, r);
}
void push_up(int rt)
{
if(node[rt].cover > 0)
{
node[rt].len = node[rt].rf - node[rt].lf;
node[rt].num = node[rt].lb = node[rt].rb = 1;
return;
}
else if(node[rt].l+1 == node[rt].r)
{
node[rt].len = node[rt].lb = node[rt].rb = node[rt].num = 0;
return;
}
else
{
node[rt].len = node[rt<<1].len + node[rt<<1|1].len;
node[rt].lb=node[rt<<1].lb;
node[rt].rb=node[rt<<1|1].rb;
node[rt].num=node[rt<<1].num+node[rt<<1|1].num-(node[rt<<1].rb*node[rt<<1|1].lb);
}
}
void updata(int rt,Line t)
{
if(node[rt].lf == t.y1 && node[rt].rf == t.y2)
{
node[rt].cover += t.flag;
push_up(rt);
return ;
}
if(t.y1 >= node[rt<<1].rf)
{
updata(rt<<1|1, t);
}
else if(t.y2 <= node[rt<<1|1].lf)
{
updata(rt<<1, t);
}
else
{
Line temp = t;
temp.y2 = node[rt<<1|1].lf;
updata(rt<<1,temp);
temp = t;
temp.y1 = node[rt<<1].rf;
updata(rt<<1|1,temp);
}
push_up(rt);
}
int main()
{
int N,M,T=1;
while(cin>>N)
{
int cnt = 0;
for(int i=0; i<N; i++)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line[cnt].x = x1;
line[cnt].y1 = y1;
line[cnt].y2 = y2;
line[cnt].flag = 1;
y[cnt++] = y1;
line[cnt].x = x2;
line[cnt].y1 = y1;
line[cnt].y2 = y2;
line[cnt].flag = -1;
y[cnt++] = y2;
}
sort(line,line+cnt,cmp);
sort(y,y+cnt);
int cnt1 = unique(y,y+cnt) - y;
build(1,0,cnt1-1);
updata(1,line[0]);
int ans = 0;
int pre = 0;
int lines = 0;
for(int i=1; i<cnt; i++)
{
ans += fabs(node[1].len - pre);
ans += node[1].num*2*(line[i].x-line[i-1].x);
//cout<<ans<<"..."<<node[1].num<<endl;
pre = node[1].len;
updata(1,line[i]);
}
ans += fabs(pre);
printf("%d\n",ans);
}
return 0;
}