【POJ 1389】Area of Simple Polygons(线段树+扫描线)
来源:互联网 发布:java 字符串排列组合 编辑:程序博客网 时间:2024/06/05 15:14
Area of Simple Polygons
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3500 Accepted: 1806
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
[Submit] [Go Back] [Status] [Discuss]
Home Page Go Back To top
[题意][计算总面积,重叠部分只计算一次]
【题解】【扫描线+线段树】
【一般来说,扫描线都是用来计算矩形的周长、面积之类的东西】
【就本题来说,计算面积,将矩形按横坐标排序,线段树里记录纵坐标,用线段树维护被纵坐标覆盖的点数】
【覆盖的时候不标记下放,这样小线段还保留着之前的结果,所以删线段的时候不会影响之前的小线段,就可以直接用小线段更新答案。】
#include<cstdio>#include<cstring>#include<algorithm>#define ll long longusing namespace std;struct data{int l,r,x,val;}a[100010];int sum[500010];ll s[500010];int tot;int tmp(data a,data b){return a.x<b.x;}inline void updata(int now,int l,int r){if(sum[now]>0) s[now]=(ll)r-l+1; else if(l==r) s[now]=0; else s[now]=s[(now<<1)]+s[(now<<1)|1];return;}void change(int now,int l,int r,int al,int ar,int v){if(al<=l&&r<=ar) { sum[now]+=v; updata(now,l,r); return; }int mid=(l+r)>>1;if(al<=mid) change((now<<1),l,mid,al,ar,v);if(ar>mid) change((now<<1)|1,mid+1,r,al,ar,v);updata(now,l,r);}int main(){int i,j;int x1,y1,x2,y2;while((scanf("%d%d%d%d",&x1,&y1,&x2,&y2)==4)) { int maxn=0; tot=0;if(x1==-1) break; memset(sum,0,sizeof(sum)); tot++; a[tot].l=y1,a[tot].r=y2; a[tot].x=x1; a[tot].val=1; tot++; a[tot].l=y1;a[tot].r=y2; a[tot].x=x2; a[tot].val=-1; if(maxn<y2) maxn=y2; while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)==4) { if(x1==-1) break; tot++; a[tot].l=y1; a[tot].r=y2; a[tot].x=x1; a[tot].val=1; tot++; a[tot].l=y1; a[tot].r=y2; a[tot].x=x2; a[tot].val=-1; if(maxn<y2) maxn=y2; }sort(a+1,a+tot+1,tmp);ll ans=0;for(i=1;i<=tot;++i) { if(i!=1) ans+=(ll)(a[i].x-a[i-1].x)*s[1]; change(1,0,maxn,a[i].l+1,a[i].r,a[i].val); }printf("%lld\n",ans); }return 0;}
0 0
- 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(线段树+扫描线+离散化)
- [POJ1389]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 线段树+扫描线
- hdoj 1542 && poj 1151 Atlantis && 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
- 对称算法(计算机图形学)
- HDU 1213 并查集
- hdu 5790 Prefix (字典树 + 主席树)
- HDU 2196 Computer [树形dp]
- 用一个二维码做下载地址,自动区分是 ios 还是 android
- 【POJ 1389】Area of Simple Polygons(线段树+扫描线)
- Android eclipse sdk包升级
- 使用Spring JdbcTemplate调用存储过程
- android 中转换不同坐标系
- POJ 2142 The Balance
- Semaphore
- HTML5--link、selector、!important
- 小丸子总结node.js的一些问题
- 【zzuliOJ】1907 - 小火山的宝藏收益(dfs)