## Description:

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.

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

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

## Sample Output:

YES
YES
YES
NO

### 题目链接

1. 1 x y 向凸包集内添加一个点 $(x,y)$
2. 2 x y 询问点 $(x,y)$ 是否在凸包集 $s$ 的凸包内部（包含边界）

$1$ 的插入操作有三种情况

1. 若向 $s$ 内添加点 $F$ ，由于 $F$ 已经在凸包内部，所以不用添加
2. 若向 $s$ 内添加点 $G$ ，由于 $G$ 不影响凸包其它点的构成，直接添加即可
3. 若向 $s$ 内添加点 $H$ ，由于 $H$ 添加后点 $B$ 就不属于凸包集凸包上的构成点，所以需要删除点 $B$

## AC代码:

#include <bits/stdc++.h>
using namespace std;

typedef double db;
const int maxn = 1e5 + 5;
const db eps = 1e-9;

int Sgn(db Key) {return fabs(Key) < eps ? 0 : (Key < 0 ? -1 : 1);}
int Cmp(db Key1, db Key2) {return Sgn(Key1 - Key2);}
struct Point {db X, Y;};
typedef Point Vector;
Vector operator - (Vector Key1, Vector Key2) {return (Vector){Key1.X - Key2.X, Key1.Y - Key2.Y};}
Vector operator + (Vector Key1, Vector Key2) {return (Vector){Key1.X + Key2.X, Key1.Y + Key2.Y};}
db operator * (Vector Key1, Vector Key2) {return Key1.X * Key2.X + Key1.Y * Key2.Y;}
db operator ^ (Vector Key1, Vector Key2) {return Key1.X * Key2.Y - Key1.Y * Key2.X;}
db GetLen(Vector Key) {return sqrt(Key * Key);}

int N;
Point Basic;
Point points[maxn];
set<Point> S;

bool operator < (Point Key1, Point Key2) {
Key1 = Key1 - Basic; Key2 = Key2 - Basic;
db Ang1 = atan2(Key1.Y, Key1.X), Ang2 = atan2(Key2.Y, Key2.X);
db Len1 = GetLen(Key1), Len2 = GetLen(Key2);
if (Cmp(Ang1, Ang2) != 0) return Cmp(Ang1, Ang2) < 0;
return Cmp(Len1, Len2) < 0;
}

set<Point>::iterator Prev(set<Point>::iterator Key) {
if (Key == S.begin()) Key = S.end();
return --Key;
}

set<Point>::iterator Next(set<Point>::iterator Key) {
++Key;
return Key == S.end() ? S.begin() : Key;
}

bool Query(Point Key) {
set<Point>::iterator it = S.lower_bound(Key);
if (it == S.end()) it = S.begin();
return Sgn((Key - *(Prev(it))) ^ (*(it) - *(Prev(it)))) <= 0;
}

void Insert(Point Key) {
if (Query(Key)) return;
S.insert(Key);
set<Point>::iterator Cur = Next(S.find(Key));
while (S.size() > 3 && Sgn((Key - *(Next(Cur))) ^ (*(Cur) - *(Next(Cur)))) <= 0) {
S.erase(Cur);
Cur = Next(S.find(Key));
}
Cur = Prev(S.find(Key));
while (S.size() > 3 && Sgn((Key - *(Cur)) ^ (*(Cur) - *(Prev(Cur)))) >= 0) {
S.erase(Cur);
Cur = Prev(S.find(Key));
}
}

int main(int argc, char *argv[]) {
scanf("%d", &N);
Basic.X = Basic.Y = 0.0;
for (int i = 1, T; i <= 3; ++i) {
scanf("%d%lf%lf", &T, &points[i].X, &points[i].Y);
Basic.X += points[i].X; Basic.Y += points[i].Y;
}
Basic.X /= 3.0; Basic.Y /= 3.0;
for (int i = 1; i <= 3; ++i) S.insert(points[i]);
for (int i = 4, T; i <= N; ++i) {
scanf("%d%lf%lf", &T, &points[i].X, &points[i].Y);
if (T == 1) Insert(points[i]);
else {
if (Query(points[i])) printf("YES\n");
else printf("NO\n");
}
}
return 0;
}
Last modification：March 27th, 2019 at 09:45 pm