제출 #619620

#제출 시각아이디문제언어결과실행 시간메모리
619620evenvalue자리 배치 (IOI18_seats)C++17
31 / 100
4033 ms64812 KiB
#include "seats.h"
#include <bits/stdc++.h>
using namespace std;

template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using max_heap = priority_queue<T, vector<T>, less<T>>;

using int64 = long long;
using ld = long double;

constexpr int kInf = 1e9 + 10;
constexpr int64 kInf64 = 1e15 + 10;
constexpr int kMod = 1e9 + 7;

template<class node, class F = std::function<node(const node &, const node &)>>
class SegTree {
  int n = 0;
  std::vector<node> t;
  F unite;

  void build(const int x, const int l, const int r, const std::vector<node> &a) {
    if (l == r) {
      t[x] = a[l];
      return;
    }
    const int mid = (l + r) / 2;
    const int y = 2 * (mid - l + 1) + x;
    build(x + 1, l, mid, a);
    build(y, mid + 1, r, a);
    t[x] = unite(t[x + 1], t[y]);
  }

  void update(const int x, const int l, const int r, const int p, const node &v) {
    if (l == p and p == r) {
      t[x] = v;
      return;
    }
    const int mid = (l + r) / 2;
    const int y = 2 * (mid - l + 1) + x;
    if (p <= mid) {
      update(x + 1, l, mid, p, v);
    } else {
      update(y, mid + 1, r, p, v);
    }
    t[x] = unite(t[x + 1], t[y]);
  }

  node query(const int x, const int l, const int r, const int ql, const int qr) const {
    if (ql <= l and r <= qr) {
      return t[x];
    }
    const int mid = (l + r) / 2;
    const int y = 2 * (mid - l + 1) + x;
    if (qr <= mid) {
      return query(x + 1, l, mid, ql, qr);
    } else if (mid < ql) {
      return query(y, mid + 1, r, ql, qr);
    } else {
      return unite(query(x + 1, l, mid, ql, qr), query(y, mid + 1, r, ql, qr));
    }
  }

public:
  SegTree() = default;
  explicit SegTree(const int n, const node e, F f) : n(n), t(2 * n - 1, e), unite(std::move(f)) {}
  explicit SegTree(const std::vector<node> &a, F f) : n(a.size()), t(2 * (a.size()) - 1), unite(std::move(f)) {
    build(0, 0, n - 1, a);
  }

  void set(const int p, const node &v) {
    assert(0 <= p and p < n);
    update(0, 0, n - 1, p, v);
  }

  node get(const int l, const int r) const {
    assert(0 <= l and l <= r and r < n);
    return query(0, 0, n - 1, l, r);
  }
};

struct node {
  int l = 0, r = 0, t = 0, b = 0;
};

int H, W;
vector<int> R, C;
SegTree<node> st;

void give_initial_chart(int H_, int W_, std::vector<int> R_, std::vector<int> C_) {
  H = H_, W = W_;
  const int mat_size = H * W;
  R = move(R_), C = move(C_);
  st = SegTree<node>(mat_size, node(), [](const node &l, const node &r) {
    node ret{};
    ret.l = min(l.l, r.l);
    ret.r = max(l.r, r.r);
    ret.t = min(l.t, r.t);
    ret.b = max(l.b, r.b);
    return ret;
  });
  for (int i = 0; i < mat_size; i++) {
    st.set(i, node({C[i], C[i], R[i], R[i]}));
  }
}

inline int find_beauty() {
  const int total_seats = H * W;
  int ans = 0;
  for (int cur_seat = 0; cur_seat < total_seats;) {
    const auto [l, r, t, b] = st.get(0, cur_seat);
    const int rect_size = (r - l + 1) * (b - t + 1);
    const bool beautiful = (cur_seat + 1 == rect_size);
    ans += beautiful;
    cur_seat = rect_size - 1 + beautiful;
  }
  return ans;
};

int swap_seats(int x, int y) {
  swap(R[x], R[y]);
  swap(C[x], C[y]);
  st.set(x, {C[x], C[x], R[x], R[x]});
  st.set(y, {C[y], C[y], R[y], R[y]});
  return find_beauty();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...