地址: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. JQuery------图片幻灯片插件

    下载地址: http://www.jq22.com/jquery-info36

  2. Eclipse配置Tomcat并运行

    这篇文章介绍Eclipse配置tomcat.我们假设已经安装好JDK并且配置好了JDK的环境变量.然后我们需要下载并安装Eclipse和tomcat:Eclipse:http://www.eclips ...

  3. 【BZOJ1441】Min 拓展裴蜀定理

    [BZOJ1441]Min Description 给出n个数(A1...An)现求一组整数序列(X1...Xn)使得S=A1*X1+...An*Xn>0,且S的值最小 Input 第一行给出数 ...

  4. springmvc常用注解标签详解(转载)

    1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...

  5. 事件处理程序中 this 的指向

    js 高程 事件处理程序 章节原话(斜体表示): 13.2.1 HTML事件处理程序 在这个函数内部,this 值等于事件的目标元素,例如: <!-- 输出 "Click Me&quo ...

  6. 转发URL请求

    如何用Netty实现一个轻量级的HTTP代理服务器 - Newland - 博客园 https://www.cnblogs.com/jietang/p/8926325.html 现在迫切希望有一个HT ...

  7. mysql5.7.22在centos7.5下的安装

    1.下载,解压 把下载的文件放到 /app/programs/目录下 tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz 地址:https://d ...

  8. requests和bs4

    requests模块,仿造浏览器发送Http请求bs4主要对html或xml格式字符串解析成对象,使用find/find_all查找 text/attrs 爬取汽车之家 爬取汽车之家的资讯信息,它没有 ...

  9. 找新朋友---hdu1286(欧拉函数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1286 欧拉函数:对正整数n,欧拉函数是求少于n的数中与n互质的数的数目: 素数(质数)指在一个大于1的 ...

  10. Period II---fzu1901(Next数组)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1901 给你一个字符串 s 求出所有满足s[i] == s[i+p] ( 0 < i+p < len ...