답안 #257424

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
257424 2020-08-04T09:04:00 Z KoD 무지개나라 (APIO17_rainbow) C++17
100 / 100
816 ms 41320 KB
#line 1 "main.cpp"

/**
 * @title Template
 */

#include "rainbow.h"
#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <cassert>
#include <set>
#include <map>

#line 2 "/Users/kodamankod/Desktop/Programming/Library/other/chmin_chmax.cpp"

template <class T, class U>
constexpr bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) { 
    lhs = rhs; 
    return true; 
  }
  return false;
}

template <class T, class U>
constexpr bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) { 
    lhs = rhs; 
    return true; 
  }
  return false;
}

/**
 * @title Chmin/Chmax
 */
#line 2 "/Users/kodamankod/Desktop/Programming/Library/other/range.cpp"

#line 4 "/Users/kodamankod/Desktop/Programming/Library/other/range.cpp"

class range {
public:
  class iterator {
  private:
    int64_t M_position;

  public:
    constexpr iterator(int64_t position) noexcept: M_position(position) { }
    constexpr void operator ++ () noexcept { ++M_position; }
    constexpr bool operator != (iterator other) const noexcept { return M_position != other.M_position; }
    constexpr int64_t operator * () const noexcept { return M_position; }

  };

  class reverse_iterator {
  private:
    int64_t M_position;
  
  public:
    constexpr reverse_iterator(int64_t position) noexcept: M_position(position) { }
    constexpr void operator ++ () noexcept { --M_position; }
    constexpr bool operator != (reverse_iterator other) const noexcept { return M_position != other.M_position; }
    constexpr int64_t operator * () const noexcept { return M_position; }

  };
  
private:
  const iterator M_first, M_last;

public:
  constexpr range(int64_t first, int64_t last) noexcept: M_first(first), M_last(std::max(first, last)) { }
  constexpr iterator begin() const noexcept { return M_first; }
  constexpr iterator end() const noexcept { return M_last; }
  constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(*M_last - 1); } 
  constexpr reverse_iterator rend() const noexcept { return reverse_iterator(*M_first - 1); } 

};

/**
 * @title Range
 */
#line 2 "/Users/kodamankod/Desktop/Programming/Library/other/rev.cpp"

#include <type_traits>
#include <iterator>
#line 6 "/Users/kodamankod/Desktop/Programming/Library/other/rev.cpp"

template <class T>
class rev_impl {
public:
  using iterator = typename std::decay<T>::type::reverse_iterator;

private:
  const iterator M_begin;
  const iterator M_end;

public:
  constexpr rev_impl(T &&cont) noexcept: M_begin(std::rbegin(cont)), M_end(std::rend(cont)) { }
  constexpr iterator begin() const noexcept { return M_begin; }
  constexpr iterator end() const noexcept { return M_end; }

};

template <class T>
constexpr decltype(auto) rev(T &&cont) {
  return rev_impl<T>(std::forward<T>(cont));
}

/**
 * @title Reverser
 */
#line 2 "/Users/kodamankod/Desktop/Programming/Library/other/fix_point.cpp"

#line 4 "/Users/kodamankod/Desktop/Programming/Library/other/fix_point.cpp"

template <class Func>
struct fix_point_impl: private Func {
  explicit constexpr fix_point_impl(Func &&func): Func(std::forward<Func>(func)) { }
  template <class... Args>
  constexpr decltype(auto) operator () (Args &&... args) const {
    return Func::operator()(*this, std::forward<Args>(args)...);
  }
};

template <class Func>
constexpr decltype(auto) fix_point(Func &&func) {
  return fix_point_impl<Func>(std::forward<Func>(func));
}

/**
 * @title Lambda Recursion
 */
#line 2 "/Users/kodamankod/Desktop/Programming/Library/container/wavelet_matrix.cpp"

#line 2 "/Users/kodamankod/Desktop/Programming/Library/container/bit_vector.cpp"

#include <cstddef>
#include <cstdint>
#line 7 "/Users/kodamankod/Desktop/Programming/Library/container/bit_vector.cpp"

class bit_vector {
public:
  using size_type = size_t;
  using bit_type = uint64_t;
  using count_type = uint32_t;

private:
  static constexpr size_type S_block_size = 64;

  size_type M_size;
  std::vector<bit_type> M_block;
  std::vector<count_type> M_accum;

public:
  bit_vector() = default;
  template <class InputIterator>
  explicit bit_vector(InputIterator first, InputIterator last) { construct(first, last); }

  template <class InputIterator>
  void construct(InputIterator first, InputIterator last) { 
    M_size = std::distance(first, last);
    size_type fixed_size = M_size / S_block_size + 1;
    M_block.assign(fixed_size, 0);
    M_accum.assign(fixed_size, 0);
    for (size_type i = 0; i < M_size; ++i) {
      M_block[i / S_block_size] |= (bit_type(*first) & 1) << (i & (S_block_size - 1));
      ++first;
    }
    for (size_type i = 1; i < fixed_size; ++i) {
      M_accum[i] = M_accum[i - 1] + __builtin_popcountll(M_block[i - 1]);
    }
  }

  bool access(size_type idx) const {
    return M_block[idx / S_block_size] >> (idx & (S_block_size - 1)) & 1;
  }
  size_type rank(bool value, size_type idx) const {
    bit_type mask = (bit_type(1) << (idx & (S_block_size - 1))) - 1;
    size_type res = M_accum[idx / S_block_size] + __builtin_popcountll(M_block[idx / S_block_size] & mask);
    return value ? res : idx - res;
  }
  size_type select(bool value, size_type idx) const {
    if (idx >= rank(value, M_size)) {
      return M_size;
    }
    size_type ok = 0, ng = M_size;
    while (ng - ok > 1) {
      size_type md = (ok + ng) >> 1;
      (rank(value, md) <= idx ? ok : ng) = md;
    }
    return ok;
  }
  size_type select(bool value, size_type idx, size_type l) const {
    return select(value, idx + rank(value, l));
  }

};

/**
 * @title Succinct Bit Vector
 */
#line 6 "/Users/kodamankod/Desktop/Programming/Library/container/wavelet_matrix.cpp"

template <class T, size_t W>
class wavelet_matrix {
public:
  using value_type = T;
  using size_type = size_t;

  static constexpr size_type word_size = W;

private:
  size_type M_size;
  std::array<bit_vector, word_size> M_fid;
  std::array<size_type, word_size> M_zero;

public:
  wavelet_matrix() = default;
  template <class InputIterator>
  explicit wavelet_matrix(InputIterator first, InputIterator last) { construct(first, last); }

  template <class InputIterator>
  void construct(InputIterator first, InputIterator last) { 
    M_size = std::distance(first, last);
    std::vector<bool> bit(M_size);
    std::vector<value_type> current(first, last);
    std::vector<value_type> next(M_size);
    for (size_type k = word_size; k--;) {
      auto l = next.begin(), r = next.rbegin();
      for (size_type i = 0; i < M_size; ++i) {
        bit[i] = current[i] >> k & 1;
        (bit[i] ? *(r++) : *(l++)) = current[i];
      }
      M_fid[k].construct(bit.begin(), bit.end());
      M_zero[k] = l - next.begin();
      std::reverse(next.rbegin(), r);
      current.swap(next);
    }
  }

  size_type rank(value_type value, size_type l, size_type r) const {
    for (size_type k = word_size; k--;) {
      bool p = value >> k & 1;
      l = M_fid[k].rank(p, l) + p * M_zero[k];
      r = M_fid[k].rank(p, r) + p * M_zero[k];
    }
    return r - l;
  }

  size_type select(value_type value, size_type index) const {
    std::array<size_type, word_size + 1> l, r;
    l[word_size] = 0;
    r[word_size] = M_size;
    for (size_type k = word_size; k--;) {
      bool p = value >> k & 1;
      l[k] = M_fid[k].rank(p, l[k + 1]) + p * M_zero[k];
      r[k] = M_fid[k].rank(p, r[k + 1]) + p * M_zero[k];
    }
    if (r[0] - l[0] <= index) {
      return M_size;
    }
    for (size_type k = 0; k < word_size; ++k) {
      bool p = value >> k & 1;
      index = M_fid[k].select(p, index, l[k + 1]) - l[k + 1];
    }
    return index;
  }

  value_type access(size_type index) const {
    value_type res = 0;
    for (size_type k = word_size; k--;) {
      bool p = M_fid[k].access(index);
      res |= value_type(p) << k;
      index = M_fid[k].rank(p, index) + p * M_zero[k];
    }
    return res;
  }

  value_type quantile(size_type index, size_type l, size_type r) const {
    value_type res = 0;
    for (size_type k = word_size; k--;) {
      size_type lc = M_fid[k].rank(1, l);
      size_type rc = M_fid[k].rank(1, r);
      size_type zc = (r - l) - (rc - lc);
      bool p = (index >= zc);
      res |= value_type(p) << k;
      if (p) {
        l = lc + M_zero[k];
        r = rc + M_zero[k];
        index -= zc;
      }
      else {
        l -= lc;
        r -= rc;
      }
    }
    return res;
  }

  size_type count(size_type l, size_type r, value_type value) const {
    size_type res = 0;
    for (size_type k = word_size; k--;) {
      size_type lc = M_fid[k].rank(1, l);
      size_type rc = M_fid[k].rank(1, r);
      if (value >> (k) & 1) {
        l = lc + M_zero[k];
        r = rc + M_zero[k];
      }
      else {
        l -= lc;
        r -= rc;
        res += (rc - lc);
      }
    }
    return res + (r - l);
  }
  size_type count(size_type l, size_type r, value_type lb, value_type ub) const {
    return count(l, r, lb) - count(l, r, ub);
  }

};

/**
 * @title Wavelet Matrix
 */
#line 22 "main.cpp"

using i32 = int32_t;
using i64 = int64_t;
using u32 = uint32_t;
using u64 = uint64_t;

struct count_points {
  wavelet_matrix<i32, 20> matrix;
  std::set<std::pair<i32, i32>> points;
  std::vector<i32> X;
  void add_point(i32 x, i32 y) {
    points.emplace(x, y);
  }
  void build() {
    std::vector<i32> Y;
    for (auto [x, y]: points) {
      X.push_back(x);
      Y.push_back(y);
    }
    matrix.construct(Y.begin(), Y.end());
  }
  i32 count(i32 l, i32 r, i32 lb, i32 ub) {
    if (l >= r || lb >= ub) return 0;
    i32 il = std::lower_bound(X.begin(), X.end(), l) - X.begin();
    i32 ir = std::lower_bound(X.begin(), X.end(), r) - X.begin();
    return matrix.count(il, ir, lb, ub);
  }
};

i32 H, W;
count_points nov, noe_h, noe_w, noc;
std::set<std::pair<i32, i32>> river;

void disable(i32 x, i32 y) {
  river.emplace(x, y);
  nov.add_point(x, y);
  noe_h.add_point(x, y);
  noe_h.add_point(x - 1, y);
  noe_w.add_point(x, y);
  noe_w.add_point(x, y - 1);
  noc.add_point(x, y);
  noc.add_point(x - 1, y);
  noc.add_point(x, y - 1);
  noc.add_point(x - 1, y - 1);
}

void init(i32 R, i32 C, i32 sr, i32 sc, i32 M, char * S) {
  H = R, W = C;
  i32 x = sr - 1, y = sc - 1;
  disable(x, y);
  for (auto i: range(0, M)) {
    char c = S[i];
    if (c == 'N') --x;
    if (c == 'E') ++y;
    if (c == 'W') --y;
    if (c == 'S') ++x;
    disable(x, y);
  }
  nov.build();
  noe_h.build();
  noe_w.build();
  noc.build();
}
 
i32 colour(i32 ar, i32 ac, i32 br, i32 bc) {
  --ar; --ac; --br; --bc;
  i64 V = (i64) (br - ar + 1) * (bc - ac + 1);
  i64 E = (i64) (br - ar) * (bc - ac + 1) + (i64) (bc - ac) * (br - ar + 1);
  i64 C = (i64) (br - ar) * (bc - ac);
  i32 tmp = nov.count(ar, br + 1, ac, bc + 1);
  if (tmp == 0) return 1;
  i32 tmp2 = nov.count(ar + 1, br, ac + 1, bc);
  if (tmp2 == tmp) ++C;
  V -= nov.count(ar, br + 1, ac, bc + 1);
  E -= noe_h.count(ar, br, ac, bc + 1);
  E -= noe_w.count(ar, br + 1, ac, bc);
  C -= noc.count(ar, br, ac, bc);
  return C - E + V;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 7 ms 640 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 8 ms 768 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Correct 6 ms 512 KB Output is correct
13 Correct 7 ms 904 KB Output is correct
14 Correct 10 ms 1056 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 451 ms 24416 KB Output is correct
4 Correct 686 ms 40780 KB Output is correct
5 Correct 667 ms 41192 KB Output is correct
6 Correct 587 ms 31600 KB Output is correct
7 Correct 673 ms 30552 KB Output is correct
8 Correct 217 ms 1144 KB Output is correct
9 Correct 723 ms 40792 KB Output is correct
10 Correct 741 ms 41192 KB Output is correct
11 Correct 618 ms 31684 KB Output is correct
12 Correct 504 ms 37916 KB Output is correct
13 Correct 512 ms 40776 KB Output is correct
14 Correct 583 ms 41184 KB Output is correct
15 Correct 535 ms 31476 KB Output is correct
16 Correct 496 ms 29792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 365 ms 40512 KB Output is correct
3 Correct 384 ms 41260 KB Output is correct
4 Correct 350 ms 40444 KB Output is correct
5 Correct 296 ms 30708 KB Output is correct
6 Correct 137 ms 8200 KB Output is correct
7 Correct 253 ms 15892 KB Output is correct
8 Correct 350 ms 36664 KB Output is correct
9 Correct 367 ms 32840 KB Output is correct
10 Correct 109 ms 8688 KB Output is correct
11 Correct 243 ms 20380 KB Output is correct
12 Correct 416 ms 40660 KB Output is correct
13 Correct 349 ms 41136 KB Output is correct
14 Correct 347 ms 40188 KB Output is correct
15 Correct 317 ms 30796 KB Output is correct
16 Correct 132 ms 6684 KB Output is correct
17 Correct 222 ms 15740 KB Output is correct
18 Correct 337 ms 40344 KB Output is correct
19 Correct 361 ms 40436 KB Output is correct
20 Correct 366 ms 40272 KB Output is correct
21 Correct 360 ms 36756 KB Output is correct
22 Correct 335 ms 32568 KB Output is correct
23 Correct 109 ms 8560 KB Output is correct
24 Correct 256 ms 20380 KB Output is correct
25 Correct 409 ms 40496 KB Output is correct
26 Correct 353 ms 41320 KB Output is correct
27 Correct 337 ms 40200 KB Output is correct
28 Correct 281 ms 30836 KB Output is correct
29 Correct 127 ms 6684 KB Output is correct
30 Correct 210 ms 15740 KB Output is correct
31 Correct 354 ms 40536 KB Output is correct
32 Correct 355 ms 40232 KB Output is correct
33 Correct 357 ms 40188 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 7 ms 640 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 8 ms 768 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Correct 6 ms 512 KB Output is correct
13 Correct 7 ms 904 KB Output is correct
14 Correct 10 ms 1056 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 791 ms 21448 KB Output is correct
19 Correct 188 ms 2040 KB Output is correct
20 Correct 186 ms 1276 KB Output is correct
21 Correct 163 ms 1400 KB Output is correct
22 Correct 158 ms 1528 KB Output is correct
23 Correct 177 ms 1912 KB Output is correct
24 Correct 250 ms 1272 KB Output is correct
25 Correct 202 ms 1528 KB Output is correct
26 Correct 194 ms 1656 KB Output is correct
27 Correct 488 ms 17840 KB Output is correct
28 Correct 507 ms 9124 KB Output is correct
29 Correct 535 ms 16360 KB Output is correct
30 Correct 816 ms 40384 KB Output is correct
31 Correct 8 ms 384 KB Output is correct
32 Correct 721 ms 18212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 7 ms 640 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 8 ms 768 KB Output is correct
6 Correct 0 ms 384 KB Output is correct
7 Correct 1 ms 256 KB Output is correct
8 Correct 0 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Correct 6 ms 512 KB Output is correct
13 Correct 7 ms 904 KB Output is correct
14 Correct 10 ms 1056 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 384 KB Output is correct
18 Correct 791 ms 21448 KB Output is correct
19 Correct 188 ms 2040 KB Output is correct
20 Correct 186 ms 1276 KB Output is correct
21 Correct 163 ms 1400 KB Output is correct
22 Correct 158 ms 1528 KB Output is correct
23 Correct 177 ms 1912 KB Output is correct
24 Correct 250 ms 1272 KB Output is correct
25 Correct 202 ms 1528 KB Output is correct
26 Correct 194 ms 1656 KB Output is correct
27 Correct 488 ms 17840 KB Output is correct
28 Correct 507 ms 9124 KB Output is correct
29 Correct 535 ms 16360 KB Output is correct
30 Correct 816 ms 40384 KB Output is correct
31 Correct 8 ms 384 KB Output is correct
32 Correct 721 ms 18212 KB Output is correct
33 Correct 365 ms 40512 KB Output is correct
34 Correct 384 ms 41260 KB Output is correct
35 Correct 350 ms 40444 KB Output is correct
36 Correct 296 ms 30708 KB Output is correct
37 Correct 137 ms 8200 KB Output is correct
38 Correct 253 ms 15892 KB Output is correct
39 Correct 350 ms 36664 KB Output is correct
40 Correct 367 ms 32840 KB Output is correct
41 Correct 109 ms 8688 KB Output is correct
42 Correct 243 ms 20380 KB Output is correct
43 Correct 416 ms 40660 KB Output is correct
44 Correct 349 ms 41136 KB Output is correct
45 Correct 347 ms 40188 KB Output is correct
46 Correct 317 ms 30796 KB Output is correct
47 Correct 132 ms 6684 KB Output is correct
48 Correct 222 ms 15740 KB Output is correct
49 Correct 337 ms 40344 KB Output is correct
50 Correct 361 ms 40436 KB Output is correct
51 Correct 366 ms 40272 KB Output is correct
52 Correct 360 ms 36756 KB Output is correct
53 Correct 335 ms 32568 KB Output is correct
54 Correct 109 ms 8560 KB Output is correct
55 Correct 256 ms 20380 KB Output is correct
56 Correct 409 ms 40496 KB Output is correct
57 Correct 353 ms 41320 KB Output is correct
58 Correct 337 ms 40200 KB Output is correct
59 Correct 281 ms 30836 KB Output is correct
60 Correct 127 ms 6684 KB Output is correct
61 Correct 210 ms 15740 KB Output is correct
62 Correct 354 ms 40536 KB Output is correct
63 Correct 355 ms 40232 KB Output is correct
64 Correct 357 ms 40188 KB Output is correct
65 Correct 761 ms 36660 KB Output is correct
66 Correct 771 ms 32824 KB Output is correct
67 Correct 423 ms 9300 KB Output is correct
68 Correct 595 ms 21144 KB Output is correct
69 Correct 607 ms 40516 KB Output is correct
70 Correct 634 ms 41132 KB Output is correct
71 Correct 556 ms 40204 KB Output is correct
72 Correct 473 ms 30908 KB Output is correct
73 Correct 323 ms 7324 KB Output is correct
74 Correct 364 ms 16152 KB Output is correct
75 Correct 469 ms 40308 KB Output is correct
76 Correct 667 ms 40420 KB Output is correct
77 Correct 805 ms 40204 KB Output is correct
78 Correct 451 ms 24416 KB Output is correct
79 Correct 686 ms 40780 KB Output is correct
80 Correct 667 ms 41192 KB Output is correct
81 Correct 587 ms 31600 KB Output is correct
82 Correct 673 ms 30552 KB Output is correct
83 Correct 217 ms 1144 KB Output is correct
84 Correct 723 ms 40792 KB Output is correct
85 Correct 741 ms 41192 KB Output is correct
86 Correct 618 ms 31684 KB Output is correct
87 Correct 504 ms 37916 KB Output is correct
88 Correct 512 ms 40776 KB Output is correct
89 Correct 583 ms 41184 KB Output is correct
90 Correct 535 ms 31476 KB Output is correct
91 Correct 496 ms 29792 KB Output is correct