poj 1389(离散化+计算几何)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 3412 | Accepted: 1763 |
Description
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
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
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(离散化+计算几何)的更多相关文章
- POJ 1389 Area of Simple Polygons 扫描线+线段树面积并
---恢复内容开始--- LINK 题意:同POJ1151 思路: /** @Date : 2017-07-19 13:24:45 * @FileName: POJ 1389 线段树+扫描线+面积并 ...
- poj 1151(离散化+矩形面积并)
题目链接:http://poj.org/problem?id=1151 关于离散化,这篇博客讲的很好:http://www.cppblog.com/MiYu/archive/2010/10/15/12 ...
- POJ 1410 Intersection (计算几何)
题目链接:POJ 1410 Description You are to write a program that has to decide whether a given line segment ...
- 【POJ 1389】Area of Simple Polygons(线段树+扫描线,矩形并面积)
离散化后,[1,10]=[1,3]+[6,10]就丢了[4,5]这一段了. 因为更新[3,6]时,它只更新到[3,3],[6,6]. 要么在相差大于1的两点间加入一个值,要么就让左右端点为l,r的线段 ...
- POJ 1106 Transmitters(计算几何)
题目链接 切计算几何,感觉计算几何的算法还不熟.此题,枚举线段和圆点的直线,平分一个圆 #include <iostream> #include <cstring> #incl ...
- poj 2507Crossed ladders <计算几何>
链接:http://poj.org/problem?id=2507 题意:哪个直角三角形,一直角边重合, 斜边分别为 X, Y, 两斜边交点高为 C , 求重合的直角边长度~ 思路: 设两个三角形不重 ...
- TOYS - POJ 2318(计算几何,叉积判断)
题目大意:给你一个矩形的左上角和右下角的坐标,然后这个矩形有 N 个隔板分割成 N+1 个区域,下面有 M 组坐标,求出来每个区域包含的坐标数. 分析:做的第一道计算几何题目....使用叉积判断方 ...
- D - Mayor's posters POJ - 2528 离散化+线段树 区间修改单点查询
题意 贴海报 最后可以看到多少海报 思路 :离散化大区间 其中[1,4] [5,6]不能离散化成[1,2] [2,3]因为这样破坏了他们的非相邻关系 每次离散化区间 [x,y]时 把y+1点也加入 ...
- POJ 1654 Area 计算几何
#include<stdio.h> #include<string.h> #include<iostream> #include<math.h> usi ...
随机推荐
- 阿里巴巴Java开发规约Eclipse插件安装及使用
技术交流群:233513714 插件安装 环境:JDK1.8,Eclipse4+.有同学遇到过这样的情况,安装插件重启后,发现没有对应的菜单项,从日志上也看不到相关的异常信息,最后把JDK从1.6升级 ...
- MySQL之查询性能优化(一)
为什么查询速度会慢 通常来说,查询的生命周期大致可以按照顺序来看:从客户端,到服务器,然后在服务器上进行解析,生成执行计划,执行,并返回结果给客户端.其中“执行”可以认为是整个生命周期中最重要的阶段, ...
- Redis常用知识
1.redis是什么 redis是一种支持Key-Value等多种数据结构的存储系统.可用于缓存.事件发布或订阅.高速队列等场景.该数据库使用ANSI C语言编写,支持网络,提供字符串.哈希.列表.队 ...
- java IO小结
package 字符与字节转换; import java.io.*; public class char_byte { public static void main(String[] args) { ...
- 孤荷凌寒自学python第三十八天初识python的线程控制
孤荷凌寒自学python第三十八天初识python的线程控制 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 一.线程 在操作系统中存在着很多的可执行的应用程序,每个应用程序启动后,就可以看 ...
- Python 装饰器初探
Python 装饰器初探 在谈及Python的时候,装饰器一直就是道绕不过去的坎.面试的时候,也经常会被问及装饰器的相关知识.总感觉自己的理解很浅显,不够深刻.是时候做出改变,对Python的装饰器做 ...
- ES原理(转载)
该博客属于转载,是很经典的一篇关于ES的介绍: Elasticsearch 是一个兼有搜索引擎和NoSQL数据库功能的开源系统,基于Java/Lucene构建,可以用于全文搜索,结构化搜索以及近实时分 ...
- winform 路径
System.AppDomain.CurrentDomain.BaseDirectory d:\project\bin\release\
- SQL 基础笔记(二):进阶查询
本笔记整理自<SQL 基础教程>.<MySQL 必知必会>和网上资料.个人笔记不保证正确. 一.复杂查询 视图 将 SELECT 查询包装成一个虚拟表,该虚拟表就被称为视图.( ...
- ocrosoft Contest1316 - 信奥编程之路~~~~~第三关问题 D: 手机话费
http://acm.ocrosoft.com/problem.php?cid=1316&pid=3 题目描述 小明的手机每天消费1元,每消费K元就可以获赠1元,一开始小明有M元,问最多可以用 ...