proconlib

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

View the Project on GitHub anqooqie/proconlib

:heavy_check_mark: The number of inversions (tools/inversion_number.hpp)

template <::std::ranges::range R>
long long inversion_number(R&& a);

It returns the number of inversions of a given sequence $(a_0, a_1, \ldots, a_{n - 1})$.

Constraints

Time Complexity

License

Author

Depends on

Verified with

Code

#ifndef TOOLS_INVERSION_NUMBER_HPP
#define TOOLS_INVERSION_NUMBER_HPP

#include <algorithm>
#include <iterator>
#include <ranges>
#include <vector>
#include "atcoder/fenwicktree.hpp"
#include "tools/compress.hpp"

namespace tools {

  template <::std::ranges::range R>
  long long inversion_number(R&& a) {
    ::std::vector<int> compressed;
    ::tools::compress(a, ::std::back_inserter(compressed));

    if (compressed.empty()) return 0;

    const auto max = *::std::ranges::max_element(compressed);
    ::atcoder::fenwick_tree<int> fw(max + 1);
    long long res = 0;
    for (const auto x : compressed) {
      res += fw.sum(x + 1, max + 1);
      fw.add(x, 1);
    }

    return res;
  }
}

#endif
#line 1 "tools/inversion_number.hpp"



#include <algorithm>
#include <iterator>
#include <ranges>
#include <vector>
#line 1 "lib/ac-library/atcoder/fenwicktree.hpp"



#include <cassert>
#line 6 "lib/ac-library/atcoder/fenwicktree.hpp"

#line 1 "lib/ac-library/atcoder/internal_type_traits.hpp"



#line 5 "lib/ac-library/atcoder/internal_type_traits.hpp"
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder


#line 8 "lib/ac-library/atcoder/fenwicktree.hpp"

namespace atcoder {

// Reference: https://en.wikipedia.org/wiki/Fenwick_tree
template <class T> struct fenwick_tree {
    using U = internal::to_unsigned_t<T>;

  public:
    fenwick_tree() : _n(0) {}
    explicit fenwick_tree(int n) : _n(n), data(n) {}

    void add(int p, T x) {
        assert(0 <= p && p < _n);
        p++;
        while (p <= _n) {
            data[p - 1] += U(x);
            p += p & -p;
        }
    }

    T sum(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        return sum(r) - sum(l);
    }

  private:
    int _n;
    std::vector<U> data;

    U sum(int r) {
        U s = 0;
        while (r > 0) {
            s += data[r - 1];
            r -= r & -r;
        }
        return s;
    }
};

}  // namespace atcoder


#line 1 "tools/compress.hpp"



#line 1 "tools/lower_bound.hpp"



#line 6 "tools/lower_bound.hpp"

namespace tools {

  template <class ForwardIterator, class T>
  auto lower_bound(ForwardIterator first, ForwardIterator last, const T& value) {
    return ::std::distance(first, ::std::lower_bound(first, last, value));
  }

  template <class ForwardIterator, class T, class Compare>
  auto lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp) {
    return ::std::distance(first, ::std::lower_bound(first, last, value, comp));
  }
}


#line 8 "tools/compress.hpp"

namespace tools {
  template <::std::ranges::range R, typename OutputIterator>
  void compress(R&& a, OutputIterator result) {
    using T = typename ::std::ranges::range_value_t<R>;
    if constexpr (::std::ranges::forward_range<R>) {
      ::std::vector<T> sorted(::std::ranges::begin(a), ::std::ranges::end(a));
      ::std::ranges::sort(sorted);
      sorted.erase(::std::unique(sorted.begin(), sorted.end()), sorted.end());
      for (auto it = ::std::ranges::begin(a); it != ::std::ranges::end(a); ++it, ++result) {
        *result = ::tools::lower_bound(sorted.begin(), sorted.end(), *it);
      }
    } else {
      ::tools::compress(::std::vector<T>(::std::ranges::begin(a), ::std::ranges::end(a)), result);
    }
  }
}


#line 10 "tools/inversion_number.hpp"

namespace tools {

  template <::std::ranges::range R>
  long long inversion_number(R&& a) {
    ::std::vector<int> compressed;
    ::tools::compress(a, ::std::back_inserter(compressed));

    if (compressed.empty()) return 0;

    const auto max = *::std::ranges::max_element(compressed);
    ::atcoder::fenwick_tree<int> fw(max + 1);
    long long res = 0;
    for (const auto x : compressed) {
      res += fw.sum(x + 1, max + 1);
      fw.add(x, 1);
    }

    return res;
  }
}


Back to top page