結果

問題 No.1300 Sum of Inversions
ユーザー mel02mel02
提出日時 2020-11-27 22:06:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 310 ms / 2,000 ms
コード長 6,136 bytes
コンパイル時間 1,906 ms
コンパイル使用メモリ 180,916 KB
実行使用メモリ 32,384 KB
最終ジャッジ日時 2024-07-26 12:56:07
合計ジャッジ時間 10,302 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 217 ms
25,728 KB
testcase_04 AC 221 ms
24,960 KB
testcase_05 AC 174 ms
21,504 KB
testcase_06 AC 266 ms
28,288 KB
testcase_07 AC 245 ms
27,392 KB
testcase_08 AC 282 ms
29,696 KB
testcase_09 AC 289 ms
29,696 KB
testcase_10 AC 140 ms
18,304 KB
testcase_11 AC 141 ms
18,560 KB
testcase_12 AC 217 ms
25,344 KB
testcase_13 AC 222 ms
24,576 KB
testcase_14 AC 310 ms
31,872 KB
testcase_15 AC 279 ms
29,440 KB
testcase_16 AC 237 ms
26,240 KB
testcase_17 AC 134 ms
17,792 KB
testcase_18 AC 161 ms
19,968 KB
testcase_19 AC 193 ms
22,784 KB
testcase_20 AC 197 ms
23,296 KB
testcase_21 AC 198 ms
23,040 KB
testcase_22 AC 175 ms
21,504 KB
testcase_23 AC 277 ms
28,288 KB
testcase_24 AC 185 ms
21,888 KB
testcase_25 AC 153 ms
19,456 KB
testcase_26 AC 144 ms
19,200 KB
testcase_27 AC 168 ms
20,992 KB
testcase_28 AC 300 ms
30,336 KB
testcase_29 AC 189 ms
23,040 KB
testcase_30 AC 272 ms
29,568 KB
testcase_31 AC 173 ms
21,632 KB
testcase_32 AC 185 ms
22,272 KB
testcase_33 AC 86 ms
19,712 KB
testcase_34 AC 140 ms
19,712 KB
testcase_35 AC 208 ms
32,384 KB
testcase_36 AC 227 ms
32,256 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