library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub maspypy/library

:warning: ds/counter.hpp

Depends on

Code

#include "ds/my_bitset.hpp"

// mo + 最頻値クエリで使える
// most_freq_key:O(sqrt(N)+KEY_MAX/w)
// それ以外は O(1)
template <int USE_MAX_FREQ_KEY>
struct Counter {
  int N;
  int thresh;
  int ma;
  vc<int> freq;
  vc<int> freq_cnt;
  vc<My_Bitset> freq_to_key;

  // key が [0, N), 要素数が常に [0, N]
  Counter(int N) : N(N) {
    thresh = sqrtl(N);
    ma = 0;
    freq.assign(N, 0);
    freq_cnt.assign(N + 1, 0);
    freq_cnt[0] = N;
    if constexpr (USE_MAX_FREQ_KEY) {
      freq_to_key.assign(thresh + 1, My_Bitset(N));
    }
  }

  void insert(int k) {
    assert(0 <= k && k < N);
    if (ma == freq[k]) ++ma;
    if constexpr (USE_MAX_FREQ_KEY) {
      freq_to_key[min(thresh, freq[k])][k] = 0;
    }
    freq_cnt[freq[k]]--, freq[k]++, freq_cnt[freq[k]]++;
    if constexpr (USE_MAX_FREQ_KEY) {
      freq_to_key[min(thresh, freq[k])][k] = 1;
    }
  }
  void add(int k) { insert(k); }

  void erase(int k) {
    assert(0 <= k && k < N);
    if (ma == freq[k] && freq_cnt[freq[k]] == 1) --ma;
    if constexpr (USE_MAX_FREQ_KEY) {
      freq_to_key[min(thresh, freq[k])][k] = 0;
    }
    freq_cnt[freq[k]]--, freq[k]--, freq_cnt[freq[k]]++;
    if constexpr (USE_MAX_FREQ_KEY) {
      freq_to_key[min(thresh, freq[k])][k] = 1;
    }
  }
  void remove(int k) { erase(k); }

  int operator[](int x) { return freq[x]; }

  int max_freq() { return ma; }
  int max_freq_key() {
    static_assert(USE_MAX_FREQ_KEY);
    if (ma < thresh) return freq_to_key[mx]._Find_first();
    My_Bitset& b = freq_to_key[thresh];
    int p = b._Find_first();
    while (1) {
      if (freq[p] == mx) return p;
      p = b._Find_next(p);
    }
    assert(0);
    return -1;
  }
};
#line 2 "ds/my_bitset.hpp"

// https://codeforces.com/contest/914/problem/F
// https://yukicoder.me/problems/no/142
// わずかに普通の bitset より遅いときもあるようだが,
// 固定長にしたくないときや slice 操作が必要なときに使う
struct My_Bitset {
  using T = My_Bitset;
  int N;
  vc<u64> dat;

  // x で埋める
  My_Bitset(int N = 0, int x = 0) : N(N) {
    assert(x == 0 || x == 1);
    u64 v = (x == 0 ? 0 : -1);
    dat.assign((N + 63) >> 6, v);
    if (N) dat.back() >>= (64 * len(dat) - N);
  }

  int size() { return N; }

  void resize(int size) {
    dat.resize((size + 63) >> 6);
    int remainingBits = size & 63;
    if (remainingBits != 0) {
      u64 mask = (u64(1) << remainingBits) - 1;
      dat.back() &= mask;
    }
    N = size;
  }

  // thanks to chatgpt!
  class Proxy {
  public:
    Proxy(vc<u64> &d, int i) : dat(d), index(i) {}
    operator bool() const { return (dat[index >> 6] >> (index & 63)) & 1; }

    Proxy &operator=(u64 value) {
      dat[index >> 6] &= ~(u64(1) << (index & 63));
      dat[index >> 6] |= (value & 1) << (index & 63);
      return *this;
    }
    void flip() {
      dat[index >> 6] ^= (u64(1) << (index & 63)); // XOR to flip the bit
    }

  private:
    vc<u64> &dat;
    int index;
  };

  Proxy operator[](int i) { return Proxy(dat, i); }

  T &operator&=(const T &p) {
    assert(N == p.N);
    FOR(i, len(dat)) dat[i] &= p.dat[i];
    return *this;
  }
  T &operator|=(const T &p) {
    assert(N == p.N);
    FOR(i, len(dat)) dat[i] |= p.dat[i];
    return *this;
  }
  T &operator^=(const T &p) {
    assert(N == p.N);
    FOR(i, len(dat)) dat[i] ^= p.dat[i];
    return *this;
  }
  T operator&(const T &p) const { return T(*this) &= p; }
  T operator|(const T &p) const { return T(*this) |= p; }
  T operator^(const T &p) const { return T(*this) ^= p; }

  int count() {
    int ans = 0;
    for (u64 val: dat) ans += popcnt(val);
    return ans;
  }

  int next(int i) {
    chmax(i, 0);
    if (i >= N) return N;
    int k = i >> 6;
    {
      u64 x = dat[k];
      int s = i & 63;
      x = (x >> s) << s;
      if (x) return (k << 6) | lowbit(x);
    }
    FOR(idx, k + 1, len(dat)) {
      if (dat[idx] == 0) continue;
      return (idx << 6) | lowbit(dat[idx]);
    }
    return N;
  }

  int prev(int i) {
    chmin(i, N - 1);
    if (i <= -1) return -1;
    int k = i >> 6;
    if ((i & 63) < 63) {
      u64 x = dat[k];
      x &= (u64(1) << ((i & 63) + 1)) - 1;
      if (x) return (k << 6) | topbit(x);
      --k;
    }
    FOR_R(idx, k + 1) {
      if (dat[idx] == 0) continue;
      return (idx << 6) | topbit(dat[idx]);
    }
    return -1;
  }

  My_Bitset range(int L, int R) {
    assert(L <= R);
    My_Bitset p(R - L);
    int rm = (R - L) & 63;
    FOR(rm) {
      p[R - L - 1] = bool((*this)[R - 1]);
      --R;
    }
    int n = (R - L) >> 6;
    int hi = L & 63;
    int lo = 64 - hi;
    int s = L >> 6;
    if (hi == 0) {
      FOR(i, n) { p.dat[i] ^= dat[s + i]; }
    } else {
      FOR(i, n) { p.dat[i] ^= (dat[s + i] >> hi) ^ (dat[s + i + 1] << lo); }
    }
    return p;
  }

  int count_range(int L, int R) {
    assert(L <= R);
    int cnt = 0;
    while ((L < R) && (L & 63)) cnt += (*this)[L++];
    while ((L < R) && (R & 63)) cnt += (*this)[--R];
    int l = L >> 6, r = R >> 6;
    FOR(i, l, r) cnt += popcnt(dat[i]);
    return cnt;
  }

  // [L,R) に p を代入
  void assign_to_range(int L, int R, My_Bitset &p) {
    assert(p.N == R - L);
    int a = 0, b = p.N;
    while (L < R && (L & 63)) { (*this)[L++] = bool(p[a++]); }
    while (L < R && (R & 63)) { (*this)[--R] = bool(p[--b]); }
    // p[a:b] を [L:R] に
    int l = L >> 6, r = R >> 6;
    int s = a >> 6, t = b >> t;
    int n = r - l;
    if (!(a & 63)) {
      FOR(i, n) dat[l + i] = p.dat[s + i];
    } else {
      int hi = a & 63;
      int lo = 64 - hi;
      FOR(i, n) dat[l + i] = (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo);
    }
  }

  // [L,R) に p を xor
  void xor_to_range(int L, int R, My_Bitset &p) {
    assert(p.N == R - L);
    int a = 0, b = p.N;
    while (L < R && (L & 63)) {
      dat[L >> 6] ^= u64(p[a]) << (L & 63);
      ++a, ++L;
    }
    while (L < R && (R & 63)) {
      --b, --R;
      dat[R >> 6] ^= u64(p[b]) << (R & 63);
    }
    // p[a:b] を [L:R] に
    int l = L >> 6, r = R >> 6;
    int s = a >> 6, t = b >> t;
    int n = r - l;
    if (!(a & 63)) {
      FOR(i, n) dat[l + i] ^= p.dat[s + i];
    } else {
      int hi = a & 63;
      int lo = 64 - hi;
      FOR(i, n) dat[l + i] ^= (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo);
    }
  }

  // [L,R) に p を and
  void and_to_range(int L, int R, My_Bitset &p) {
    assert(p.N == R - L);
    int a = 0, b = p.N;
    while (L < R && (L & 63)) {
      if (!p[a++]) (*this)[L++] = 0;
    }
    while (L < R && (R & 63)) {
      if (!p[--b]) (*this)[--R] = 0;
    }
    // p[a:b] を [L:R] に
    int l = L >> 6, r = R >> 6;
    int s = a >> 6, t = b >> t;
    int n = r - l;
    if (!(a & 63)) {
      FOR(i, n) dat[l + i] &= p.dat[s + i];
    } else {
      int hi = a & 63;
      int lo = 64 - hi;
      FOR(i, n) dat[l + i] &= (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo);
    }
  }

  // [L,R) に p を or
  void or_to_range(int L, int R, My_Bitset &p) {
    assert(p.N == R - L);
    int a = 0, b = p.N;
    while (L < R && (L & 63)) {
      dat[L >> 6] |= u64(p[a]) << (L & 63);
      ++a, ++L;
    }
    while (L < R && (R & 63)) {
      --b, --R;
      dat[R >> 6] |= u64(p[b]) << (R & 63);
    }
    // p[a:b] を [L:R] に
    int l = L >> 6, r = R >> 6;
    int s = a >> 6, t = b >> t;
    int n = r - l;
    if (!(a & 63)) {
      FOR(i, n) dat[l + i] |= p.dat[s + i];
    } else {
      int hi = a & 63;
      int lo = 64 - hi;
      FOR(i, n) dat[l + i] |= (p.dat[s + i] >> hi) | (p.dat[1 + s + i] << lo);
    }
  }

  string to_string() const {
    string S;
    FOR(i, N) S += '0' + (dat[i >> 6] >> (i & 63) & 1);
    return S;
  }

  // bitset に仕様を合わせる
  void set(int i) { (*this)[i] = 1; }
  void reset(int i) { (*this)[i] = 0; }
  void flip(int i) { (*this)[i].flip(); }
  void set() {
    fill(all(dat), u64(-1));
    resize(N);
  }
  void reset() { fill(all(dat), 0); }

  int _Find_first() { return next(0); }
  int _Find_next(int p) { return next(p + 1); }
};
#line 2 "ds/counter.hpp"

// mo + 最頻値クエリで使える
// most_freq_key:O(sqrt(N)+KEY_MAX/w)
// それ以外は O(1)
template <int USE_MAX_FREQ_KEY>
struct Counter {
  int N;
  int thresh;
  int ma;
  vc<int> freq;
  vc<int> freq_cnt;
  vc<My_Bitset> freq_to_key;

  // key が [0, N), 要素数が常に [0, N]
  Counter(int N) : N(N) {
    thresh = sqrtl(N);
    ma = 0;
    freq.assign(N, 0);
    freq_cnt.assign(N + 1, 0);
    freq_cnt[0] = N;
    if constexpr (USE_MAX_FREQ_KEY) {
      freq_to_key.assign(thresh + 1, My_Bitset(N));
    }
  }

  void insert(int k) {
    assert(0 <= k && k < N);
    if (ma == freq[k]) ++ma;
    if constexpr (USE_MAX_FREQ_KEY) {
      freq_to_key[min(thresh, freq[k])][k] = 0;
    }
    freq_cnt[freq[k]]--, freq[k]++, freq_cnt[freq[k]]++;
    if constexpr (USE_MAX_FREQ_KEY) {
      freq_to_key[min(thresh, freq[k])][k] = 1;
    }
  }
  void add(int k) { insert(k); }

  void erase(int k) {
    assert(0 <= k && k < N);
    if (ma == freq[k] && freq_cnt[freq[k]] == 1) --ma;
    if constexpr (USE_MAX_FREQ_KEY) {
      freq_to_key[min(thresh, freq[k])][k] = 0;
    }
    freq_cnt[freq[k]]--, freq[k]--, freq_cnt[freq[k]]++;
    if constexpr (USE_MAX_FREQ_KEY) {
      freq_to_key[min(thresh, freq[k])][k] = 1;
    }
  }
  void remove(int k) { erase(k); }

  int operator[](int x) { return freq[x]; }

  int max_freq() { return ma; }
  int max_freq_key() {
    static_assert(USE_MAX_FREQ_KEY);
    if (ma < thresh) return freq_to_key[mx]._Find_first();
    My_Bitset& b = freq_to_key[thresh];
    int p = b._Find_first();
    while (1) {
      if (freq[p] == mx) return p;
      p = b._Find_next(p);
    }
    assert(0);
    return -1;
  }
};
Back to top page