codeforces 70D Professor's task(动态二维凸包)
题目链接:http://codeforces.com/contest/70/problem/D
Once a walrus professor Plato asked his programming students to perform the following practical task.
The students had to implement such a data structure that would support a convex hull on some set of points S. The input to the program had q queries of two types:
1. Add a point with coordinates (x, y) into the set S. Note that in this case the convex hull of S could have changed, and could have remained the same.
2. Say whether a point with coordinates (x, y) belongs to an area limited by the convex hull, including the border.
All the students coped with the task. What about you?
The first line contains an integer q (4 ≤ q ≤ 105).
Then follow q lines in the following way: "t x y", where t is the query type (1 or 2), and (x, y) are the coordinates of the point ( - 106 ≤ x, y ≤ 106, x and y are integers).
There is at least one query of type 2.
It is guaranteed that the three queries of the first type follow first and the points given in the queries form a non-degenerative triangle. Also all the points added in S are distinct.
For each query of the second type print one string containing "YES", if the point lies inside the convex hull or on its border. Otherwise, print "NO".
题目大意:q个操作。操作1:往点集中添加一个点。操作2:给一个点,问这个点是否在点集所构成的凸包内部(包括边缘)。
思路:增量法求动态凸包。可以看:http://blog.csdn.net/crazy_ac/article/details/8499443
PS:原来map的迭代器是首尾相连的……
代码(156MS):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
typedef map<int, int> MPII; struct Point {
int x, y;
Point() {}
Point(int x, int y): x(x), y(y) {}
Point(MPII::iterator it): x(it->first), y(it->second) {}
Point operator - (const Point &rhs) const {
return Point(x - rhs.x, y - rhs.y);
}
}; LL cross(const Point &a, const Point &b) {
return (LL)a.x * b.y - (LL)a.y * b.x;
} LL cross(const Point &o, const Point &a, const Point &b) {
return cross(a - o, b - o);
} bool below(MPII &mp, Point p) {
if(mp.empty()) return false;
if(p.x < mp.begin()->first || mp.rbegin()->first < p.x) return false;
MPII::iterator a = mp.lower_bound(p.x), b = a--;
if(b->first == p.x) return b->second >= p.y;
return cross(a, b, p) <= ;
} void insert(MPII &mp, Point p) {
if(below(mp, p)) return ;
MPII::iterator a, b, it;
mp[p.x] = p.y;
it = mp.lower_bound(p.x); b = it; ++b;
if(b != mp.end()) {
a = b++;
while(b != mp.end() && cross(p, a, b) >= )
mp.erase(a), a = b++;
} a = it; --a;
if(it != mp.begin() && a != mp.begin()) {
b = a--;
while(b != mp.begin() && cross(p, b, a) <= )
mp.erase(b), b = a--;
}
} MPII up, down;
int q, op, x, y; int main() {
scanf("%d", &q);
while(q--) {
scanf("%d%d%d", &op, &x, &y);
if(op == ) {
insert(up, Point(x, y));
insert(down, Point(x, -y));
} else {
puts((below(up, Point(x, y)) && below(down, Point(x, -y))) ? "YES" : "NO");
}
}
}
codeforces 70D Professor's task(动态二维凸包)的更多相关文章
- Codeforces Gym 100286A. Aerodynamics 计算几何 求二维凸包面积
Problem A. AerodynamicsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/co ...
- C++ 里 构建动态二维数组
//****动态二维数组 /* int m=3; int **data; int n=2; data=new int*[m]; for(int j=0;j<m;j++) { data[j]=ne ...
- C++动态二维数组的创建
两种方式. 一,二级指针,创建2行3列的动态二维数组. 这里,p指向的是2个地址,这两个地址各指向长度为3的一维整型数组. 在内存中,每行元素内部顺序排列.两行元素的首地址不同,p[1]与p[2]存放 ...
- C++建立动态二维数组
C++建立动态二维数组主要有两种方法: 1.使用数组指针,分配一个指针数组,将其首地址保存在b中,然后再为指针数组的每个元素分配一个数组 int * ...
- php 合并图片 (将活动背景图片和动态二维码图片合成一张图片)
<?php //案例一:将活动背景图片和动态二维码图片合成一张图片 //图片一 $path_1 = './background.png'; //图片二 $path_2 = './FU0851_2 ...
- 动态二维数组赋值及for循环遍历和toString遍历
package com.Summer_0421.cn; import java.util.Arrays; /** * @author Summer * 动态二维数组赋值及for循环遍历和toStrin ...
- 以杨辉三角为例,从内存角度简单分析C语言中的动态二维数组
学C语言,一定绕不过指针这一大难关,而指针最让人头疼的就是各种指向关系,一阶的指针还比较容易掌握,但一旦阶数一高,就很容易理不清楚其中的指向关系,现在我将通过杨辉三角为例,我会用四种方法从内存的角度简 ...
- python小工具myqr生成动态二维码
python小工具myqr生成动态二维码 (一)安装 (二)使用 (一)安装 命令: pip install myqr 安装完成后,就可以在命令行中输入 myqr 查看下使用帮助: myqr --he ...
- Python | 一行命令生成动态二维码
当我看到别人的二维码都做的这么炫酷的时候,我心动了! 我也想要一个能够吸引眼球的二维码,今天就带大家一起用 Python 来做一个炫酷的二维码! 首先要安装工具 myqr: pip install m ...
随机推荐
- 关于FireMonkey TGrid赋值的一点小研究
FireMoneky的TStringGrid用法和VCL里面的差不多, 但是另一个TGrid实在是奇葩, 几乎找不到给单元格赋值的方法(除了使用LiveBind) 看了其源码, 发现只要给某个Colu ...
- 弹窗文件js+css
// 每个弹窗的标识 var x =0; var idzt = new Array(); var Window = function(config){ //ID不重复 idzt[x] = " ...
- postgressql安装
参考文章 FreeBSD10.0下ports安装postgresql9.4-server: http://download.csdn.net/detail/machen_smiling/8425613 ...
- angularjs select 循环中出现第一个 option 为空格问题
当select 的ng-module 为空时, select显示空白行. 解决:指定ng-module的默认值.
- Simplest way to serve static data from outside the application server in a Java web application
tomcat service.xml <Context docBase="/path/to/images" path="/images" /> re ...
- [LeetCode]题解(python):057-Insert Interval
题目来源 https://leetcode.com/problems/insert-interval/ Given a set of non-overlapping intervals, insert ...
- Windows-005-显示隐藏文件
此文主要讲述如何设置 Win7 系统显示隐藏的文件.文件夹和驱动器,敬请亲们参阅.若有不足之处,敬请大神指正,不胜感激!详情如下: Win7 系统安装完成后,默认是不显示隐藏的文件.文件夹和驱动器的( ...
- Selenium2学习-002-Selenium2 Web 元素定位及 XPath 编写演示示例
此文主要对 Selenium2 的 Web 元素定位及 XPath 编写示例,敬请各位亲们参阅,共同探讨.若有不足之处,敬请各位大神指正,不胜感激! 通过 Firefox(火狐)浏览器的插件 Fire ...
- [转]详细介绍java中的数据结构
详细介绍java中的数据结构 本文介绍的是java中的数据结构,本文试图通过简单的描述,向读者阐述各个类的作用以及如何正确使用这些类.一起来看本文吧! 也许你已经熟练使用了java.util包里面的各 ...
- Java的浮点数和整数的进制转换
整数的表达 –原码:第一位为符号位(0为正数,1为负数) –反码:符号位不动,原码取反 –负数补码:符号位不动,反码加1 –正数补码:和原码相同 -6 5 原码 10000110 0 ...