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
  • 相关阅读:
    玩懂Log,打开Android大门(sundy深入浅出)之一
    ListView 中getView的原理+如何在ListView中放置多个item(android.widget.ListView)
    验证视图状态MAC失败问题正确的解决办法
    Coolite Extjs Store开发心得(转)
    Delphi进制转换
    得到Exitjs DataView中图片文件名
    C#文件常用操作
    Delphi中TList类应用
    代码优化的第一步是判定程序热点(转)
    Asp.net性能优化
  • 原文地址:https://www.cnblogs.com/oyking/p/3910539.html
Copyright © 2011-2022 走看看