답안 #955918

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

ll Bx, By;

struct Point {
  ll x, y;
  int idx;
  Point operator-(const Point p) const { return {x - p.x, y - p.y, idx}; }
  Point operator+(const Point p) const { return {x + p.x, y + p.y, idx}; }
  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 {
    // TODO: Improve this
    const Point B{Bx, By, -1};
    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, idx}; }
};

short sgn(const ll x) { return (x > 0) - (x < 0); }

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

// template <class T>
// struct BIT {
//   BIT(const int n) : n(n) { bit.assign(n, 0); }
//   // void clear(const int n) { memset(bit, 0, n * sizeof(T)); }

//   T sum(int l, int r) { return sum(r) - sum(l - 1); }

//   void add(int idx, const T delta) {
//     for (; idx < n; idx = idx | (idx + 1)) bit[idx] += delta;
//   }

//  private:
//   int n;
//   vector<T> bit;

//   T sum(int r) {
//     T ret = 0;
//     for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];
//     return ret;
//   }
// };

// unordered_map<int, vector<Point>> order_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;
// }

// int handle_query(const vector<Point>& points1, const vector<Point>& points2,
//                  const vector<Point>& ord_b, const Point B) {
//   if (points1.empty() || points2.empty()) return 0;

//   BIT<short> active(points2.size());

//   for (const auto q : points2) {
//     if (!q.above()) {
//       active.add(q.idx, 1);
//     }
//   }

//   int total = 0;

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

//       if (q.to_upper().cross(p.to_upper()) < 0) break;
//       active.add(q.idx, q.above() ? 1 : -1);

//       j++;
//     }

//     Point from_point = p;
//     Point to_point = B + (p - B).negate();

//     if (!p.above()) swap(from_point, to_point);

//     // total += 1;
//     // continue;

//     const auto it1 = lower_bound(ord_b.begin(), ord_b.end(), from_point, cmp_by_b);
//     const auto it2 = lower_bound(ord_b.begin(), ord_b.end(), to_point, cmp_by_b);

//     const int from = it1 - ord_b.begin();
//     const int to = it2 - ord_b.begin();

//     total += active.sum(from, to - 1);
//   }

//   return total;
// }

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

  while (cin >> N >> M) {
    vector<vector<Point>> tribe_points(M + 1);
    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;

    // for (auto& points : tribe_points) {
    //   for (auto& p : points) p = p - A;
    // }

    // for (int m = 0; m <= M; m++) {
    //   auto& points = tribe_points[m];

    //   sort(points.begin(), points.end(), cmp_by_b);
    //   for (int i = 0; i < (int)points.size(); i++) {
    //     points[i].idx = i;
    //   }
    //   order_by_b[m] = tribe_points.at(m);
    // }

    // for (auto& points : tribe_points) {
    //   sort(points.begin(), points.end());
    // }

    const Point B{Bx, By, -1};

    // map<pair<int, int>, int> memo;

    // const Point origin{0, 0, -1};
    const Point origin = A;

    while (Q--) {
      int i, j;
      cin >> i >> j;
      // if (memo.count({i, j})) {
      //   cout << memo.count({i, j}) << '\n';
      //   continue;
      // }

      // if (false && (tribe_points[i].size() > 1000 || tribe_points[j].size() > 1000)) {
      //   const int ans =
      //       handle_query(tribe_points.at(i), tribe_points.at(j), order_by_b.at(j), B);
      //   memo[{i, j}] = ans;
      //   cout << ans << '\n';
      // } else {
      int total = 0;
      for (const Point p : tribe_points[i]) {
        for (const Point q : tribe_points[j]) {
          if (orientation(origin, B, p) == 1) {
            if (orientation(B, p, q) == 1 && orientation(p, origin, q) == 1) total++;
          } else {
            if (orientation(origin, p, q) == 1 && orientation(p, B, q) == 1) total++;
          }
        }
      }
      cout << total << '\n';
    }
    // }
  }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 348 KB Output is correct
2 Correct 19 ms 344 KB Output is correct
3 Correct 36 ms 568 KB Output is correct
4 Correct 175 ms 932 KB Output is correct
5 Correct 184 ms 600 KB Output is correct
6 Correct 5 ms 600 KB Output is correct
7 Correct 5 ms 348 KB Output is correct
8 Correct 10 ms 604 KB Output is correct
9 Correct 8 ms 348 KB Output is correct
10 Correct 9 ms 584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1110 ms 1260 KB Output is correct
2 Correct 2007 ms 1464 KB Output is correct
3 Correct 56 ms 1588 KB Output is correct
4 Correct 24 ms 1616 KB Output is correct
5 Correct 28 ms 2104 KB Output is correct
6 Correct 1090 ms 1452 KB Output is correct
7 Correct 1000 ms 1444 KB Output is correct
8 Correct 634 ms 1108 KB Output is correct
9 Correct 588 ms 1364 KB Output is correct
10 Correct 636 ms 1352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 13 ms 348 KB Output is correct
2 Correct 19 ms 344 KB Output is correct
3 Correct 36 ms 568 KB Output is correct
4 Correct 175 ms 932 KB Output is correct
5 Correct 184 ms 600 KB Output is correct
6 Correct 5 ms 600 KB Output is correct
7 Correct 5 ms 348 KB Output is correct
8 Correct 10 ms 604 KB Output is correct
9 Correct 8 ms 348 KB Output is correct
10 Correct 9 ms 584 KB Output is correct
11 Correct 1110 ms 1260 KB Output is correct
12 Correct 2007 ms 1464 KB Output is correct
13 Correct 56 ms 1588 KB Output is correct
14 Correct 24 ms 1616 KB Output is correct
15 Correct 28 ms 2104 KB Output is correct
16 Correct 1090 ms 1452 KB Output is correct
17 Correct 1000 ms 1444 KB Output is correct
18 Correct 634 ms 1108 KB Output is correct
19 Correct 588 ms 1364 KB Output is correct
20 Correct 636 ms 1352 KB Output is correct
21 Correct 1079 ms 1260 KB Output is correct
22 Correct 2037 ms 1624 KB Output is correct
23 Correct 1738 ms 1992 KB Output is correct
24 Correct 669 ms 2128 KB Output is correct
25 Correct 186 ms 1872 KB Output is correct
26 Correct 195 ms 2116 KB Output is correct
27 Correct 48 ms 1992 KB Output is correct
28 Correct 51 ms 2092 KB Output is correct
29 Correct 2828 ms 2604 KB Output is correct
30 Correct 215 ms 3080 KB Output is correct
31 Correct 193 ms 3216 KB Output is correct
32 Correct 224 ms 3248 KB Output is correct
33 Correct 691 ms 3648 KB Output is correct
34 Correct 188 ms 3664 KB Output is correct
35 Correct 183 ms 3152 KB Output is correct
36 Correct 182 ms 3280 KB Output is correct
37 Correct 196 ms 3408 KB Output is correct
38 Correct 940 ms 3484 KB Output is correct
39 Correct 833 ms 3216 KB Output is correct
40 Correct 664 ms 3544 KB Output is correct
41 Correct 2299 ms 3420 KB Output is correct
42 Correct 1783 ms 3272 KB Output is correct
43 Correct 1378 ms 3432 KB Output is correct
44 Correct 1640 ms 2732 KB Output is correct
45 Correct 853 ms 2640 KB Output is correct
46 Correct 553 ms 2712 KB Output is correct
47 Correct 1138 ms 2704 KB Output is correct
48 Correct 686 ms 2428 KB Output is correct
49 Correct 446 ms 2588 KB Output is correct