zoukankan      html  css  js  c++  java
  • 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?

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

    题目大意:q个操作。操作1:往点集中添加一个点。操作2:给一个点,问这个点是否在点集所构成的凸包内部(包括边缘)。

    思路:增量法求动态凸包。可以看:http://blog.csdn.net/crazy_ac/article/details/8499443

    PS:原来map的迭代器是首尾相连的……

    代码(156MS):

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 #include <map>
     6 using namespace std;
     7 typedef long long LL;
     8 typedef map<int, int> MPII;
     9 
    10 struct Point {
    11     int x, y;
    12     Point() {}
    13     Point(int x, int y): x(x), y(y) {}
    14     Point(MPII::iterator it): x(it->first), y(it->second) {}
    15     Point operator - (const Point &rhs) const {
    16         return Point(x - rhs.x, y - rhs.y);
    17     }
    18 };
    19 
    20 LL cross(const Point &a, const Point &b) {
    21     return (LL)a.x * b.y - (LL)a.y * b.x;
    22 }
    23 
    24 LL cross(const Point &o, const Point &a, const Point &b) {
    25     return cross(a - o, b - o);
    26 }
    27 
    28 bool below(MPII &mp, Point p) {
    29     if(mp.empty()) return false;
    30     if(p.x < mp.begin()->first || mp.rbegin()->first < p.x) return false;
    31     MPII::iterator a = mp.lower_bound(p.x), b = a--;
    32     if(b->first == p.x) return b->second >= p.y;
    33     return cross(a, b, p) <= 0;
    34 }
    35 
    36 void insert(MPII &mp, Point p) {
    37     if(below(mp, p)) return ;
    38     MPII::iterator a, b, it;
    39     mp[p.x] = p.y;
    40     it = mp.lower_bound(p.x);
    41 
    42     b = it; ++b;
    43     if(b != mp.end()) {
    44         a = b++;
    45         while(b != mp.end() && cross(p, a, b) >= 0)
    46             mp.erase(a), a = b++;
    47     }
    48 
    49     a = it; --a;
    50     if(it != mp.begin() && a != mp.begin()) {
    51         b = a--;
    52         while(b != mp.begin() && cross(p, b, a) <= 0)
    53             mp.erase(b), b = a--;
    54     }
    55 }
    56 
    57 MPII up, down;
    58 int q, op, x, y;
    59 
    60 int main() {
    61     scanf("%d", &q);
    62     while(q--) {
    63         scanf("%d%d%d", &op, &x, &y);
    64         if(op == 1) {
    65             insert(up, Point(x, y));
    66             insert(down, Point(x, -y));
    67         } else {
    68             puts((below(up, Point(x, y)) && below(down, Point(x, -y))) ? "YES" : "NO");
    69         }
    70     }
    71 }
    View Code
  • 相关阅读:
    《人类简史》八、融合统一(下)——宗教的法则、历史的混沌
    《今日简史》七、融合统一(中)——帝国的愿景
    《人类简史》六、融合统一(上)——历史的方向、金钱的味道
    《人类简史》五、监狱高墙——想象构建的秩序
    设计模式之职责链模式(Chain of Responsibility)
    设计模式之代理模式(Proxy)
    设计模式之享元模式(FlyWeight)
    设计模式之外观模式(Facade)
    设计模式之装饰模式(Decorator)
    设计模式之组合模式(Composite)
  • 原文地址:https://www.cnblogs.com/oyking/p/3910539.html
Copyright © 2011-2022 走看看