Picture
Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 10284 | Accepted: 5452 |
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.
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.
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
Source
//AC代码
/*
题意:求矩形周长的并
此题本人看了许多大牛解题报告愣是没太弄懂(智商啊)
下面代码是我照着大牛的代码敲的,悲哀~~Orz
*/
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
#define LL long long
#define IT __int64
#define zero(x) fabs(x)<eps
#define mm(a,b) memset(a,b,sizeof(a))
const int INF=0x7fffffff;
const double inf=1e8;
const double eps=1e-10;
const double PI=acos(-1.0);
const int Max=20001;
using namespace std;
int sign(double x)
{
return (x>eps)-(x<-eps);
}
struct Rectangular
{
int x;
int y1,y2;
int ok;
Rectangular(const int &_x=0,const int &_y1=0,const int &_y2=0,const int &_ok=0):x(_x),y1(_y1),y2(_y2),ok(_ok){}
}line[Max];
struct Node
{
int left,right;
int length;
int cover;
int num;
int cover_l,cover_r;
}node[Max];
int cover_y[Max];
int Total_Len;
bool cmp_x(Rectangular u,Rectangular v)
{
return u.x<v.x;
}
void Build_Tree(int t,int left,int right)
{
node[t].left=left;
node[t].right=right;
node[t].cover=0;
node[t].length=0;
node[t].cover_l=0;
node[t].cover_r=0;
node[t].num=0;
if((left+1)==right) return;
int mid=(left+right)>>1;
Build_Tree(t<<1,left,mid);
Build_Tree(t<<1|1,mid,right);
}
void Calen_Len(int t)
{
if(node[t].cover>0)
{
node[t].length=cover_y[node[t].right]-cover_y[node[t].left];
node[t].num=1;
node[t].cover_l=1;
node[t].cover_r=1;
}
else
{
if(node[t].left+1==node[t].right)
{
node[t].length=0;
node[t].cover_l=0;
node[t].cover_r=0;
node[t].num=0;
}
else
{
node[t].cover_l=node[t<<1].cover_l;
node[t].cover_r=node[t<<1|1].cover_r;
node[t].num=node[t<<1].num+node[t<<1|1].num-node[t<<1].cover_r*node[t<<1|1].cover_l;
node[t].length=node[t<<1].length+node[t<<1|1].length;
}
}
}
void Update_Len(int t,Rectangular R)
{
if(cover_y[node[t].left]>=R.y1&&cover_y[node[t].right]<=R.y2)
{
node[t].cover+=R.ok;
Calen_Len(t);
return;
}
if(node[t].left+1==node[t].right) return;
int mid=(node[t].left+node[t].right)>>1;
if(R.y1<=cover_y[mid])
Update_Len(t<<1,R);
if(R.y2>=cover_y[mid])
Update_Len(t<<1|1,R);
Calen_Len(t);
}
void Init_len(int &num_x,int &num_y,int x1,int y1,int x2,int y2)
{
line[num_x++]=Rectangular(x1,y1,y2,1);
line[num_x++]=Rectangular(x2,y1,y2,-1);
cover_y[num_y++]=y1;
cover_y[num_y++]=y2;
}
int main()
{
int n,m,i,j;
int x1,y1,x2,y2;
int Len_y;
int num_x,num_y;
freopen("D:\\in.txt","r",stdin);
while(cin>>n)
{
num_x=0,num_y=0;
Len_y=0;
Total_Len=0;
for(i=0;i<n;i++)
{
cin>>x1>>y1>>x2>>y2;
Init_len(num_x,num_y,x1,y1,x2,y2);
}
sort(line,line+num_x,cmp_x);
sort(cover_y,cover_y+num_y);
num_y=unique(cover_y,cover_y+num_y)-cover_y;
//unique的功能是去除相邻的重复元素(只保留一个)
//unique(num,mun+n)返回的是num去重后的尾地址,之所以说比不真正把重复的元素删除
//其实是,该函数把重复的元素一到后面去了,然后依然保存到了原数组中然后返回去重后最后一个元素的地址
Build_Tree(1,0,num_y-1);
for(i=0;i<num_x;i++)
{
Update_Len(1,line[i]);
cout<<node[1].length<<" "<<Len_y<<" "<<node[1].num<<endl;
Total_Len+=abs(node[1].length-Len_y);
if(i<num_x-1)
{
Total_Len+=2*node[1].num*(line[i+1].x-line[i].x);
}
Len_y=node[1].length;
}
cout<<Total_Len<<endl;
}
return 0;
}