地址:http://codeforces.com/problemset/problem/70/D

题目:

D. Professor's task
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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?

Input

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.

Output

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".

Examples
input
8
1 0 0
1 2 0
1 2 2
2 1 0
1 0 2
2 1 1
2 2 1
2 20 -1
output
YES
YES
YES
NO
思路:动态凸包。
摘自http://blog.csdn.net/auto_ac/article/details/10664641

本题关键:在log(n)的复杂度内判断点在凸包 或 把点插入凸包

判断:平衡树log(n)内选出点所属于的区域

插入:平衡树log(n)内选出点所属于的区域, 与做一般凸包的时候类似,分别以该点向左右两边进行维护,

一直删除不满足凸包的点,直到所有点满足凸包为止。

水平序:

可以用2个平衡树分别维护上下2个半凸包,具体实现时可以把其中一个半凸包按y轴对称以后,那么2个半凸包的维护就是同一种方法,写2个函数就ok了。

具体平衡树可以用set或map,用STL以后边界处理有点烦,需要注意。

水平序的凸包有一个特点(如按x排序):对于上下凸包(分开来看),x相同的点只有一个。所以用set维护比较麻烦,用map维护相对容易一点。

极角序:

之前给你的3个点一定是插入的,可以选它们的中心点o作为之后的凸包中心,按o进行极角排序。

之后的做法就跟 “本题关键” 的做法一致。

 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e5+;
const int mod=1e9+; /*******判断ta与tb的大小关系*******/
int sgn(double ta,double tb);
/** 基本几何结构 **/
struct Point
{
double x,y,a,b;
Point(double a=, double b=){x=a,y=b;}
bool operator<(const Point &ta)const
{
return sgn(a,ta.a)<||(sgn(a,ta.a)==&&sgn(b,ta.b)<);
}
};
typedef set<Point>::iterator iter;
double cross(Point ta,Point tb,Point tc);
double getdis2(const Point &ta,const Point &tb);
iter L(iter it);
iter R(iter it);
int q,n,ch;
Point o,tmp,pt[];
set<Point>st; int main(void)
{
cin>>q;
o.x=o.y=;
for(int i=;i<=;i++)
scanf("%d%lf%lf",&ch,&pt[i].x,&pt[i].y),o.x+=pt[i].x,o.y+=pt[i].y;
o.x/=3.0,o.y/=3.0;
for(int i=;i<=;i++)
{
pt[i].a=atan2(pt[i].y-o.y,pt[i].x-o.x);
pt[i].b=getdis2(o,pt[i]);
st.insert(pt[i]);
}
set<Point>::iterator ia,ib,ic;
for(int i=;i<=q;i++)
{ scanf("%d%lf%lf",&ch,&tmp.x,&tmp.y);
tmp.a=atan2(tmp.y-o.y,tmp.x-o.x);
tmp.b=getdis2(tmp,o);
ia=st.lower_bound(tmp);
if(ia==st.end())ia=st.begin();
ib=L(ia);
if(ch==)
{
if(sgn(cross(*ib,tmp,*ia),)<=)
continue;
st.insert(tmp);
ic=st.find(tmp);
ia=L(ic),ib=L(ia);
while(sgn(cross(*ib,*ia,*ic),)<=)
{
st.erase(ia);
ia=ib,ib=L(ia);
}
ia=R(ic),ib=R(ia);
while(sgn(cross(*ib,*ia,*ic),)>=)
{
st.erase(ia);
ia=ib,ib=R(ia);
}
}
else
{
if(sgn(cross(*ib,tmp,*ia),)<=)
printf("YES\n");
else
printf("NO\n");
}
}
return ;
} int sgn(double ta,double tb)
{
if(fabs(ta-tb)<eps)return ;
if(ta<tb) return -;
return ;
}
double cross(Point ta,Point tb,Point tc)
{
return (tb.x-ta.x)*(tc.y-ta.y)-(tb.y-ta.y)*(tc.x-ta.x);
}
double getdis2(const Point &ta,const Point &tb)
{
return (ta.x-tb.x)*(ta.x-tb.x)+(ta.y-tb.y)*(ta.y-tb.y);
}
iter L(iter it)
{
if(it==st.begin())it=st.end();
return --it;
}
iter R(iter it)
{
if(++it==st.end()) it=st.begin();
return it;
}

codeforces 70 D. Professor's task 动态凸包的更多相关文章

  1. codeforces 70D Professor's task(动态二维凸包)

    题目链接:http://codeforces.com/contest/70/problem/D Once a walrus professor Plato asked his programming ...

  2. CF70D Professor's task(动态凸包)

    题面 两种操作: 1 往点集S中添加一个点(x,y); 2 询问(x,y)是否在点集S的凸包中. 数据保证至少有一个2操作, 保证刚开始会给出三个1操作, 且这三个操作中的点不共线. 题解 动态凸包板 ...

  3. Codeforces Beta Round #64D - Professor's task

    题意:两种操作1.加点2.查询点是否在之前给定点的凸包内 题解:set维护动态凸包,分别维护上下凸壳,对y取反就行,判断点是否在凸壳内,把点加进去看要不要删除就好了 //#pragma GCC opt ...

  4. 【BZOJ 2300】 2300: [HAOI2011]防线修建 (动态凸包+set)

    2300: [HAOI2011]防线修建 Description 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可是A国上 ...

  5. BZOJ 2300: [HAOI2011]防线修建( 动态凸包 )

    离线然后倒着做就变成了支持加点的动态凸包...用平衡树维护上凸壳...时间复杂度O(NlogN) --------------------------------------------------- ...

  6. [NOI2007]货币兑换Cash(DP+动态凸包)

    第一次打动态凸包维护dp,感觉学到了超级多的东西. 首先,set是如此的好用!!!可以通过控制一个flag来实现两种查询,维护凸包和查找斜率k 不过就是重载运算符和一些细节方面有些恶心,90行解决 后 ...

  7. BZOJ [HAOI2011]防线修建(动态凸包)

    听说有一种很高端的东西叫动态凸包维护dp就像学一下,不过介于本人还不会动态凸包就去学了下,还是挺神奇的说,维护上下凸包的写法虽然打得有点多不过也只是维护复制黏贴的事情而已罢了. 先说下动态凸包怎么写吧 ...

  8. 【BZOJ 1701】Cow School(斜率优化/动态凸包/分治优化)

    原题题解和数据下载 Usaco2007 Jan 题意 小牛参加了n个测试,第i个测试满分是\(p_i\),它的得分是\(t_i\).老师去掉\(t_i/p_i\)最小的d个测试,将剩下的总得分/总满分 ...

  9. Code Chef TSUM2(动态凸包+点分治)

    题面 传送门 题解 真是毒瘤随机化算法居然一分都不给 首先这种树上的题目一般想到的都是点分 我们考虑如何统计经过当前点的路径的贡献,设当前点\(u\)在序列中是第\(c\)个,那么一条路径的贡献就是 ...

随机推荐

  1. 《Linux实验要求》

    实验 1:登录和使用基本的 Linux 命令 实验环境: 安装了 Red Hat Enterprise Linux 6.0 可运行系统,并且是成功验证系统. 有另外一个无特权用户 student,密码 ...

  2. lua基础(一)

    参考链接: http://blog.csdn.net/lyh916/article/details/49719697 一.注释 --这是行注释 --[[ 这是块注释 这是块注释 这是块注释 --]] ...

  3. android Contacts/Acore进程常常被Kill,导致联系人开机后丢失怎么办?

    Contacts/Acore进程,在内存较少和开机进程过多的情况下会常常被 ActivityManager Kill 掉. 导致Sim卡联系人开机后未导入或者仅仅导入一部分,造成联系人丢失的现象,可是 ...

  4. Hibernate_day01--课程安排_Hibernate概述_Hibernate入门

    Hibernate_day01 Hibernate课程安排 今天内容介绍 WEB内容回顾 JavaEE三层结构 MVC思想 Hibernate概述 什么是框架 什么是hibernate框架(重点) 什 ...

  5. 将list列表中unicode类型的值转换为字符串类型

  6. KVC(Key-Value-Coding)和KVO(Key-Value-Observer)

    KVC(Key-Value-Coding)和KVO(Key-Value-Observer) 目录 概述 KVC的基本用法 KVC的运用 KVO的基本用法 KVO的运用 概述 键-值编码是一个用于间接访 ...

  7. 160414、java上传文件以流方式判断类型

    public enum FileType {       /**        * JEPG.        */       JPEG("FFD8FF"),          / ...

  8. python中lambda使用

    一.lambda函数 1.lambda函数基础: lambda函数也叫匿名函数,即,函数没有具体的名称,而用def创建的方法是有名称的.如下: """命名的foo函数&q ...

  9. 自定义HTTP头时的注意事项(转)

    原文:https://blog.gnuers.org/?p=462 HTTP头是可以包含英文字母([A-Za-z]).数字([0-9]).连接号(-)hyphens, 也可义是下划线(_).在使用ng ...

  10. LINux网络的NAPI机制详解一

    在查看NAPI机制的时候发现一篇介绍NAPI引入初衷的文章写的很好,通俗易懂,就想要分享下,重要的是博主还做了可以在他基础上任意修改,而并不用注明出处的声明,着实令我敬佩,不过还是附上原文链接! ht ...