POJ1389[Area of Simple Polygons]
2016-02-25 09:39
513 查看
Area of Simple Polygons
Submit Status
Description
There are N, 1 <= N <= 1,000 rectangles in the 2-D xy-plane. The four sides of a rectangle are horizontal or vertical line segments. Rectangles are defined by their lower-left and upper-right corner points. Each corner point is a pair of two nonnegative integers
in the range of 0 through 50,000 indicating its x and y coordinates.
Assume that the contour of their union is defi ned by a set S of segments. We can use a subset of S to construct simple polygon(s). Please report the total area of the polygon(s) constructed by the subset of S. The area should be as large as possible. In a
2-D xy-plane, a polygon is defined by a finite set of segments such that every segment extreme (or endpoint) is shared by exactly two edges and no subsets of edges has the same property. The segments are edges and their extremes are the vertices of the polygon.
A polygon is simple if there is no pair of nonconsecutive edges sharing a point.
Example: Consider the following three rectangles:
rectangle 1: < (0, 0) (4, 4) >,
rectangle 2: < (1, 1) (5, 2) >,
rectangle 3: < (1, 1) (2, 5) >.
The total area of all simple polygons constructed by these rectangles is 18.
Input
The input consists of multiple test cases. A line of 4 -1's separates each test case. An extra line of 4 -1's marks the end of the input. In each test case, the rectangles are given one by one in a line. In each line for a rectangle, 4 non-negative integers
are given. The first two are the x and y coordinates of the lower-left corner. The next two are the x and y coordinates of the upper-right corner.
Output
For each test case, output the total area of all simple polygons in a line.
Sample Input
Sample Output
Source
Taiwan 2001
一道裸的扫描线矩形面积并,看数据范围应该不用离散化,管它的,写了好一点。
把矩形拆成左右两条线段,然后排个序,从左往右扫一道,再次巩固一下。
(我的线段树版本,每个节点记录的是[l,r]的里面线段的左端点,所以更新的时候更新[l,r-1]左端点呀~)
最主要的是巩固特殊的pushup
void pushup(int i)
{
int l=T[i].l;
int r=T[i].r;
for(int j=0;j<=k;j++)T[i].len[j]=0;
if(l==r)
{
int t=min(T[i].flag,k);
T[i].len[t]=det[l+1]-det[l];
}
else
{
for(int j=0;j<=k;j++)
{
int t=min(T[i].flag+j,k);
T[i].len[t]+=T[i<<1].len[j]+T[i<<1|1].len[j];
}
}
}
用这个特殊的pushup更新被覆盖k的矩形面积,这样以后就算写覆盖多少次也没关系,直接套。
AC代码:
#include<cstdio>
#include<iostream>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<vector>
const int maxn=1000+20;
const int k=1;
using namespace std;
struct line
{
int x,y1,y2,flag;
line(int x,int y1,int y2,int flag)
{
this->x=x;
this->y1=y1;
this->y2=y2;
this->flag=flag;
}
bool operator<(const line &p)const
{
return x<p.x;
}
};
vector<line>L;
int det[2*maxn];
int tot;
struct node
{
int l,r,flag;
int len[5];
void set(int l,int r,int flag)
{
this->l=l;
this->r=r;
this->flag=flag;
}
}T[8*maxn];
void pushup(int i)
{
int l=T[i].l;
int r=T[i].r;
for(int j=0;j<=k;j++)T[i].len[j]=0;
if(l==r)
{
int t=min(T[i].flag,k);
T[i].len[t]=det[l+1]-det[l];
}
else
{
for(int j=0;j<=k;j++)
{
int t=min(T[i].flag+j,k);
T[i].len[t]+=T[i<<1].len[j]+T[i<<1|1].len[j];
}
}
}
void build(int i,int l,int r)
{
T[i].set(l,r,0);
if(l==r)
{
pushup(i);
return ;
}
int mid=(l+r)>>1;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
pushup(i);
}
void updata(int i,int L,int R,int x)
{
int l=T[i].l;
int r=T[i].r;
if(l>=L&&r<=R)
{
T[i].flag+=x;
pushup(i);
return;
}
int mid=(l+r)>>1;
if(L<=mid)updata(i<<1,L,R,x);
if(R>mid)updata(i<<1|1,L,R,x);
pushup(i);
}
int find(int x)
{
return lower_bound(det+1,det+tot+1,x)-det;
}
int main()
{
int x1,y1,x2,y2;
while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)
{
tot=0;
L.clear();
if(x1==-1&&y1==-1&&x2==-1&&y2==-1)break;
det[++tot]=y1;
det[++tot]=y2;
L.push_back(line(x1,y1,y2,1));
L.push_back(line(x2,y1,y2,-1));
while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)
{
if(x1==-1&&y1==-1&&x2==-1&&y2==-1)break;
det[++tot]=y1;
det[++tot]=y2;
L.push_back(line(x1,y1,y2,1));
L.push_back(line(x2,y1,y2,-1));
}
sort(det+1,det+tot+1);
int t=tot;
tot=unique(det+1,det+t+1)-det-1;
sort(L.begin(),L.end());
build(1,1,tot);
int ans=0;
for(int i=0;i<L.size()-1;i++)
{
int l=find(L[i].y1);
int r=find(L[i].y2);
updata(1,l,r-1,L[i].flag);
ans+=T[1].len[k]*(L[i+1].x-L[i].x);
}
printf("%d\n",ans);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
There are N, 1 <= N <= 1,000 rectangles in the 2-D xy-plane. The four sides of a rectangle are horizontal or vertical line segments. Rectangles are defined by their lower-left and upper-right corner points. Each corner point is a pair of two nonnegative integers
in the range of 0 through 50,000 indicating its x and y coordinates.
Assume that the contour of their union is defi ned by a set S of segments. We can use a subset of S to construct simple polygon(s). Please report the total area of the polygon(s) constructed by the subset of S. The area should be as large as possible. In a
2-D xy-plane, a polygon is defined by a finite set of segments such that every segment extreme (or endpoint) is shared by exactly two edges and no subsets of edges has the same property. The segments are edges and their extremes are the vertices of the polygon.
A polygon is simple if there is no pair of nonconsecutive edges sharing a point.
Example: Consider the following three rectangles:
rectangle 1: < (0, 0) (4, 4) >,
rectangle 2: < (1, 1) (5, 2) >,
rectangle 3: < (1, 1) (2, 5) >.
The total area of all simple polygons constructed by these rectangles is 18.
Input
The input consists of multiple test cases. A line of 4 -1's separates each test case. An extra line of 4 -1's marks the end of the input. In each test case, the rectangles are given one by one in a line. In each line for a rectangle, 4 non-negative integers
are given. The first two are the x and y coordinates of the lower-left corner. The next two are the x and y coordinates of the upper-right corner.
Output
For each test case, output the total area of all simple polygons in a line.
Sample Input
0 0 4 4 1 1 5 2 1 1 2 5 -1 -1 -1 -1 0 0 2 2 1 1 3 3 2 2 4 4 -1 -1 -1 -1 -1 -1 -1 -1
Sample Output
18 10
Source
Taiwan 2001
一道裸的扫描线矩形面积并,看数据范围应该不用离散化,管它的,写了好一点。
把矩形拆成左右两条线段,然后排个序,从左往右扫一道,再次巩固一下。
(我的线段树版本,每个节点记录的是[l,r]的里面线段的左端点,所以更新的时候更新[l,r-1]左端点呀~)
最主要的是巩固特殊的pushup
void pushup(int i)
{
int l=T[i].l;
int r=T[i].r;
for(int j=0;j<=k;j++)T[i].len[j]=0;
if(l==r)
{
int t=min(T[i].flag,k);
T[i].len[t]=det[l+1]-det[l];
}
else
{
for(int j=0;j<=k;j++)
{
int t=min(T[i].flag+j,k);
T[i].len[t]+=T[i<<1].len[j]+T[i<<1|1].len[j];
}
}
}
用这个特殊的pushup更新被覆盖k的矩形面积,这样以后就算写覆盖多少次也没关系,直接套。
AC代码:
#include<cstdio>
#include<iostream>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<vector>
const int maxn=1000+20;
const int k=1;
using namespace std;
struct line
{
int x,y1,y2,flag;
line(int x,int y1,int y2,int flag)
{
this->x=x;
this->y1=y1;
this->y2=y2;
this->flag=flag;
}
bool operator<(const line &p)const
{
return x<p.x;
}
};
vector<line>L;
int det[2*maxn];
int tot;
struct node
{
int l,r,flag;
int len[5];
void set(int l,int r,int flag)
{
this->l=l;
this->r=r;
this->flag=flag;
}
}T[8*maxn];
void pushup(int i)
{
int l=T[i].l;
int r=T[i].r;
for(int j=0;j<=k;j++)T[i].len[j]=0;
if(l==r)
{
int t=min(T[i].flag,k);
T[i].len[t]=det[l+1]-det[l];
}
else
{
for(int j=0;j<=k;j++)
{
int t=min(T[i].flag+j,k);
T[i].len[t]+=T[i<<1].len[j]+T[i<<1|1].len[j];
}
}
}
void build(int i,int l,int r)
{
T[i].set(l,r,0);
if(l==r)
{
pushup(i);
return ;
}
int mid=(l+r)>>1;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
pushup(i);
}
void updata(int i,int L,int R,int x)
{
int l=T[i].l;
int r=T[i].r;
if(l>=L&&r<=R)
{
T[i].flag+=x;
pushup(i);
return;
}
int mid=(l+r)>>1;
if(L<=mid)updata(i<<1,L,R,x);
if(R>mid)updata(i<<1|1,L,R,x);
pushup(i);
}
int find(int x)
{
return lower_bound(det+1,det+tot+1,x)-det;
}
int main()
{
int x1,y1,x2,y2;
while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)
{
tot=0;
L.clear();
if(x1==-1&&y1==-1&&x2==-1&&y2==-1)break;
det[++tot]=y1;
det[++tot]=y2;
L.push_back(line(x1,y1,y2,1));
L.push_back(line(x2,y1,y2,-1));
while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)
{
if(x1==-1&&y1==-1&&x2==-1&&y2==-1)break;
det[++tot]=y1;
det[++tot]=y2;
L.push_back(line(x1,y1,y2,1));
L.push_back(line(x2,y1,y2,-1));
}
sort(det+1,det+tot+1);
int t=tot;
tot=unique(det+1,det+t+1)-det-1;
sort(L.begin(),L.end());
build(1,1,tot);
int ans=0;
for(int i=0;i<L.size()-1;i++)
{
int l=find(L[i].y1);
int r=find(L[i].y2);
updata(1,l,r-1,L[i].flag);
ans+=T[1].len[k]*(L[i+1].x-L[i].x);
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- C#数据结构之顺序表(SeqList)实例详解
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- 数据结构之Treap详解
- 用C语言举例讲解数据结构中的算法复杂度结与顺序表
- C#数据结构之堆栈(Stack)实例详解
- C#数据结构之双向链表(DbLinkList)实例详解
- JavaScript数据结构和算法之图和图算法
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- Java数据结构及算法实例:插入排序 Insertion Sort
- Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture
- java数据结构之java实现栈
- java数据结构之实现双向链表的示例
- Java数据结构及算法实例:选择排序 Selection Sort
- Java数据结构及算法实例:朴素字符匹配 Brute Force
- Java数据结构及算法实例:汉诺塔问题 Hanoi
- Java数据结构及算法实例:快速计算二进制数中1的个数(Fast Bit Counting)