Tifa's CP Library

:heavy_check_mark: unordered_stl_hacker (src/code/util/unordered_stl_hacker.hpp)

Depends on

Verified with

Code

#ifndef TIFALIBS_UTIL_UNORDERED_STL_HACKER
#define TIFALIBS_UTIL_UNORDERED_STL_HACKER

#include "../rand/gen.hpp"
#include "util.hpp"

namespace tifa_libs::util {

// Just run it on the same compiler and same options as the solution you want to hack.
// Works for integral types, and strn. Slow for strn.
template <class T>
inline vec<T> unordered_stl_hacker(usz n) {
  auto get_bucket_counts = [n]() {
    vec<usz> ans;
    std::unordered_set<int> s;
    rand::Gen<std::uniform_int_distribution<int>> gen;

    while (s.size() < n) {
      if (ans.empty() || ans.back() != s.bucket_count()) ans.push_back(s.bucket_count());
      s.insert(gen());
    }
    return ans;
  };

  vec<usz> bc = get_bucket_counts();

  if constexpr (std::is_same_v<strn, T>) {
    // Edit these if need
    const usz len = 15;
    const strn pref = "";
    const strn alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";

    rand::Gen<std::uniform_int_distribution<usz>> gen(0, alphabet.size() - 1);
    auto gen_str_with_divisible_hash = [&](usz mod) -> strn {
      strn s = pref;
      while (true) {
        s.resize(pref.size());
        while (s.size() < len) s += alphabet[gen()];
        if (!(std::hash<strn>{}(s) % mod)) return s;
      }
      return "";
    };
    vec<T> ans1;
    i64 op1 = 0;
    for (usz i = 0, psb = 0, cnt = 0, lst = bc[0]; i < n; ++i) {
      T nw = gen_str_with_divisible_hash(lst);
      ans1.push_back(nw);
      op1 += cnt;
      if (i >= lst) {
        lst = bc[++psb];
        cnt = 1;
      } else ++cnt;
    }
    vec<T> ans2;
    i64 op2 = 0;
    for (usz i = 0, cnt = 0, pr = bc.size() > 1 ? bc[bc.size() - 2] : 0, lst = bc.back(); i < n; ++i) {
      T nw = gen_str_with_divisible_hash(lst);
      ans2.push_back(nw);
      op2 += i < pr ? 1 : cnt;
      ++cnt;
    }
    return op1 > op2 ? ans1 : ans2;
  } else if constexpr (std::is_integral_v<T>) {
    vec<T> ans1;
    i64 op1 = 0;
    for (usz i = 0, psb = 0, cnt = 0, lst = bc[0]; i < n; ++i) {
      ans1.push_back(T(lst * cnt));
      op1 += cnt;
      if (i >= lst) {
        lst = bc[++psb];
        cnt = 1;
      } else ++cnt;
    }
    vec<T> ans2;
    i64 op2 = 0;
    for (usz i = 0, cnt = 0, pr = bc.size() > 1 ? bc[bc.size() - 2] : 0, lst = bc.back(); i < n; ++i) {
      ans2.push_back(T(lst * cnt));
      op2 += i < pr ? 1 : cnt;
      ++cnt;
    }
    return op1 > op2 ? ans1 : ans2;
  }
}

}  // namespace tifa_libs::util

#endif
#line 1 "src/code/util/unordered_stl_hacker.hpp"



#line 1 "src/code/rand/gen.hpp"



#line 1 "src/code/util/util.hpp"



#include <bits/stdc++.h>

template <class T>
constexpr T abs(T x) { return x < 0 ? -x : x; }

using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using i128 = __int128_t;
using isz = ptrdiff_t;

using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using u128 = __uint128_t;
using usz = size_t;

using f32 = float;
using f64 = double;
using f128 = long double;

template <class T>
using ptt = std::pair<T, T>;
template <class T>
using pt3 = std::tuple<T, T, T>;
template <class T>
using pt4 = std::tuple<T, T, T, T>;

template <class T, usz N>
using arr = std::array<T, N>;
template <class T>
using vec = std::vector<T>;
template <class T>
using vvec = vec<vec<T>>;
template <class T>
using v3ec = vec<vvec<T>>;
template <class U, class T>
using vecp = vec<std::pair<U, T>>;
template <class U, class T>
using vvecp = vvec<std::pair<U, T>>;
template <class T>
using vecpt = vec<ptt<T>>;
template <class T>
using vvecpt = vvec<ptt<T>>;

template <class T, class C = std::less<T>>
using pq = std::priority_queue<T, vec<T>, C>;
template <class T>
using pqg = std::priority_queue<T, vec<T>, std::greater<T>>;

using strn = std::string;
using strnv = std::string_view;

using vecu = vec<u32>;
using vvecu = vvec<u32>;
using v3ecu = v3ec<u32>;
using vecu64 = vec<u64>;
using vecb = vec<bool>;
using vvecb = vvec<bool>;

#ifdef ONLINE_JUDGE
#undef assert
#define assert(x) 42
#endif

using namespace std::literals;

constexpr i8 operator""_i8(unsigned long long x) { return (i8)x; }
constexpr i16 operator""_i16(unsigned long long x) { return (i16)x; }
constexpr i32 operator""_i32(unsigned long long x) { return (i32)x; }
constexpr i64 operator""_i64(unsigned long long x) { return (i64)x; }
constexpr isz operator""_iz(unsigned long long x) { return (isz)x; }

constexpr u8 operator""_u8(unsigned long long x) { return (u8)x; }
constexpr u16 operator""_u16(unsigned long long x) { return (u16)x; }
constexpr u32 operator""_u32(unsigned long long x) { return (u32)x; }
constexpr u64 operator""_u64(unsigned long long x) { return (u64)x; }
constexpr usz operator""_uz(unsigned long long x) { return (usz)x; }

inline const auto fn_0 = [](auto&&...) {};


#line 5 "src/code/rand/gen.hpp"

namespace tifa_libs::rand {

template <class Distri>
class Gen {
  std::conditional_t<sizeof(typename Distri::result_type) <= 4, std::mt19937, std::mt19937_64> re;
  Distri dist;

 public:
  using random_engine = decltype(re);
  using distribution = Distri;
  using result_type = typename Distri::result_type;

  constexpr Gen() : re(std::random_device{}()), dist() {}
  constexpr Gen(result_type a, result_type b) : re(std::random_device{}()), dist(a, b) {}

  constexpr void set_range(result_type a, result_type b) { dist = Distri(a, b); }
  constexpr random_engine& rand_eng() { return re; }
  constexpr Distri& distrib() { return dist; }

  void reset_seed() { re.seed((result_type)std::chrono::duration_cast<std::conditional_t<sizeof(typename Distri::result_type) <= 4, std::chrono::seconds, std::chrono::nanoseconds>>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); }
  constexpr result_type operator()() { return dist(re); }
  result_type next() { return dist(re); }
};

}  // namespace tifa_libs::rand


#line 6 "src/code/util/unordered_stl_hacker.hpp"

namespace tifa_libs::util {

// Just run it on the same compiler and same options as the solution you want to hack.
// Works for integral types, and strn. Slow for strn.
template <class T>
inline vec<T> unordered_stl_hacker(usz n) {
  auto get_bucket_counts = [n]() {
    vec<usz> ans;
    std::unordered_set<int> s;
    rand::Gen<std::uniform_int_distribution<int>> gen;

    while (s.size() < n) {
      if (ans.empty() || ans.back() != s.bucket_count()) ans.push_back(s.bucket_count());
      s.insert(gen());
    }
    return ans;
  };

  vec<usz> bc = get_bucket_counts();

  if constexpr (std::is_same_v<strn, T>) {
    // Edit these if need
    const usz len = 15;
    const strn pref = "";
    const strn alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz";

    rand::Gen<std::uniform_int_distribution<usz>> gen(0, alphabet.size() - 1);
    auto gen_str_with_divisible_hash = [&](usz mod) -> strn {
      strn s = pref;
      while (true) {
        s.resize(pref.size());
        while (s.size() < len) s += alphabet[gen()];
        if (!(std::hash<strn>{}(s) % mod)) return s;
      }
      return "";
    };
    vec<T> ans1;
    i64 op1 = 0;
    for (usz i = 0, psb = 0, cnt = 0, lst = bc[0]; i < n; ++i) {
      T nw = gen_str_with_divisible_hash(lst);
      ans1.push_back(nw);
      op1 += cnt;
      if (i >= lst) {
        lst = bc[++psb];
        cnt = 1;
      } else ++cnt;
    }
    vec<T> ans2;
    i64 op2 = 0;
    for (usz i = 0, cnt = 0, pr = bc.size() > 1 ? bc[bc.size() - 2] : 0, lst = bc.back(); i < n; ++i) {
      T nw = gen_str_with_divisible_hash(lst);
      ans2.push_back(nw);
      op2 += i < pr ? 1 : cnt;
      ++cnt;
    }
    return op1 > op2 ? ans1 : ans2;
  } else if constexpr (std::is_integral_v<T>) {
    vec<T> ans1;
    i64 op1 = 0;
    for (usz i = 0, psb = 0, cnt = 0, lst = bc[0]; i < n; ++i) {
      ans1.push_back(T(lst * cnt));
      op1 += cnt;
      if (i >= lst) {
        lst = bc[++psb];
        cnt = 1;
      } else ++cnt;
    }
    vec<T> ans2;
    i64 op2 = 0;
    for (usz i = 0, cnt = 0, pr = bc.size() > 1 ? bc[bc.size() - 2] : 0, lst = bc.back(); i < n; ++i) {
      ans2.push_back(T(lst * cnt));
      op2 += i < pr ? 1 : cnt;
      ++cnt;
    }
    return op1 > op2 ? ans1 : ans2;
  }
}

}  // namespace tifa_libs::util


Back to top page