cpp-library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub shino16/cpp-library

:warning: ds/doubling.hpp

Depends on

Code

#pragma once
#include "bit/ilog2.hpp"
#include "bit/ctz.hpp"

class doubling {
 public:
  template <class Iter>
  doubling(Iter f, Iter l, ll max_power = LONG_LONG_MAX)
      : size(l - f), height(ilog2(max_power) + 1), data(height) {
    data[0].assign(f, l);
    rep(h, height - 1) {
      data[h + 1].resize(size);
      rep(x, size) data[h + 1][x] = data[h][data[h][x]];
    }
  }

  int apply(ll power, int x) const {
    rep(h, height) if (power >> h & 1) x = data[h][x];
    return x;
  }
  int operator()(ll power, int x) const { return apply(power, x); }
  // min i s.t. !p(apply(i, x))
  template <class P>
  ll partition_point(int x, P p) const {
    if (!p(x)) return 0;
    ll i = 0;
    repr(h, height)
      if (p(data[h][x])) i |= 1LL << h, x = data[h][x];
    return i + (i != LLONG_MAX);
  }

 private:
  int size, height;
  vector<vector<int>> data;
};
#line 2 "prelude.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vc = vector<char>;
#define rep2(i, m, n) for (auto i = (m); i < (n); i++)
#define rep(i, n) rep2(i, 0, n)
#define repr2(i, m, n) for (auto i = (n); i-- > (m);)
#define repr(i, n) repr2(i, 0, n)
#define all(x) begin(x), end(x)
auto ndvec(int n, auto e) { return vector(n, e); }
auto ndvec(int n, auto ...e) { return vector(n, ndvec(e...)); }
auto comp_key(auto&& f) { return [&](auto&& a, auto&& b) { return f(a) < f(b); }; }
auto& max(const auto& a, const auto& b) { return a < b ? b : a; }
auto& min(const auto& a, const auto& b) { return b < a ? b : a; }
#if __cpp_lib_ranges
namespace R = std::ranges;
namespace V = std::views;
#endif
#line 3 "bit/clz.hpp"

#pragma GCC target("lzcnt")

template <class T>
int clz(T x) {
    if (!x) return sizeof(T) * 8;
    if constexpr (sizeof(T) <= sizeof(unsigned)) {
        return __builtin_clz((unsigned)x);
    } else if constexpr (sizeof(T) <= sizeof(unsigned long long)) {
        return __builtin_clzll((unsigned long long)x);
    } else if constexpr (sizeof(T) <= sizeof(unsigned long long) * 2) {
        int l = clz((unsigned long long)(x >> sizeof(unsigned long long) * 8));
        return l != sizeof(unsigned long long) * 8 ? l : l + clz((unsigned long long)x);
    }
}
#line 4 "bit/ilog2.hpp"

template <class T>
__attribute__((pure)) int ilog2(T x) { assert(x != 0); return sizeof(T) * 8 - 1 - clz(x); }

template <class T>
__attribute__((pure)) int ilog2_ceil(T x) { return x == 0 || x == 1 ? 0 : ilog2(x - 1) + 1; }

template <class T, enable_if_t<is_signed_v<T>>* = nullptr>
__attribute__((pure)) T bit_floor(T x) { return T(1) << ilog2(x); }

template <class T, enable_if_t<is_signed_v<T>>* = nullptr>
__attribute__((pure)) T bit_ceil(T x) { return T(1) << ilog2_ceil(x); }
#line 3 "bit/ctz.hpp"

#pragma GCC target("bmi")

template <class T>
int ctz(T x) {
  if (!x) return sizeof(T) * 8;
  if constexpr (sizeof(T) <= sizeof(unsigned)) {
    return __builtin_ctz((unsigned)x);
  } else if constexpr (sizeof(T) <= sizeof(unsigned long long)) {
    return __builtin_ctzll((unsigned long long)x);
  } else if constexpr (sizeof(T) <= sizeof(unsigned long long) * 2) {
    unsigned long long y = x;
    return y ? ctz(y)
             : sizeof(y) * 8 + ctz((unsigned long long)(x >> sizeof(y) * 8));
  }
}
#line 4 "ds/doubling.hpp"

class doubling {
 public:
  template <class Iter>
  doubling(Iter f, Iter l, ll max_power = LONG_LONG_MAX)
      : size(l - f), height(ilog2(max_power) + 1), data(height) {
    data[0].assign(f, l);
    rep(h, height - 1) {
      data[h + 1].resize(size);
      rep(x, size) data[h + 1][x] = data[h][data[h][x]];
    }
  }

  int apply(ll power, int x) const {
    rep(h, height) if (power >> h & 1) x = data[h][x];
    return x;
  }
  int operator()(ll power, int x) const { return apply(power, x); }
  // min i s.t. !p(apply(i, x))
  template <class P>
  ll partition_point(int x, P p) const {
    if (!p(x)) return 0;
    ll i = 0;
    repr(h, height)
      if (p(data[h][x])) i |= 1LL << h, x = data[h][x];
    return i + (i != LLONG_MAX);
  }

 private:
  int size, height;
  vector<vector<int>> data;
};
Back to top page