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
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.
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
0 <= number of rectangles < 5000
All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.
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 16Sample Output
228
线段树经典题目,借鉴了大牛的思路,用自己喜欢的书写风格改写,谨记录于此,防忘记。
Code:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=10000+5;
struct TreeNode{
int l,r;
int count;
int line;
int lbd,rbd;
int m;
}tr[maxn<<2];
struct Scanline{
int x;
int y1,y2;
int flag;
}scan[maxn];
int y[maxn];
bool cmp(struct Scanline a,struct Scanline b){
if(a.x==b.x) return a.flag>b.flag;
return a.x<b.x;
}
void build(int l,int r,int i){
tr[i].l=l;
tr[i].r=r;
tr[i].count=0;
tr[i].m=0;
tr[i].line=0;
if(r-l>1){
int middle=(l+r)/2;
build(l,middle,i<<1);
build(middle, r, i<<1|1);
}
}
void update_m(int i){
if(tr[i].count>0)
tr[i].m=y[tr[i].r]-y[tr[i].l];
else if(tr[i].r-tr[i].l==1)
tr[i].m=0;
else{
tr[i].m=tr[i<<1].m+tr[i<<1|1].m;
}
}
void update_line(int i){
if(tr[i].count>0){
tr[i].lbd=tr[i].rbd=tr[i].line=1;
}
else if(tr[i].r-tr[i].l==1){
tr[i].lbd=tr[i].rbd=tr[i].line=0;
}
else{
tr[i].lbd=tr[i<<1].lbd;
tr[i].rbd=tr[i<<1|1].rbd;
tr[i].line=tr[i<<1].line+tr[i<<1|1].line-tr[i<<1].rbd*tr[i<<1|1].lbd;
}
}
void insert(int L,int R,int v,int i){
if(L<=y[tr[i].l] && y[tr[i].r]<=R)
tr[i].count+=v;
else if(tr[i].r-tr[i].l==1)
return;
else{
int middle=(tr[i].l+tr[i].r)>>1;
if(R<=y[middle])
insert(L, R, v, i<<1);
else if(L>=y[middle])
insert(L, R, v, i<<1|1);
else{
insert(L, y[middle], v, i<<1);
insert(y[middle], R, v, i<<1|1);
}
}
update_m(i);
update_line(i);
}
int main(){
int n;
scanf("%d",&n);
int x1,y1,x2,y2;
int i=0;
while(n--){
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
scan[i].x = x1;
scan[i].y1 = y1;
scan[i].y2 = y2;
scan[i].flag = 1;
y[i++] = y1;
scan[i].x = x2;
scan[i].y1 = y1;
scan[i].y2 = y2;
scan[i].flag = -1;
y[i++] = y2;
}
sort(y,y+i);
sort(scan,scan+i,cmp);
int unique_count = unique(y, y + i) - y;
build(0,unique_count-1,1);
int perimeter=0;
int now_m=0;
int now_line=0;
for(int j=0;j<i;j++){
insert(scan[j].y1, scan[j].y2, scan[j].flag, 1);
if(j>=1){
perimeter+=2*now_line*(scan[j].x-scan[j-1].x);
}
perimeter+=abs(tr[1].m-now_m);
now_m=tr[1].m;
now_line=tr[1].line;
}
printf("%d\n", perimeter);
return 0;
}