結果

問題 No.1270 Range Arrange Query
ユーザー MisterMister
提出日時 2020-10-23 23:04:09
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3,798 ms / 7,000 ms
コード長 5,958 bytes
コンパイル時間 835 ms
コンパイル使用メモリ 83,752 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-28 17:44:25
合計ジャッジ時間 33,482 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 45 ms
4,380 KB
testcase_07 AC 2,546 ms
4,380 KB
testcase_08 AC 78 ms
4,376 KB
testcase_09 AC 1,366 ms
4,376 KB
testcase_10 AC 1,683 ms
4,380 KB
testcase_11 AC 3,753 ms
4,380 KB
testcase_12 AC 3,798 ms
4,380 KB
testcase_13 AC 3,788 ms
4,376 KB
testcase_14 AC 3,449 ms
4,376 KB
testcase_15 AC 3,760 ms
4,376 KB
testcase_16 AC 3,598 ms
4,380 KB
testcase_17 AC 3,662 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>


#include <cassert>
#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

#include <cassert>
#include <vector>

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) {}
    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


using lint = long long;

void solve() {
    int n, q;
    std::cin >> n >> q;

    std::vector<int> xs(n);
    for (auto& x : xs) {
        std::cin >> x;
        --x;
    }

    std::vector<lint> fore(n + 1, 0);  // [0, i)
    {
        atcoder::fenwick_tree<lint> bit(n);
        for (int i = 0; i < n; ++i) {
            fore[i + 1] = fore[i] + bit.sum(xs[i] + 1, n);
            bit.add(xs[i], 1);
        }
    }

    std::vector<lint> back(n + 1, 0);  // [i, n)
    {
        atcoder::fenwick_tree<lint> bit(n);
        for (int i = n - 1; i >= 0; --i) {
            back[i] = back[i + 1] + bit.sum(0, xs[i]);
            bit.add(xs[i], 1);
        }
    }

    while (q--) {
        int l, r;
        std::cin >> l >> r;
        --l;  // [l, r)

        // [0, l)と[r, n)
        lint ans = fore[l] + back[r];

        std::vector<lint> inv(n, 0);

        std::vector<lint> fs(n, 0);  // [i, n)
        for (int i = 0; i < l; ++i) ++fs[xs[i]];

        {
            lint acc = 0;
            for (int i = n - 1; i >= 0; --i) {
                inv[i] += acc;
                acc += fs[i];
                fs[i] = acc;
            }
        }

        {
            std::vector<lint> bs(n, 0);
            for (int i = r; i < n; ++i) ++bs[xs[i]];

            lint acc = 0;
            for (int i = 0; i < n; ++i) {
                if (i + 1 < n) ans += fs[i + 1] * bs[i];
                inv[i] += acc;
                acc += bs[i];
            }
        }

        ans += *std::min_element(inv.begin(), inv.end()) * (r - l);
        std::cout << ans << "\n";
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0