結果

問題 No.1300 Sum of Inversions
ユーザー ShibuyapShibuyap
提出日時 2021-02-06 05:10:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 1,814 bytes
コンパイル時間 3,370 ms
コンパイル使用メモリ 209,416 KB
実行使用メモリ 13,148 KB
最終ジャッジ日時 2023-09-15 11:18:43
合計ジャッジ時間 9,577 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 111 ms
10,308 KB
testcase_04 AC 107 ms
10,092 KB
testcase_05 AC 89 ms
8,780 KB
testcase_06 AC 123 ms
11,164 KB
testcase_07 AC 118 ms
10,700 KB
testcase_08 AC 133 ms
11,704 KB
testcase_09 AC 130 ms
11,408 KB
testcase_10 AC 73 ms
7,880 KB
testcase_11 AC 72 ms
7,780 KB
testcase_12 AC 112 ms
10,100 KB
testcase_13 AC 108 ms
9,984 KB
testcase_14 AC 145 ms
12,260 KB
testcase_15 AC 133 ms
11,476 KB
testcase_16 AC 113 ms
10,616 KB
testcase_17 AC 70 ms
7,768 KB
testcase_18 AC 80 ms
8,304 KB
testcase_19 AC 96 ms
9,364 KB
testcase_20 AC 101 ms
9,492 KB
testcase_21 AC 97 ms
9,540 KB
testcase_22 AC 88 ms
8,720 KB
testcase_23 AC 127 ms
11,300 KB
testcase_24 AC 92 ms
8,948 KB
testcase_25 AC 78 ms
8,176 KB
testcase_26 AC 76 ms
8,136 KB
testcase_27 AC 86 ms
8,812 KB
testcase_28 AC 140 ms
11,764 KB
testcase_29 AC 95 ms
9,520 KB
testcase_30 AC 134 ms
11,500 KB
testcase_31 AC 88 ms
8,892 KB
testcase_32 AC 92 ms
8,980 KB
testcase_33 AC 58 ms
13,148 KB
testcase_34 AC 111 ms
13,144 KB
testcase_35 AC 91 ms
12,468 KB
testcase_36 AC 111 ms
12,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005
const ll MOD = 998244353;

// long long version
ll bit[MAX_N+1][2];
int nn = MAX_N; // 1-index

void adds(int a, ll w, int idx){
    for(int x = a; x <= nn; x += x & -x) bit[x][idx] += w;
}

// bit[1,a]の和を返す.
ll sums(int a, int idx){ 
    ll ret = 0;
    for(int x = a; x > 0; x -= x & -x) ret += bit[x][idx];
    return ret;
}

int main() {
    ll n; cin >> n;
    ll a[n+1] = {};
    srep(i,1,n+1) cin >> a[i];

    ll sum[n+1] = {};
    srep(i,1,n+1) sum[i] = sum[i-1] + a[i];

    vector<P> v(n);
    srep(i,1,n+1){
        v[i-1].first = a[i];
        v[i-1].second = i;
    }
    sort(v.begin(), v.end(),greater<P>());

    ll ans = 0;
    rep(i,n){
        ll x = v[i].first;
        vector<int> keep;
        while(i<n&&v[i].first==x){
            keep.push_back(v[i].second);
            i++;
        }
        reverse(keep.begin(), keep.end());

        rep(j,keep.size()){
            int y = keep[j];
            ll left = sums(y,1) - j;
            ll right = n - y - (sums(n,1) - sums(y,1)) - (keep.size()-1-j);
            ans += x * left % MOD * right % MOD;

            ll sumleft = sums(y,0) - x*j;
            ll sumright = sum[n] - sum[y] - (sums(n,0) - sums(y,0)) - x*(keep.size()-1-j);
            sumleft %= MOD;
            sumright %= MOD;
            ans += sumleft * right % MOD;
            ans += sumright * left % MOD;

            ans %= MOD;

            adds(y,x,0);
            adds(y,1,1);
        }

        i--;
    }

    cout << ans << endl;
    return 0;
}


0