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
因为计算图形的周长,那么我可以用一颗树先横着扫一遍计算下底边的周长,再新建一颗树用来扫描并计算左右两边的长度,最后求一个和就可以了,计算边长时就是底边不用乘高度。
#include<map>
#include<stack>
#include<queue>
#include<string>
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define ls (k<<1)
#define rs (k<<1|1)
#define pb push_back
#define mid ((l+r)>>1)
//#include<bits/stdc++.h>
using namespace std;
const int p=1e4+7;
const int mod=1e9+7;
const int maxn=1e6+5;
typedef long long ll;
const int inf=0x3f3f3f3f;
ll l,r,n,x1,x2,y1,y2,ans,cnt,len,len1,last,x[maxn],y[maxn];
struct node
{
ll l,r,h,num;
}a[maxn],a1[maxn];
struct edge
{
ll len,tag;
}b[maxn],b1[maxn];
bool cmp(node a,node b)
{
return a.h<b.h;
}
void uphold(ll l,ll r,ll k)
{
if(b[k].tag)
b[k].len=x[r+1]-x[l];
else if(l==r)
b[k].len=0;
else
b[k].len=b[ls].len+b[rs].len;
}
void uphold1(ll l,ll r,ll k)
{
if(b1[k].tag)
b1[k].len=y[r+1]-y[l];
else if(l==r)
b1[k].len=0;
else
b1[k].len=b1[ls].len+b1[rs].len;
}
void update(ll l,ll r,ll l1,ll r1,ll v,ll k)
{
if(l>=l1&&r<=r1)
{
b[k].tag+=v;
uphold(l,r,k);
return ;
}
if(l1<=mid)
update(l,mid,l1,r1,v,ls);
if(r1>mid)
update(mid+1,r,l1,r1,v,rs);
uphold(l,r,k);
}
void update1(ll l,ll r,ll l1,ll r1,ll v,ll k)
{
if(l>=l1&&r<=r1)
{
b1[k].tag+=v;
uphold1(l,r,k);
return ;
}
if(l1<=mid)
update1(l,mid,l1,r1,v,ls);
if(r1>mid)
update1(mid+1,r,l1,r1,v,rs);
uphold1(l,r,k);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
while(cin>>n)
{
cnt=0;
for(int i=0;i<n;i++)
{
cin>>x1>>y1>>x2>>y2;
x[cnt]=x1;
a[cnt].l=x1;
a[cnt].r=x2;
a[cnt].h=y1;
y[cnt]=y1;
a1[cnt].l=y1;
a1[cnt].r=y2;
a1[cnt].h=x1;
a[cnt].num=1;
a1[cnt++].num=1;
x[cnt]=x2;
a[cnt].l=x1;
a[cnt].r=x2;
a[cnt].h=y2;
y[cnt]=y2;
a1[cnt].l=y1;
a1[cnt].r=y2;
a1[cnt].h=x2;
a[cnt].num=-1;
a1[cnt++].num=-1;
}
ans=0;
sort(x,x+cnt);
sort(y,y+cnt);
sort(a,a+cnt,cmp);
sort(a1,a1+cnt,cmp);
len=unique(x,x+cnt)-x;
len1=unique(y,y+cnt)-y;
last=0;
for(int i=0;i<cnt;i++)
{
l=lower_bound(x,x+len,a[i].l)-x;
r=lower_bound(x,x+len,a[i].r)-x-1;
update(0,len,l,r,a[i].num,1);
ans+=llabs(b[1].len-last);
last=b[1].len;
}
last=0;
for(int i=0;i<cnt;i++)
{
l=lower_bound(y,y+len1,a1[i].l)-y;
r=lower_bound(y,y+len1,a1[i].r)-y-1;
update1(0,len1,l,r,a1[i].num,1);
ans+=llabs(b1[1].len-last);
last=b1[1].len;
}
cout<<ans<<endl;
}
return 0;
}