답안 #955983

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
955983 2024-03-31T18:47:09 Z chrisvilches Dragon 2 (JOI17_dragon2) C++14
100 / 100
2145 ms 4792 KB
#include <bits/stdc++.h>
using namespace std;
using ll = int;

ll Bx, By;

struct Point {
  ll x, y;
  int idx;
  inline Point operator-(const Point& p) const { return {x - p.x, y - p.y, idx}; }
  inline Point operator+(const Point& p) const { return {x + p.x, y + p.y, idx}; }
  inline long long cross(const Point& p) const {
    return x * (long long)p.y - y * (long long)p.x;
  }
  inline bool operator<(const Point& p) const {
    return to_upper().cross(p.to_upper()) > 0;
  }
  inline Point to_upper() const { return above() ? *this : negate(); }
  inline bool above() const {
    // TODO: Improve this
    // const Point B{Bx, By, idx};
    return Bx * (long long)y - By * (long long)x > 0;
    // return B.cross(*this) > 0;
  }
  inline Point negate() const { return {-x, -y, idx}; }
};

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

short bit[30'001];
int bit_n = 30'001;

void clear(const int n) { memset(bit, 0, sizeof(short) * n); }

// TODO: Return should be int, not short.
int sum_single(int r) {
  int ret = 0;
  for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];
  return ret;
}

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

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

unordered_map<int, vector<Point>> order_by_b;

bool cmp_by_b(const Point& p, const Point& q) {
  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_n = (int)points2.size();
  clear(points2.size());

  for (const auto& q : points2) {
    if (!q.above()) {
      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 < p)) break;
      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 - B, cmp_by_b);
    const auto it2 = lower_bound(ord_b.begin(), ord_b.end(), to_point - B, cmp_by_b);

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

    total += 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;
    }

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

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

      for (auto& p : tribe_points.at(m)) p = p - B;
      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& p : tribe_points.at(m)) p = p + B;
    }

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

    const Point origin{0, 0, -1};

    // TODO: Are the fenwick queries actually faster???? DO some experiments

    // TODO: Set this value more properly, and explain that this doesn't really help
    //       but it's something. (assuming fenwick is ACTUALLY faster)

    // TODO: Comment that this solution is probably not the intended one. The example
    // solution is much faster (under a second).
    // TODO: The thing is, I don't want to leave this code without the radial sweep, I
    // want to add it for the lulz.
    const int big_query = N / 2;


    while (Q--) {
      int i, j;
      cin >> i >> j;
      // cerr << tribe_points[i].size() * tribe_points[j].size() << endl;
      const long long size = tribe_points[i].size() * tribe_points[j].size();

      if (size > 100'000) {
        // cerr << "big" << endl;
        // cerr << tribe_points[j].size() << " > " << big_query << endl;
        const int ans =
            handle_query(tribe_points.at(i), tribe_points.at(j), order_by_b.at(j), B);
        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';
      }
    }
  
  }
}

Compilation message

dragon2.cpp: In function 'int main()':
dragon2.cpp:162:15: warning: unused variable 'big_query' [-Wunused-variable]
  162 |     const int big_query = N / 2;
      |               ^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 14 ms 348 KB Output is correct
3 Correct 20 ms 580 KB Output is correct
4 Correct 26 ms 784 KB Output is correct
5 Correct 25 ms 860 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 1172 KB Output is correct
2 Correct 43 ms 1312 KB Output is correct
3 Correct 35 ms 1372 KB Output is correct
4 Correct 13 ms 1368 KB Output is correct
5 Correct 13 ms 4024 KB Output is correct
6 Correct 17 ms 1140 KB Output is correct
7 Correct 16 ms 1500 KB Output is correct
8 Correct 15 ms 1316 KB Output is correct
9 Correct 14 ms 1244 KB Output is correct
10 Correct 9 ms 1212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 344 KB Output is correct
2 Correct 14 ms 348 KB Output is correct
3 Correct 20 ms 580 KB Output is correct
4 Correct 26 ms 784 KB Output is correct
5 Correct 25 ms 860 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 2 ms 860 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 17 ms 1172 KB Output is correct
12 Correct 43 ms 1312 KB Output is correct
13 Correct 35 ms 1372 KB Output is correct
14 Correct 13 ms 1368 KB Output is correct
15 Correct 13 ms 4024 KB Output is correct
16 Correct 17 ms 1140 KB Output is correct
17 Correct 16 ms 1500 KB Output is correct
18 Correct 15 ms 1316 KB Output is correct
19 Correct 14 ms 1244 KB Output is correct
20 Correct 9 ms 1212 KB Output is correct
21 Correct 16 ms 1244 KB Output is correct
22 Correct 42 ms 1116 KB Output is correct
23 Correct 1160 ms 1768 KB Output is correct
24 Correct 445 ms 1956 KB Output is correct
25 Correct 38 ms 2396 KB Output is correct
26 Correct 35 ms 4280 KB Output is correct
27 Correct 15 ms 4788 KB Output is correct
28 Correct 15 ms 4792 KB Output is correct
29 Correct 2145 ms 4576 KB Output is correct
30 Correct 68 ms 4184 KB Output is correct
31 Correct 34 ms 4020 KB Output is correct
32 Correct 51 ms 4272 KB Output is correct
33 Correct 471 ms 4168 KB Output is correct
34 Correct 34 ms 4272 KB Output is correct
35 Correct 33 ms 4268 KB Output is correct
36 Correct 36 ms 4280 KB Output is correct
37 Correct 35 ms 4276 KB Output is correct
38 Correct 742 ms 4640 KB Output is correct
39 Correct 598 ms 4268 KB Output is correct
40 Correct 486 ms 4408 KB Output is correct
41 Correct 1363 ms 4248 KB Output is correct
42 Correct 591 ms 4276 KB Output is correct
43 Correct 352 ms 4284 KB Output is correct
44 Correct 1235 ms 2940 KB Output is correct
45 Correct 667 ms 2960 KB Output is correct
46 Correct 379 ms 2780 KB Output is correct
47 Correct 757 ms 2652 KB Output is correct
48 Correct 581 ms 2752 KB Output is correct
49 Correct 297 ms 2952 KB Output is correct