Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <chrono>
  4. #include <random>
  5. #include <algorithm>
  6. #include <cassert>
  7. #include <set>
  8.  
  9. std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());
  10.  
  11. struct PT {
  12.     typedef long long T;
  13.     T x, y;
  14.     PT(T xx = 0, T yy = 0) : x(xx), y(yy){}
  15.     PT operator +(const PT &p) const { return PT(x+p.x,y+p.y); }
  16.     PT operator -(const PT &p) const { return PT(x-p.x,y-p.y); }
  17.     T operator *(const PT &p)  const { return x*p.x+y*p.y;     }
  18.     T operator %(const PT &p)  const { return x*p.y-y*p.x;     }
  19.     bool operator == (const PT &p) const { return x == p.x && y == p.y; }
  20.     bool operator < (const PT &p) const {
  21.         if(*this % p != 0) {
  22.             return *this % p > 0;
  23.         } else {
  24.             return *this * *this < p * p;
  25.         }
  26.     }
  27. };
  28.  
  29. int main() {
  30.     std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
  31.     int n;
  32.     std::cin >> n;
  33.     std::set<PT> hull;
  34.     for(int i = 0; i < n; i++) {
  35.         PT p;
  36.         std::cin >> p.x >> p.y;
  37.         if(i == 0) {
  38.             hull.insert(p);
  39.         } else {
  40.             // remove from begin
  41.             while(1) {
  42.                 auto it = hull.begin();
  43.                 if(it == hull.end()) break;
  44.                 if(it->x < p.x && it->y < p.y) {
  45.                     hull.erase(it);
  46.                 } else {
  47.                     break;
  48.                 }
  49.             }
  50.             while(1) {
  51.                 auto it = hull.end();
  52.                 if(it == hull.begin()) break;
  53.                 it--;
  54.                 if(it->x < p.x && it->y < p.y) {
  55.                     hull.erase(it);
  56.                 } else {
  57.                     break;
  58.                 }
  59.             }
  60.             // remove from right
  61.             while(1) {
  62.                 auto it = hull.lower_bound(p);
  63.                 if(it == hull.end()) break;
  64.                 if(it->x < p.x && it->y < p.y) {
  65.                     hull.erase(it);
  66.                 } else {
  67.                     break;
  68.                 }
  69.             }
  70.             while(1) {
  71.                 auto it = hull.lower_bound(p);
  72.                 if(it == hull.end()) break;
  73.                 auto it2 = it;
  74.                 it2++;
  75.                 if(it2 == hull.end()) break;
  76.                 if((*it-p) % (*it2-p) < 0) {
  77.                     hull.erase(it);
  78.                 } else {
  79.                     break;
  80.                 }
  81.             }
  82.             // remove from left
  83.             while(1) {
  84.                 auto it = hull.lower_bound(p);
  85.                 if(it == hull.begin()) break;
  86.                 it--;
  87.                 if(it->x < p.x && it->y < p.y) {
  88.                     hull.erase(it);
  89.                 } else {
  90.                     break;
  91.                 }
  92.             }
  93.             while(1) {
  94.                 auto it = hull.lower_bound(p);
  95.                 if(it == hull.begin()) break;
  96.                 it--;
  97.                 auto it2 = it;
  98.                 if(it2 == hull.begin()) break;
  99.                 it2--;
  100.                 if((*it-p) % (*it2-p) > 0) {
  101.                     hull.erase(it);
  102.                 } else {
  103.                     break;
  104.                 }
  105.             }
  106.             // check to insert
  107.             bool valid = true;
  108.             if(hull.size() > 0) {
  109.                 auto it = hull.lower_bound(p);
  110.                 auto it2 = it;
  111.                 if(it == hull.begin()) {
  112.                     // check if it is greater than p
  113.                     valid = valid && (it->x <= p.x);
  114.                 } else if(it == hull.end()) {
  115.                     it--;
  116.                     valid = valid && (it->y <= p.y);
  117.                 } else {
  118.                     it2--;
  119.                     valid = valid && (p-*it2) % (*it-*it2) >= 0;
  120.                 }
  121.             }
  122.             if(valid) {
  123.                 hull.insert(p);
  124.             }
  125.         }
  126.         std::cout << i + 1 - (int) hull.size() << '\n';
  127.     }
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement