Area of Simple Polygons
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3412   Accepted: 1763

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 题意:矩形面积交.
只用了离散化没用线段树也 47MS AC。。就是耗费的空间大了点,,不过将vis数组开成bool类型应该可以少很多空间..
///离散化
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
const int N = ;
struct Rec{
int x1,y1,x2,y2;
}rec[N];
int x[N],y[N];
int vis[N][N];
int k,t;
int binary1(int value){
int mid,l=,r=k-;
while(l<r){
mid = (l+r)>>;
if(x[mid]==value) return mid;
if(x[mid]<value) l = mid+;
else r = mid-;
}
return l;
}
int binary2(int value){
int mid,l=,r=k-;
while(l<r){
mid = (l+r)>>;
if(y[mid]==value) return mid;
if(y[mid]<value) l = mid+;
else r = mid-;
}
return l;
}
void input(){
int x1,y1,x2,y2;
while(true){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1==-&&x2==-&&y1==-&&y2==-) break;
rec[t].x1 = x1,rec[t].y1 = y1,rec[t].x2=x2,rec[t++].y2 = y2;
x[k] = x1,y[k++] = y1;
x[k] = x2,y[k++] = y2;
}
sort(x,x+k);
sort(y,y+k);
}
void solve(){
int t1,t2,t3,t4;
for(int i=;i<t;i++){
t1 = binary1(rec[i].x1);
t2 = binary1(rec[i].x2);
t3 = binary2(rec[i].y1);
t4 = binary2(rec[i].y2);
for(int j=t1;j<t2;j++){
for(int l = t3;l<t4;l++){
vis[j][l] = ;
}
}
}
int area = ;
for(int i=;i<k;i++){
for(int j=;j<k;j++){
area+=vis[i][j]*(x[i+]-x[i])*(y[j+]-y[j]);
}
}
printf("%d\n",area);
}
int main()
{
int x1,y1,x2,y2;
while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF){
if(x1==-&&x2==-&&y1==-&&y2==-) break;
memset(vis,,sizeof(vis));
k=,t=;
rec[t].x1 = x1,rec[t].y1 = y1,rec[t].x2=x2,rec[t++].y2 = y2;
x[k] = x1,y[k++] = y1;
x[k] = x2,y[k++] = y2;
input();
solve();
}
return ;
}

poj 1389(离散化+计算几何)的更多相关文章

  1. POJ 1389 Area of Simple Polygons 扫描线+线段树面积并

    ---恢复内容开始--- LINK 题意:同POJ1151 思路: /** @Date : 2017-07-19 13:24:45 * @FileName: POJ 1389 线段树+扫描线+面积并 ...

  2. poj 1151(离散化+矩形面积并)

    题目链接:http://poj.org/problem?id=1151 关于离散化,这篇博客讲的很好:http://www.cppblog.com/MiYu/archive/2010/10/15/12 ...

  3. POJ 1410 Intersection (计算几何)

    题目链接:POJ 1410 Description You are to write a program that has to decide whether a given line segment ...

  4. 【POJ 1389】Area of Simple Polygons(线段树+扫描线,矩形并面积)

    离散化后,[1,10]=[1,3]+[6,10]就丢了[4,5]这一段了. 因为更新[3,6]时,它只更新到[3,3],[6,6]. 要么在相差大于1的两点间加入一个值,要么就让左右端点为l,r的线段 ...

  5. POJ 1106 Transmitters(计算几何)

    题目链接 切计算几何,感觉计算几何的算法还不熟.此题,枚举线段和圆点的直线,平分一个圆 #include <iostream> #include <cstring> #incl ...

  6. poj 2507Crossed ladders <计算几何>

    链接:http://poj.org/problem?id=2507 题意:哪个直角三角形,一直角边重合, 斜边分别为 X, Y, 两斜边交点高为 C , 求重合的直角边长度~ 思路: 设两个三角形不重 ...

  7. TOYS - POJ 2318(计算几何,叉积判断)

    题目大意:给你一个矩形的左上角和右下角的坐标,然后这个矩形有 N 个隔板分割成 N+1 个区域,下面有 M 组坐标,求出来每个区域包含的坐标数.   分析:做的第一道计算几何题目....使用叉积判断方 ...

  8. D - Mayor's posters POJ - 2528 离散化+线段树 区间修改单点查询

    题意 贴海报 最后可以看到多少海报 思路 :离散化大区间  其中[1,4] [5,6]不能离散化成[1,2] [2,3]因为这样破坏了他们的非相邻关系 每次离散化区间 [x,y]时  把y+1点也加入 ...

  9. POJ 1654 Area 计算几何

    #include<stdio.h> #include<string.h> #include<iostream> #include<math.h> usi ...

随机推荐

  1. JDBC---java与数据库中数据类型的对应关系

    基础数据类型 Java 类型 SQL 类型 int 或 java.lang.Integer INTEGER long 或 java.lang.Long BIGINT short 或 java.lang ...

  2. 1096: [ZJOI2007]仓库建设

    1096: [ZJOI2007]仓库建设 思路 斜率优化. 代码 #include<cstdio> #include<iostream> using namespace std ...

  3. 《Cracking the Coding Interview》——第6章:智力题——题目5

    2014-03-20 01:08 题目:扔鸡蛋问题.有一个鸡蛋,如果从N楼扔下去恰好会摔碎,低于N楼则不碎,可以继续扔.给你两个这样的鸡蛋,要求你一定得求出N,怎么扔才能减少最坏情况下的扔的次数? 解 ...

  4. Spring+SpringMVC+MyBatis+Redis框架学习笔记

    在Java互联网中,以Spring+Spring MVC+MyBatis (SSM) 作为主流框架. SSM+Redis的结构图 在这种框架系统中: Spring IoC 承担了一个资源管理.整合.即 ...

  5. Python IO关于mode参数的问题

    关于open()的mode参数: 'r':读 'w':写 'a':追加 'r+' == r+w(可读可写,文件若不存在就报错(IOError)) 'w+' == w+r(可读可写,文件若不存在就创建) ...

  6. 常用模块(datatime)

    import datetime,time# dt = datetime.datetime.now() # 获取当前时间的时间对象# dt = datetime.date.fromtimestamp(t ...

  7. springboot整合jersey

    https://blog.csdn.net/xiongpei00/article/details/76576420

  8. SELECTORS模块实现并发简单版FTP

    环境:windows, python 3.5功能:使用SELECTORS模块实现并发简单版FTP允许多用户并发上传下载文件 结构:ftp_client ---| bin ---| start_clie ...

  9. 牛客网暑期ACM多校训练营(第一场):J-Different Integers(分开区间不同数+树状数组)

    链接:J-Different Integers 题意:给出序列a1, a2, ..., an和区间(l1, r1), (l2, r2), ..., (lq, rq),对每个区间求集合{a1, a2, ...

  10. HTML快速入门

    我们经常上网浏览网页,来获取资讯.可网页是什么呢?它又是如何编写出来的呢? 网页又叫Web页面,我们经常可以在网页上看到文字.图片.视频等.我们所要说的HTML(超文本标记语言)就是构成网页文档的主要 ...