POJ1389[Area of Simple Polygons]
来源:互联网 发布:js获取button的value值 编辑:程序博客网 时间:2024/05/18 02:35
Area of Simple Polygons
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.
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 41 1 5 21 1 2 5-1 -1 -1 -10 0 2 21 1 3 32 2 4 4-1 -1 -1 -1-1 -1 -1 -1
Sample Output
1810
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;}
1 0
- POJ1389-Area of Simple Polygons
- POJ1389[Area of Simple Polygons]
- poj1389 Area of Simple Polygons
- [POJ1389]Area of Simple Polygons(扫描线+线段树)
- poj 1389 Area of Simple Polygons
- poj 1389 Area of Simple Polygons
- PKU 1389 Area of Simple Polygons
- POJ-1389-Area of Simple Polygons
- poj 1389 Area of Simple Polygons
- POJ 1389 Area of Simple Polygons
- POJ 1389 Area of Simple Polygons .
- POJ 1389 Area of Simple Polygons 笔记
- poj 1389 Area of Simple Polygons(线段树做法)
- poj_1389 Area of Simple Polygons 线段树+扫描线
- POJ 1389 Area of Simple Polygons(扫描线)
- poj 1389Area of Simple Polygons(矩形面积并)
- POJ 1389 Area of Simple Polygons 线段树 扫描线
- poj 1389 Area of Simple Polygons 线段树扫面线,和1151一样的嘛
- 11g打psu
- Android四大组件应用系列——Activity与Service交互实现APK下载
- 网络流相关
- 将数字格式的日期月份转换为英文月份
- eclipse 出现the user operation is waiting for "Building workspace" to comlete 卡死
- POJ1389[Area of Simple Polygons]
- Git用法之常用命令
- Android UI--自定义ListView(实现下拉刷新+加载更多)
- Android开发之MdiaPlayer详解
- 8 种提升 ASP.NET Web API 性能的方法
- struts配置2
- MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建
- 添加DOCTYPE
- 图文并茂用地址分析双向链表