Asa's CP Library

a01sa01to's competitive programming library. Requires C++20 or higher with GCC. This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub a01sa01to/cp-library

:heavy_check_mark: library/_internal/math/inversion.hpp

Depends on

Required by

Verified with

Code

#pragma once

#include <algorithm>
#include <cstddef>
#include <iterator>
#include <vector>
using namespace std;

#include "../types.hpp"

namespace asalib::_internal::math {
  template<integral_like T, bool eq, typename U, random_access_iterator iter>
  constexpr T inversion(iter first, iter last) {
    const size_t n = distance(first, last);
    if (n <= 1) return 0;
    const size_t mid = n >> 1;
    auto left = inversion<T, eq, U>(first, first + mid);
    auto right = inversion<T, eq, U>(first + mid, last);
    T inv = left + right;
    iter l = first, r = first + mid, l_end = first + mid, r_end = last;
    vector<U> tmp;
    tmp.reserve(n);
    while (l < l_end && r < r_end) {
      if constexpr (eq) {
        if (*l < *r) tmp.emplace_back(*l++);
        else {
          tmp.emplace_back(*r++);
          inv += mid - (l - first);
        }
      }
      else {
        if (*l <= *r) tmp.emplace_back(*l++);
        else {
          tmp.emplace_back(*r++);
          inv += mid - (l - first);
        }
      }
    }
    while (l < l_end) tmp.emplace_back(*l++);
    while (r < r_end) tmp.emplace_back(*r++);
    copy(tmp.begin(), tmp.end(), first);
    return inv;
  }
}  // namespace asalib::_internal::math
#line 2 "library/_internal/math/inversion.hpp"

#include <algorithm>
#include <cstddef>
#include <iterator>
#include <vector>
using namespace std;

#line 2 "library/_internal/types.hpp"

#include <concepts>
using namespace std;

#line 2 "library/_internal/modint-base.hpp"

#include <type_traits>
using namespace std;

namespace asalib::_internal {
  class modint_base {};

  template<typename T>
  concept is_modint = is_base_of_v<modint_base, T>;
}  // namespace asalib::_internal
#line 7 "library/_internal/types.hpp"

namespace asalib::_internal {
  // ---------- concept definition ---------- //
  template<class T>
  concept integral_like = integral<T> || is_modint<T>;

  template<class T>
  concept floating_like = floating_point<T>;

  template<class T>
  concept numeric_like = integral_like<T> || floating_like<T>;

  // ---------- function definition ---------- //
  template<class T>
  T plus(T a, T b) { return a + b; }

  template<class T>
  T minus(T a, T b) { return a - b; }

  // ---------- constant definition ---------- //
  template<class T>
  T zero() { return 0; }
}  // namespace asalib::_internal
#line 10 "library/_internal/math/inversion.hpp"

namespace asalib::_internal::math {
  template<integral_like T, bool eq, typename U, random_access_iterator iter>
  constexpr T inversion(iter first, iter last) {
    const size_t n = distance(first, last);
    if (n <= 1) return 0;
    const size_t mid = n >> 1;
    auto left = inversion<T, eq, U>(first, first + mid);
    auto right = inversion<T, eq, U>(first + mid, last);
    T inv = left + right;
    iter l = first, r = first + mid, l_end = first + mid, r_end = last;
    vector<U> tmp;
    tmp.reserve(n);
    while (l < l_end && r < r_end) {
      if constexpr (eq) {
        if (*l < *r) tmp.emplace_back(*l++);
        else {
          tmp.emplace_back(*r++);
          inv += mid - (l - first);
        }
      }
      else {
        if (*l <= *r) tmp.emplace_back(*l++);
        else {
          tmp.emplace_back(*r++);
          inv += mid - (l - first);
        }
      }
    }
    while (l < l_end) tmp.emplace_back(*l++);
    while (r < r_end) tmp.emplace_back(*r++);
    copy(tmp.begin(), tmp.end(), first);
    return inv;
  }
}  // namespace asalib::_internal::math
Back to top page