Submission #955942

# Submission time Handle Problem Language Result Execution time Memory
955942 2024-03-31T17:50:30 Z chrisvilches Dragon 2 (JOI17_dragon2) C++14
60 / 100
4000 ms 4904 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, idx};
    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, -1}; }
};

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

short bit[30'001];

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

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

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

void add(int idx, const short delta) {
  for (; idx < 30'001; 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;

  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.to_upper().cross(p.to_upper()) < 0) 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());
    }

    // 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 ((tribe_points[i].size() > 14900 || tribe_points[j].size() > 14900)) {
        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';
      }
    }
  }
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 604 KB Output is correct
2 Correct 13 ms 648 KB Output is correct
3 Correct 18 ms 604 KB Output is correct
4 Correct 24 ms 856 KB Output is correct
5 Correct 22 ms 860 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 2 ms 856 KB Output is correct
8 Correct 8 ms 600 KB Output is correct
9 Correct 7 ms 604 KB Output is correct
10 Correct 7 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 2016 KB Output is correct
2 Correct 1075 ms 2052 KB Output is correct
3 Correct 35 ms 2396 KB Output is correct
4 Correct 11 ms 2136 KB Output is correct
5 Correct 18 ms 4792 KB Output is correct
6 Correct 13 ms 2168 KB Output is correct
7 Correct 13 ms 2164 KB Output is correct
8 Correct 20 ms 1876 KB Output is correct
9 Correct 10 ms 1880 KB Output is correct
10 Correct 10 ms 1920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 604 KB Output is correct
2 Correct 13 ms 648 KB Output is correct
3 Correct 18 ms 604 KB Output is correct
4 Correct 24 ms 856 KB Output is correct
5 Correct 22 ms 860 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 2 ms 856 KB Output is correct
8 Correct 8 ms 600 KB Output is correct
9 Correct 7 ms 604 KB Output is correct
10 Correct 7 ms 856 KB Output is correct
11 Correct 19 ms 2016 KB Output is correct
12 Correct 1075 ms 2052 KB Output is correct
13 Correct 35 ms 2396 KB Output is correct
14 Correct 11 ms 2136 KB Output is correct
15 Correct 18 ms 4792 KB Output is correct
16 Correct 13 ms 2168 KB Output is correct
17 Correct 13 ms 2164 KB Output is correct
18 Correct 20 ms 1876 KB Output is correct
19 Correct 10 ms 1880 KB Output is correct
20 Correct 10 ms 1920 KB Output is correct
21 Correct 17 ms 2028 KB Output is correct
22 Correct 1160 ms 2016 KB Output is correct
23 Correct 1211 ms 2732 KB Output is correct
24 Correct 424 ms 2516 KB Output is correct
25 Correct 39 ms 3164 KB Output is correct
26 Correct 36 ms 4788 KB Output is correct
27 Correct 15 ms 4792 KB Output is correct
28 Correct 15 ms 4732 KB Output is correct
29 Execution timed out 4051 ms 4904 KB Time limit exceeded
30 Halted 0 ms 0 KB -