http://acm.hust.edu.cn/vjudge/contest/121640#problem/R
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
思路:
1,我们每次计算更新前后总的覆盖长度之差即为所有的长之和;
2,与面积并不同的是,我们还要记录一下区间竖线的条数,累加条数与相邻两条线段的高度之差即为所有的宽之和;
3,判断有几根竖线,可以增加变量rflag和lflag,标识区间端点是否被覆盖;
4,我们要维护的是一个左闭右开的区间!所以要注意在计算和更新区间的时候的端点取值;
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn=20090;
int cnt[maxn<<2],sum[maxn<<2],lflag[maxn<<2],rflag[maxn<<2],len[maxn<<2];
struct segment
{
int l,r,h,s;
segment(int a=0,int b=0,int c=0,int d=0):l(a),r(b),h(c),s(d){}
}seg[maxn<<2];
int cmp(segment a,segment b)
{
return a.h<b.h;
}
void pushup(int rt,int l,int r)
{
if(cnt[rt])
{
lflag[rt]=1,rflag[rt]=1;
sum[rt]=2;
len[rt]=r-l+1;
}
else if(l==r)
{
sum[rt]=rflag[rt]=lflag[rt]=len[rt]=0;
}
else
{
rflag[rt]=rflag[rt<<1|1],lflag[rt]=lflag[rt<<1];
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
len[rt]=len[rt<<1]+len[rt<<1|1];
if(lflag[rt<<1|1]==1&&rflag[rt<<1]==1)
sum[rt]-=2;
}
}
void update(int L,int R,int c,int l,int r,int rt)
{
if(L==l&&R==r)
{
cnt[rt]+=c;
pushup(rt,l,r);
return;
}
int m=l+r>>1;
if(m>=R) update(L,R,c,lson);
else if(m<L) update(L,R,c,rson);
else {
update(L,m,c,lson);
update(m+1,R,c,rson);
}
pushup(rt,l,r);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
int m=0,a,b,c,d;
int ml=10009,mr=-10009;
for(int i=0;i<n;i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
ml=min(ml,a);
mr=max(mr,c);
seg[m++]=segment(a,c,b,1);
seg[m++]=segment(a,c,d,-1);
}
sort(seg,seg+m,cmp);
int last=0,ans=0;
for(int i=0;i<m;i++)
{
update(seg[i].l,seg[i].r-1,seg[i].s,ml,mr-1,1);
ans+=sum[1]*(seg[i+1].h-seg[i].h);
ans+=abs(last-len[1]);
last=len[1];
}
printf("%d\n",ans);
}
return 0;
}