結果

問題 No.1300 Sum of Inversions
ユーザー mel02mel02
提出日時 2020-11-27 22:06:59
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 418 ms / 2,000 ms
コード長 6,136 bytes
コンパイル時間 1,975 ms
コンパイル使用メモリ 179,228 KB
実行使用メモリ 32,168 KB
最終ジャッジ日時 2023-10-01 06:05:23
合計ジャッジ時間 12,789 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 285 ms
25,472 KB
testcase_04 AC 277 ms
24,624 KB
testcase_05 AC 214 ms
20,984 KB
testcase_06 AC 336 ms
28,048 KB
testcase_07 AC 312 ms
26,676 KB
testcase_08 AC 363 ms
29,436 KB
testcase_09 AC 360 ms
29,108 KB
testcase_10 AC 171 ms
18,268 KB
testcase_11 AC 170 ms
17,964 KB
testcase_12 AC 286 ms
25,004 KB
testcase_13 AC 268 ms
24,416 KB
testcase_14 AC 418 ms
31,424 KB
testcase_15 AC 351 ms
29,232 KB
testcase_16 AC 304 ms
26,116 KB
testcase_17 AC 167 ms
17,604 KB
testcase_18 AC 197 ms
19,604 KB
testcase_19 AC 251 ms
22,360 KB
testcase_20 AC 239 ms
22,936 KB
testcase_21 AC 255 ms
22,712 KB
testcase_22 AC 223 ms
20,924 KB
testcase_23 AC 337 ms
27,944 KB
testcase_24 AC 226 ms
21,436 KB
testcase_25 AC 184 ms
19,128 KB
testcase_26 AC 187 ms
18,888 KB
testcase_27 AC 218 ms
20,508 KB
testcase_28 AC 380 ms
29,904 KB
testcase_29 AC 245 ms
22,508 KB
testcase_30 AC 354 ms
29,244 KB
testcase_31 AC 222 ms
21,240 KB
testcase_32 AC 233 ms
21,728 KB
testcase_33 AC 94 ms
19,460 KB
testcase_34 AC 147 ms
19,404 KB
testcase_35 AC 221 ms
32,168 KB
testcase_36 AC 240 ms
32,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>


#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 namespace std;
using namespace atcoder;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> iint;
typedef pair<ll,ll> llll;
#define ALL(x) (x).begin(),(x).end()
const ll zero = 0;
const ll one = 1;
const ll INF = 9223372036854775807; //10^18
const int inINF = 2147483647; //10^9
const ll MOD = 998244353;
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int main(){
    int N;
    cin >> N;
    vector<ll> A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }
    map<ll, int> m;
    for (int i = 0; i < N; i++) {
        m[A[i]] = 1;
    }
    int ind = 1;
    for (auto v : m){
        m[v.first] = ind;
        ind++;
    }

    fenwick_tree<ll> f1(N+2);
    fenwick_tree<ll> f2(N+2);
    vector<int> B(N);
    for (ll i = 0; i < N; i++) {
        B[i] = m[A[i]];
    }
    ll ans = 0;
    vector<ll> I(N), J(N), K(N), L(N), R(N);
    for (int i = 0; i < N; i++) {
        K[i] = f2.sum(B[i] + 1, N+1);
        L[i] = f1.sum(B[i] + 1, N+1);
        f1.add(B[i], 1);
        f2.add(B[i], f1.sum(B[i] + 1, N+1));
    }

    fenwick_tree<ll> f3(N+2);
    fenwick_tree<ll> f4(N+2);
    for (int i = N-1; i >= 0; i--) {
        I[i] = f4.sum(0, B[i]);
        R[i] = f3.sum(0, B[i]);
        f3.add(B[i], 1);
        f4.add(B[i], f3.sum(0, B[i]));
    }   

    for (ll i = 0; i < N; i++) {
        J[i] = L[i] * R[i] % MOD;
        I[i] %= MOD;
        K[i] %= MOD;
    }
    // for (int i = 0; i < N; i++) {
    //     printf("%d %lld %d %lld %lld %lld\n", i, A[i], B[i], I[i], J[i], K[i]);
    // }
    // for (int i = 1; i < 4; i++) {
    //     printf("%d %lld %lld %lld %lld\n", i, f1.sum(i, i+1), f2.sum(i, i+1), f3.sum(i, i+1), f4.sum(i, i+1));
    // }


    for (ll i = 0; i < N; i++) {
        ans = (ans + A[i] * (I[i] + J[i] + K[i])) % MOD;
    }
    printf("%lld\n", ans);

}
0