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.
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
扫描线经典应用:
扫描线+线段树通常用来求矩形周长并,面积并,面积。
周长并思路:
我们先计算横向线段的长度,把所有坐标的y轴坐标按照升序排列,扫描线从y的最小值依次向上扫描,求出每次扫描所覆盖的长度,与上一次长度做差得到其绝对值(这个值不需要加到线段树中去),加到最终的答案中去。横向纵向都来一遍就得出最终的答案了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int MAXN = 10020;
int n;
int dis[2][MAXN],sum[MAXN],cover[MAXN];
struct seg{
int l,r,h;
int cnt;
seg(){}
seg(int l,int r,int h,int cnt):l(l),r(r),h(h),cnt(cnt){}
friend bool operator < (const seg &p,const seg &q){
return p.h < q.h;
}
}s[2][MAXN];
void pushup(int rt,int l,int r,int flag)
{
if(cover[rt]) sum[rt] = dis[flag][r + 1] - dis[flag][l];
else if(l == r) sum[rt]= 0;
else sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void update(int rt,int l,int r,int ql,int qr,int val,int flag)
{
if(l >= ql && r <= qr){
cover[rt] += val;
pushup(rt,l,r,flag);
return ;
}
int mid = (l + r) >> 1;
if(ql <= mid) update(rt<<1,l,mid,ql,qr,val,flag);
if(qr > mid) update(rt<<1|1,mid + 1,r,ql,qr,val,flag);
pushup(rt,l,r,flag);
}
int main()
{
scanf("%d",&n);
int num[3];
int a,b,c,d;
for(int i = 1;i <= n;++i){
scanf("%d %d %d %d",&a,&b,&c,&d);
dis[0][i] = a;dis[0][i + n] = c;
dis[1][i] = b;dis[1][i + n] = d;
s[0][i] = seg(a,c,b,1);
s[0][i + n] = seg(a,c,d,-1);
s[1][i] = seg(b,d,a,1);
s[1][i + n] = seg(b,d,c,-1);
}
n *= 2;
sort(dis[0] + 1,dis[0] + n + 1);
num[0] = unique(dis[0] + 1,dis[0] + n + 1) - dis[0] - 1;
sort(dis[1] + 1,dis[1] + n + 1);
num[1] = unique(dis[1] + 1,dis[1] + n + 1) - dis[1] - 1;
sort(s[0] + 1,s[0] + n + 1);
sort(s[1] + 1,s[1] + n + 1);
int ans = 0;
for(int i = 0;i < 2;++i){
int lst = 0;
memset(sum,0,sizeof(sum));
memset(cover,0,sizeof(cover));
for(int j = 1;j <= n;++j){
int l = lower_bound(dis[i] + 1,dis[i] + num[i] + 1,s[i][j].l) - dis[i];
int r = lower_bound(dis[i] + 1,dis[i] + num[i] + 1,s[i][j].r) - dis[i];
update(1,1,num[i],l,r - 1,s[i][j].cnt,i);
ans += abs(sum[1] - lst);
lst = sum[1];
}
}
cout << ans << endl;
return 0;
}