답안 #955892

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
955892 2024-03-31T16:16:09 Z chrisvilches Dragon 2 (JOI17_dragon2) C++14
60 / 100
4000 ms 12044 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}; }
};

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;

      if (q.above()) {
        active.add(q.idx, 1);
      } else {
        active.add(q.idx, -1);
      }
      j++;
    }

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

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

    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<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;

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

    for (auto& [tribe_idx, points] : tribe_points) {
      sort(points.begin(), points.end(), cmp_by_b);
      for (int i = 0; i < (int)points.size(); i++) points[i].idx = i;
      order_by_b[tribe_idx] = points;
    }

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

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

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

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

      const int ans = handle_query(tribe_points[i], tribe_points[j], order_by_b[j], B);
      memo[{i, j}] = ans;
      cout << ans << '\n';
    }
  }
}

Compilation message

dragon2.cpp: In function 'int main()':
dragon2.cpp:130:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  130 |     for (auto& [_, points] : tribe_points) {
      |                ^
dragon2.cpp:134:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  134 |     for (auto& [tribe_idx, points] : tribe_points) {
      |                ^
dragon2.cpp:140:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  140 |     for (auto& [_, points] : tribe_points) {
      |                ^
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 600 KB Output is correct
2 Correct 4 ms 604 KB Output is correct
3 Correct 22 ms 976 KB Output is correct
4 Correct 88 ms 6996 KB Output is correct
5 Correct 77 ms 7284 KB Output is correct
6 Correct 2 ms 856 KB Output is correct
7 Correct 2 ms 864 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 1860 KB Output is correct
2 Correct 53 ms 2052 KB Output is correct
3 Correct 19 ms 2392 KB Output is correct
4 Correct 12 ms 2140 KB Output is correct
5 Correct 15 ms 4384 KB Output is correct
6 Correct 15 ms 2168 KB Output is correct
7 Correct 15 ms 2168 KB Output is correct
8 Correct 16 ms 1872 KB Output is correct
9 Correct 11 ms 1880 KB Output is correct
10 Correct 11 ms 1876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 600 KB Output is correct
2 Correct 4 ms 604 KB Output is correct
3 Correct 22 ms 976 KB Output is correct
4 Correct 88 ms 6996 KB Output is correct
5 Correct 77 ms 7284 KB Output is correct
6 Correct 2 ms 856 KB Output is correct
7 Correct 2 ms 864 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 2 ms 604 KB Output is correct
10 Correct 2 ms 600 KB Output is correct
11 Correct 18 ms 1860 KB Output is correct
12 Correct 53 ms 2052 KB Output is correct
13 Correct 19 ms 2392 KB Output is correct
14 Correct 12 ms 2140 KB Output is correct
15 Correct 15 ms 4384 KB Output is correct
16 Correct 15 ms 2168 KB Output is correct
17 Correct 15 ms 2168 KB Output is correct
18 Correct 16 ms 1872 KB Output is correct
19 Correct 11 ms 1880 KB Output is correct
20 Correct 11 ms 1876 KB Output is correct
21 Correct 18 ms 2128 KB Output is correct
22 Correct 49 ms 1880 KB Output is correct
23 Correct 297 ms 2896 KB Output is correct
24 Correct 497 ms 8872 KB Output is correct
25 Correct 107 ms 9832 KB Output is correct
26 Correct 99 ms 12044 KB Output is correct
27 Correct 21 ms 6644 KB Output is correct
28 Correct 22 ms 6668 KB Output is correct
29 Execution timed out 4086 ms 9772 KB Time limit exceeded
30 Halted 0 ms 0 KB -