답안 #955872

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

ll Bx, By;

map<pair<int, int>, int> ans;
set<pair<int, int>> queries;

struct Point {
  ll x, y;
  int idx;
  int tribe;
  Point operator-(const Point p) const { return {x - p.x, y - p.y, idx, tribe}; }
  Point operator+(const Point p) const { return {x + p.x, y + p.y, idx, tribe}; }
  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, -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, tribe}; }
};

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

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

BIT<int, 30'001> active;

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

  active.clear(points2.size());

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

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

    ans[{p.tribe, attacked_tribe_idx}] += active.sum(from, to - 1);
  }
}

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

  while (cin >> N >> M) {
    ans.clear();
    vector<Point> points;
    unordered_map<int, vector<Point>> tribe_points;
    for (int i = 0; i < N; i++) {
      Point p;
      cin >> p.x >> p.y >> p.tribe;
      tribe_points[p.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;
    }

    vector<Point> all;

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

    sort(all.begin(), all.end());

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

    queries.clear();

    map<int, int> attacked;

    vector<pair<int, int>> queries_raw;

    while (Q--) {
      int i, j;
      cin >> i >> j;
      queries_raw.emplace_back(i, j);
      queries.emplace(i, j);
    }

    for (const auto& [_, j] : queries) attacked[j]++;

    set<int> solved_attacked;

    const int sqrtn = sqrt(N);

    for (int i = 0; i < N; i++) {
      if (attacked[i] > sqrtn) {
        handle_query(all, tribe_points[i], order_by_b[i], i, B);
        solved_attacked.emplace(i);
      }
    }

    for (const auto& [i, j] : queries) {
      if (solved_attacked.count(j)) continue;
      handle_query(tribe_points[i], tribe_points[j], order_by_b[j], j, B);
    }

    for (const auto& [i, j] : queries_raw) {
      cout << ans[{i, j}] << '\n';
    }
  }
}

Compilation message

dragon2.cpp: In function 'int main()':
dragon2.cpp:131:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  131 |     for (auto& [_, points] : tribe_points) {
      |                ^
dragon2.cpp:135:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  135 |     for (auto& [tribe_idx, points] : tribe_points) {
      |                ^
dragon2.cpp:143:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  143 |     for (auto& [_, points] : tribe_points) {
      |                ^
dragon2.cpp:165:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  165 |     for (const auto& [_, j] : queries) attacked[j]++;
      |                      ^
dragon2.cpp:178:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  178 |     for (const auto& [i, j] : queries) {
      |                      ^
dragon2.cpp:183:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  183 |     for (const auto& [i, j] : queries_raw) {
      |                      ^
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 860 KB Output is correct
2 Correct 5 ms 860 KB Output is correct
3 Correct 35 ms 1600 KB Output is correct
4 Correct 1043 ms 64228 KB Output is correct
5 Correct 116 ms 14080 KB Output is correct
6 Correct 4 ms 1496 KB Output is correct
7 Correct 4 ms 1372 KB Output is correct
8 Correct 2 ms 824 KB Output is correct
9 Correct 2 ms 860 KB Output is correct
10 Correct 3 ms 992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 4032 KB Output is correct
2 Correct 57 ms 3912 KB Output is correct
3 Correct 26 ms 5060 KB Output is correct
4 Correct 20 ms 5096 KB Output is correct
5 Correct 25 ms 7360 KB Output is correct
6 Correct 22 ms 4608 KB Output is correct
7 Correct 23 ms 4584 KB Output is correct
8 Correct 21 ms 4564 KB Output is correct
9 Correct 16 ms 4312 KB Output is correct
10 Correct 17 ms 4332 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 860 KB Output is correct
2 Correct 5 ms 860 KB Output is correct
3 Correct 35 ms 1600 KB Output is correct
4 Correct 1043 ms 64228 KB Output is correct
5 Correct 116 ms 14080 KB Output is correct
6 Correct 4 ms 1496 KB Output is correct
7 Correct 4 ms 1372 KB Output is correct
8 Correct 2 ms 824 KB Output is correct
9 Correct 2 ms 860 KB Output is correct
10 Correct 3 ms 992 KB Output is correct
11 Correct 25 ms 4032 KB Output is correct
12 Correct 57 ms 3912 KB Output is correct
13 Correct 26 ms 5060 KB Output is correct
14 Correct 20 ms 5096 KB Output is correct
15 Correct 25 ms 7360 KB Output is correct
16 Correct 22 ms 4608 KB Output is correct
17 Correct 23 ms 4584 KB Output is correct
18 Correct 21 ms 4564 KB Output is correct
19 Correct 16 ms 4312 KB Output is correct
20 Correct 17 ms 4332 KB Output is correct
21 Correct 26 ms 4500 KB Output is correct
22 Correct 59 ms 4788 KB Output is correct
23 Correct 391 ms 5748 KB Output is correct
24 Correct 839 ms 17912 KB Output is correct
25 Correct 182 ms 19184 KB Output is correct
26 Correct 140 ms 21676 KB Output is correct
27 Correct 30 ms 10596 KB Output is correct
28 Correct 29 ms 10600 KB Output is correct
29 Execution timed out 4075 ms 14684 KB Time limit exceeded
30 Halted 0 ms 0 KB -