poj 1389 线段树面积覆盖
来源:互联网 发布:程序员培训夜校 编辑:程序博客网 时间:2024/06/11 19:37
http://poj.org/problem?id=1389
Area of Simple Polygons
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 2772 Accepted: 1409
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
这题和poj 1151一样,就是输入的时候复杂点。把poj 1151代码拿过来改的。
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N=1000+10;struct node{ int l,r; int lf,rf,width; int cover;}mem[N*6];//线段树 lf rf 分别为边界 cover 为覆盖情况 width 为覆盖的宽度struct poin{ int x; //记录x坐标 int y1,y2; //y1,y2用来记录平行y轴的线段的两个端点, int k; //标记是左边的边还是右边的边,拆点 k为-1代表左点 1 代表右点}point[N*2];int Y[N*2];bool cmp(poin a,poin b) //排序(x按照从小到大,只需要把x排序){ return a.x<b.x;}void build(int x,int l,int r)//建树{ mem[x].l=l; mem[x].r=r; mem[x].lf=Y[l]; mem[x].rf=Y[r]; mem[x].cover=mem[x].width=0; if(l+1==r) //叶子结点 return ; int mid=(l+r)>>1; build(x*2,l,mid);//端点模型,不是格子模型 build(x*2+1,mid,r);}void find(int x)//更新此处的覆盖宽度{ if(mem[x].cover>0) //完全覆盖(右点则要完全覆盖) { mem[x].width=mem[x].rf-mem[x].lf; //求出长度 return ; } if(mem[x].l+1==mem[x].r) // 叶子节点,wiith 为 0 mem[x].width=0; else//非叶子节点即为下面子区间的覆盖综合,注意由于插入的递归性保证了此等式的执行顺序的正确性 mem[x].width=mem[x*2].width+mem[x*2+1].width;}void add(int x,int k)//加入一个点k的影响{ if(mem[x].lf>=point[k].y1&&mem[x].rf<=point[k].y2)//全部覆盖 ;//直接比较实际值,显示出了cover和标记为1,-1的作用 { mem[x].cover+=point[k].k; find(x); return ; } int mid=(mem[x].l+mem[x].r)>>1; if(Y[mid]<=point[k].y1)//查看 需要怎样往下更新 { add(x*2+1,k); } else if(Y[mid]>=point[k].y2) { add(x*2,k); } else { add(x*2,k); add(x*2+1,k); } find(x);}int main(){ //freopen("data.txt","r",stdin); int x1,y1,x2,y2; while(1) { scanf("%d%d%d%d",&x1,&y1,&x2,&y2); if(x1==-1&&y1==-1&&x2==-1&&y2==-1) break; int I=0; point[I].x=x1; //把坐标映射到数组里(离散化) point[I].y1=y1; point[I].y2=y2; point[I].k=1; Y[I]=y1; ++I; point[I].x=x2; point[I].y1=y1; point[I].y2=y2; point[I].k=-1; Y[I]=y2; ++I; while(1){ scanf("%d%d%d%d",&x1,&y1,&x2,&y2); if(x1==-1&&y1==-1&&x2==-1&&y2==-1) break; point[I].x=x1; //把坐标映射到数组里(离散化) point[I].y1=y1; point[I].y2=y2; point[I].k=1; Y[I]=y1; ++I; point[I].x=x2; point[I].y1=y1; point[I].y2=y2; point[I].k=-1; Y[I]=y2; ++I; } sort(Y,Y+I); //纵坐标排序(小到大) sort(point,point+I,cmp); build(1,0,I-1); //建树 add(1,0); int ans=0; for(int i=1;i<I;++i) { //cout<<i<<endl; ans+=(mem[1].width*(point[i].x-point[i-1].x));//每次用两个x坐标差 × 当前覆盖宽度 //cout<<ans<<endl; add(1,i);//cout<<"TTT"<<endl; } printf("%d\n",ans); } return 0;}
- poj 1389 线段树面积覆盖
- HDU 1255 覆盖的面积 线段树
- hdu 4419线段树覆盖面积
- hdu-1255-覆盖的面积 线段树
- hdu-1255-覆盖的面积-线段树
- HDU-1255-覆盖的面积(线段树)
- hdoj1255_覆盖的面积(线段树)
- 线段树 hdu1255 覆盖的面积
- 覆盖的面积(HDU 1255 线段树)
- Hdu 1255 覆盖的面积 线段树+矩形面积并
- 【HDU1255】覆盖的面积【线段树】【矩形面积交】
- HDU1255 覆盖的面积 (线段树求面积交)
- HDU 1255 覆盖的面积(线段树+面积交)
- poj 1389 线段树求面积并 整数版本
- POJ 1389 求矩形面积并(线段树)
- hdu 1255 覆盖的面积 (线段树扫描线 计算两次覆盖的面积)
- hdu 1255 覆盖的面积 线段树求重复覆盖的面积
- HDU-1255 覆盖的面积 (线段树 求矩形覆盖面积)
- fight1
- 莫走回头路
- 真正体会到一个ac的快感
- poj 1804求逆序数
- poj 3469 网络流最小割
- poj 1389 线段树面积覆盖
- vs2010配备boost编程环境
- 【我的第一篇技术博文】水一水我的Ubuntu13.04【会有一段时间不定时更新本篇】
- Java中的enum在switch中的一个疑问
- Linux命令rsync的使用(一例)
- android屏幕保持唤醒
- make menuconfig出错解决方法
- win32防止被删日志的非主流保护
- Intent用法简单总结