A number, N (1 <= N < 5000), 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.
Figure 1 shows an example with seven rectangles:
Figure 1. A set of seven rectangles
The corresponding boundary is the whole set of line segments drawn in Figure 2:
Figure 2. The boundary of the set of rectangles
The vertices of all rectangles have integer coordinates. All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area. The numeric value of the result fits in a 32-bit signed representation.
MULTI TEST CASE!! Line 1: N, the number of rectangles pasted on the wall.
Lines 2..N+1 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.
A single line with a non-negative integer which corresponds to the perimeter for the input rectangles.
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
228
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define ABS(x) ((x)>=0?(x):-(x))
const int MAXN=5500;
struct TSegNode
{
int L,R,Lch,Rch,count,len;
TSegNode(int x,int y):L(x),R(y),Lch(-1),Rch(-1),count(0),len(0){}
TSegNode(){TSegNode(-1,-1);}
};
struct Tevent
{
int L,R,x;bool style;
friend const bool operator <(Tevent a,Tevent b)
{
if(a.x!=b.x) return a.x<b.x;
return (a.style&& !b.style);
}
};
int n,lx[MAXN],ly[MAXN],ux[MAXN],uy[MAXN],total,nevent,res;
TSegNode node[MAXN*4];
Tevent event[MAXN*4];
void CreatTree(int r)
{
if(node[r].R-node[r].L>1)
{
int mid=(node[r].R+node[r].L)>>1;
node[total]=TSegNode(node[r].L,mid);
node[r].Lch=total;CreatTree(total++);
node[total]=TSegNode(mid,node[r].R);
node[r].Rch=total;CreatTree(total++);
}
}
void update(int r,int L,int R,int v)
{
if(L>=node[r].R||R<=node[r].L) return ;
if(L<=node[r].L&&R>=node[r].R)
{
node[r].count+=v;
if(v>0&&v==node[r].count) node[r].len=node[r].R-node[r].L;
if(v<0&&node[r].count==0) if(node[r].Lch<0) node[r].len=0;
else node[r].len=node[node[r].Lch].len+node[node[r].Rch].len;
}
else
{
update(node[r].Lch,L,R,v);update(node[r].Rch,L,R,v);
if(node[r].count==0)
node[r].len=node[node[r].Lch].len+node[node[r].Rch].len;
}
}
void process()
{
int nlist,list[MAXN*2],last,i,now;
nevent=0,nlist=0;
for(i=0;i<n;i++)
{
event[nevent].x=lx[i];event[nevent].L=ly[i];
event[nevent].R=uy[i];event[nevent++].style=true;
event[nevent].x=ux[i];event[nevent].L=ly[i];
event[nevent].R=uy[i];event[nevent++].style=false;
list[nlist++]=ly[i];list[nlist++]=uy[i];
}
sort(event,event+nevent);
sort(list,list+nlist);
nlist=unique(list,list+nlist)-list;
node[total=0,total++]=TSegNode(0,nlist-1);
CreatTree(0);
for(i=0;i<total;i++)
{
node[i].L=list[node[i].L];node[i].R=list[node[i].R];
}
last=i=0;
while(i<nevent)
{
now=event[i].x;
while(i<nevent&&event[i].x==now&&event[i].style)
{
update(0,event[i].L,event[i].R,1);++i;
}
res+=ABS(node[0].len-last);last=node[0].len;
while(i<nevent&&event[i].x==now)
{
update(0,event[i].L,event[i].R,-1);++i;
}
res+=ABS(node[0].len-last);last=node[0].len;
}
}
int main()
{
while(scanf("%d",&n)==1)
{
for(int i=0;i<n;i++) scanf("%d%d%d%d",&lx[i],&ly[i],&ux[i],&uy[i]);
res=0;process();
for(int i=0;i<n;i++)
{
swap(lx[i],ly[i]);swap(ux[i],uy[i]);
}
process();
printf("%d\n",res);
}
return 0;
}