题目链接:POJ 1177 Picture
题目
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.
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
解析
扫描线裸题。首先,因为都是矩形,所以计算周长的时候只需要计算左边的边,然后*2即可。
思路还是用两条边来替换一个矩形,且权值分别为1和-1,然后线段树维护区间被覆盖的次数
c
n
t
cnt
cnt和覆盖的总长度
l
e
n
len
len。
一个直观的感受是符合周长的边是从线段树上的一些区间从无到有,即本来值为0的数组后来被覆盖了。直接进行这一统计其实有一丢丢难,我们可以转换一下,每次计算加入一条边后被覆盖区间增加的长度,最后横着扫一遍,竖着扫一遍再乘二即可。
注意一个小细节:有可能横坐标相等的两条边权值相反,且他们覆盖的区间有某一子区间本来是被覆盖了一次。那么这两条边其实对周长是没有贡献的,但是更新不当极其容易把该子区间算作边长。解决方法其实很简单:只要排序的时候对于横坐标相同的边按权值由大到小排序即可,纵向同理。
AC代码:
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <functional>
#include <cstring>
using namespace std;
const int maxn = 5010;
int sx[maxn*2], sy[maxn*2];
struct node{
int cnt, len;
} tree[maxn*16];
struct lne{
int x, d, u;
int val;
bool operator < (const lne &oth) const{
if(x == oth.x) return val > oth.val;
else return x < oth.x;
}
} l[maxn*2];
struct vec{
int x1, x2, y1, y2;
} v[maxn];
void build(int ll, int r, int dex, int *m)
{
tree[dex].len = tree[dex].cnt = 0;
if(ll != r){
int mid = (ll+r) >> 1;
build(ll, mid, dex*2, m), build(mid+1, r, dex*2+1, m);
}
}
void update(int ll, int r, int dex, int x, int y, int val, int *m)
{
if(ll > y || r < x) return;
else if(ll >= x && r <= y) tree[dex].cnt += val;
else{
int mid = (ll+r) >> 1;
update(ll, mid, dex*2, x, y, val, m), update(mid+1, r, dex*2+1, x, y, val, m);
tree[dex].len = tree[dex*2].len + tree[dex*2+1].len;
}
if(tree[dex].cnt > 0) tree[dex].len = m[r+1] - m[ll];
else tree[dex].len = (ll==r?0:tree[dex*2].len+tree[dex*2+1].len);
}
int main()
{
int res = 0, pre = 0;
int n, x1, x2, y1, y2;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d%d%d%d", &v[i].x1, &v[i].y1, &v[i].x2, &v[i].y2),
sx[i] = v[i].x1, sx[i+n] = v[i].x2, sy[i] = v[i].y1, sy[i+n] = v[i].y2;
sort(sx+1, sx+1+2*n), sort(sy+1, sy+1+n*2);
int xx = unique(sx+1, sx+1+2*n) - sx-1, yy = unique(sy+1, sy+1+2*n) - sy-1;
for(int i = 1; i <= n; i++)
v[i].x1 = lower_bound(sx+1, sx+1+xx, v[i].x1) - sx,
v[i].x2 = lower_bound(sx+1, sx+1+xx, v[i].x2) - sx,
v[i].y1 = lower_bound(sy+1, sy+1+yy, v[i].y1) - sy,
v[i].y2 = lower_bound(sy+1, sy+1+yy, v[i].y2) - sy;
for(int i = 1; i <= n; i++){
l[i].x = v[i].x1, l[i].d = v[i].y1, l[i].u = v[i].y2;
l[i+n] = l[i], l[i+n].x = v[i].x2;
l[i].val = 1, l[i+n].val = -1;
}
sort(l+1, l+1+2*n);
build(1, yy-1, 1, sy);
for(int i = 1; i <= 2*n; i++){
update(1, yy-1, 1, l[i].d, l[i].u-1, l[i].val, sy);
res += max(0, tree[1].len-pre);
pre = tree[1].len;
}
pre = 0;
for(int i = 1; i <= n; i++){
l[i].x = v[i].y1, l[i].d = v[i].x1, l[i].u = v[i].x2;
l[i+n] = l[i], l[i+n].x = v[i].y2;
l[i].val = 1, l[i+n].val = -1;
}
sort(l+1, l+1+n*2);
build(1, xx-1, 1, sx);
for(int i = 1; i <= 2*n; i++){
update(1, xx-1, 1, l[i].d, l[i].u-1, l[i].val, sx);
res += max(0, tree[1].len - pre);
pre = tree[1].len;
}
printf("%d\n", res*2);
}