답안 #955826

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
955826 2024-03-31T13:30:25 Z chrisvilches Dragon 2 (JOI17_dragon2) C++14
15 / 100
4000 ms 3488 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

// TODO: New index category: point inside polygon (log N), and polygon tangent.

ll Bx, By;

struct Point {
  ll x, y;
  Point operator-(const Point p) const { return {x - p.x, y - p.y}; }
  Point operator+(const Point p) const { return {x + p.x, y + p.y}; }
  ll cross(const Point p) const { return x * p.y - y * p.x; }
  bool operator<(const Point p) const { return to_upper().cross(p.to_upper()) > 0; }
  Point to_upper() const { return above() ? *this : negate(); }
  bool above() const {
    const Point B{Bx, By};
    return B.cross(*this) > 0;
  }
  ll operator*(const Point p) const { return x * p.x + y * p.y; }
  bool deg0(const Point p) const { return cross(p) == 0 && *this * p >= 0; }
  bool deg180(const Point p) const { return cross(p) == 0 && *this * p < 0; }
  Point negate() const { return {-x, -y}; }
};
ostream& operator<<(ostream& os, const Point& p) {
  return os << "(" << p.x << ", " << p.y << ")";
}

short orientation(const Point o, const Point a, const Point b) {
  const ll cross = (a - o).cross(b - o);
  assert(cross != 0);
  return (cross > 0) - (cross < 0);
}

map<int, vector<Point>> ordered_by_b;

bool cmp_by_b(Point p, Point q) {
  p.x -= Bx;
  q.x -= Bx;
  p.y -= By;
  q.y -= By;
  const bool a1 = p.above();
  const bool a2 = q.above();
  if (a1 != a2) return a1;
  return p.cross(q) > 0;
}

map<pair<ll, ll>, int> point_idx;

int get_point_idx(const Point p) {
  const pair<ll, ll> val{p.x, p.y};
  return point_idx.at(val);
}

int handle_query(const vector<Point>& points1, const vector<Point>& points2,
                 const vector<Point>& points2_sorted_by_b, const Point B) {
  vector<int> active(points2.size(), 0);

  for (const auto q : points2) {
    if (!q.above()) {
      active[get_point_idx(q)] = true;
    }
  }

  int total = 0;

  int j = 0;
  for (const Point p : points1) {
    while (j < (int)points2.size()) {
      const Point q = points2[j];
      const int idx = get_point_idx(q);

      if (q.to_upper().cross(p.to_upper()) < 0) {
        break;
      }
      if (q.above()) {
        assert(!active[idx]);
        active[idx] = 1;
        assert(active[idx]);
      } else {
        assert(active[idx]);
        active[idx] = 0;
        assert(!active[idx]);
      }
      j++;
    }

    if (p.above()) {
      auto it1 = lower_bound(points2_sorted_by_b.begin(), points2_sorted_by_b.end(), p,
                             cmp_by_b);

      const Point B{Bx, By};
      const Point p2 = B + (p - B).negate();
      auto it2 = lower_bound(points2_sorted_by_b.begin(), points2_sorted_by_b.end(), p2,
                             cmp_by_b);
      // const auto it2 = lower_bound(ordered_by_b.begin(), ordered_by_b.end(), p,
      // cmp_by_b);

      while (it1 != it2) {
        const Point q = *it1;
        const int idx = get_point_idx(q);
        total += active[idx];
        it1++;
      }
    } else {
      // TODO: (Brute force)
      for (const Point q : points2) {
        // if (p.above()) {
        //   if (orientation(B, p, q) > 0) total += active[get_point_idx(q)];
        // } else {
        if (orientation(p, B, q) > 0) total += active[get_point_idx(q)];
        // }
      }
    }
  }

  return total;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int N, M, Q;

  while (cin >> N >> M) {
    vector<Point> points;
    unordered_map<int, vector<Point>> tribe_points;
    for (int i = 0; i < N; i++) {
      Point p;
      int tribe;
      cin >> p.x >> p.y >> tribe;
      tribe_points[tribe].push_back(p);
    }

    Point A;
    cin >> A.x >> A.y;
    cin >> Bx >> By;
    cin >> Q;
    Bx -= A.x;
    By -= A.y;
    const Point B{Bx, By};

    for (auto& [_, points] : tribe_points) {
      for (auto& p : points) {
        p = p - A;
      }

      sort(points.begin(), points.end());
    }

    for (auto [tribe_idx, points] : tribe_points) {
      sort(points.begin(), points.end(), cmp_by_b);

      for (int i = 0; i < (int)points.size(); i++) {
        const pair<ll, ll> val{points[i].x, points[i].y};
        point_idx[val] = i;
      }

      ordered_by_b[tribe_idx] = points;
    }

    while (Q--) {
      int i, j;
      cin >> i >> j;
      cout << handle_query(tribe_points[i], tribe_points[j], ordered_by_b[j], B) << '\n';
    }
  }
}

Compilation message

dragon2.cpp: In function 'int main()':
dragon2.cpp:143:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  143 |     for (auto& [_, points] : tribe_points) {
      |                ^
dragon2.cpp:151:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  151 |     for (auto [tribe_idx, points] : tribe_points) {
      |               ^
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 604 KB Output is correct
2 Correct 70 ms 604 KB Output is correct
3 Correct 127 ms 784 KB Output is correct
4 Correct 118 ms 856 KB Output is correct
5 Correct 50 ms 1364 KB Output is correct
6 Correct 3 ms 1116 KB Output is correct
7 Correct 3 ms 1116 KB Output is correct
8 Correct 158 ms 604 KB Output is correct
9 Correct 39 ms 600 KB Output is correct
10 Correct 96 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4075 ms 3488 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 604 KB Output is correct
2 Correct 70 ms 604 KB Output is correct
3 Correct 127 ms 784 KB Output is correct
4 Correct 118 ms 856 KB Output is correct
5 Correct 50 ms 1364 KB Output is correct
6 Correct 3 ms 1116 KB Output is correct
7 Correct 3 ms 1116 KB Output is correct
8 Correct 158 ms 604 KB Output is correct
9 Correct 39 ms 600 KB Output is correct
10 Correct 96 ms 604 KB Output is correct
11 Execution timed out 4075 ms 3488 KB Time limit exceeded
12 Halted 0 ms 0 KB -