Submission #955830

# Submission time Handle Problem Language Result Execution time Memory
955830 2024-03-31T14:02:46 Z chrisvilches Dragon 2 (JOI17_dragon2) C++14
60 / 100
4000 ms 14260 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}; }
};

template <class T, unsigned int N>
struct BIT {
  void clear(const int n) { fill(bit, bit + n, 0); }

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

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

 private:
  T bit[N];

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

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

BIT<int, 30'001> active;

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

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

  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;
        active.add(idx, 1);
        // assert(active[idx]);
      } else {
        // assert(active[idx]);
        // active[idx] = 0;
        // assert(active.sum == 1);
        active.add(idx, -1);
        // assert(!active[idx]);
      }
      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(points2_sorted_by_b.begin(), points2_sorted_by_b.end(),
                                 from_point, cmp_by_b);

    const auto it2 = lower_bound(points2_sorted_by_b.begin(), points2_sorted_by_b.end(),
                                 to_point, cmp_by_b);

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

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

    // for (int i = from; i < to; i++) {
    //   const Point q = points2_sorted_by_b[i];
    //   const int idx = get_point_idx(q);
    //   total += active.sum(i, i);
    //   // total += active[idx];
    // }
  }

  return total;
}

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

  // BIT<int, 100000> bit;

  // bit.add(3, 1);
  // bit.add(4, 1);
  // bit.add(9, 1);
  // cerr << bit.sum(2, 6) << endl;
  // assert(bit.sum(2, 6) == 2);
  // assert(bit.sum(2, 11) == 3);
  // bit.add(9, -1);
  // assert(bit.sum(2, 11) == 2);

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

    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], ordered_by_b[j], B);

      memo[{i, j}] = ans;
      cout << ans << '\n';
    }
  }
}

Compilation message

dragon2.cpp: In function 'int main()':
dragon2.cpp:175:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  175 |     for (auto& [_, points] : tribe_points) {
      |                ^
dragon2.cpp:183:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  183 |     for (auto [tribe_idx, points] : tribe_points) {
      |               ^
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 6 ms 604 KB Output is correct
3 Correct 48 ms 988 KB Output is correct
4 Correct 156 ms 7356 KB Output is correct
5 Correct 92 ms 7500 KB Output is correct
6 Correct 3 ms 1368 KB Output is correct
7 Correct 3 ms 1372 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 3 ms 600 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 3500 KB Output is correct
2 Correct 95 ms 3420 KB Output is correct
3 Correct 32 ms 3672 KB Output is correct
4 Correct 20 ms 3676 KB Output is correct
5 Correct 26 ms 6348 KB Output is correct
6 Correct 22 ms 3540 KB Output is correct
7 Correct 24 ms 3540 KB Output is correct
8 Correct 22 ms 3540 KB Output is correct
9 Correct 16 ms 3440 KB Output is correct
10 Correct 15 ms 3376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 604 KB Output is correct
2 Correct 6 ms 604 KB Output is correct
3 Correct 48 ms 988 KB Output is correct
4 Correct 156 ms 7356 KB Output is correct
5 Correct 92 ms 7500 KB Output is correct
6 Correct 3 ms 1368 KB Output is correct
7 Correct 3 ms 1372 KB Output is correct
8 Correct 2 ms 604 KB Output is correct
9 Correct 3 ms 600 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 27 ms 3500 KB Output is correct
12 Correct 95 ms 3420 KB Output is correct
13 Correct 32 ms 3672 KB Output is correct
14 Correct 20 ms 3676 KB Output is correct
15 Correct 26 ms 6348 KB Output is correct
16 Correct 22 ms 3540 KB Output is correct
17 Correct 24 ms 3540 KB Output is correct
18 Correct 22 ms 3540 KB Output is correct
19 Correct 16 ms 3440 KB Output is correct
20 Correct 15 ms 3376 KB Output is correct
21 Correct 28 ms 3808 KB Output is correct
22 Correct 94 ms 3420 KB Output is correct
23 Correct 715 ms 4072 KB Output is correct
24 Correct 1433 ms 10292 KB Output is correct
25 Correct 263 ms 11468 KB Output is correct
26 Correct 158 ms 14260 KB Output is correct
27 Correct 36 ms 9132 KB Output is correct
28 Correct 41 ms 9180 KB Output is correct
29 Execution timed out 4035 ms 6516 KB Time limit exceeded
30 Halted 0 ms 0 KB -