結果

問題 No.1300 Sum of Inversions
ユーザー ぷらぷら
提出日時 2020-11-27 22:50:45
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,602 bytes
コンパイル時間 1,654 ms
コンパイル使用メモリ 175,740 KB
実行使用メモリ 18,668 KB
最終ジャッジ日時 2023-10-09 20:52:44
合計ジャッジ時間 7,311 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 128 ms
15,220 KB
testcase_04 AC 125 ms
14,788 KB
testcase_05 AC 100 ms
12,768 KB
testcase_06 AC 142 ms
16,468 KB
testcase_07 AC 131 ms
16,000 KB
testcase_08 AC 149 ms
17,216 KB
testcase_09 AC 145 ms
17,200 KB
testcase_10 AC 80 ms
11,204 KB
testcase_11 AC 83 ms
11,256 KB
testcase_12 AC 119 ms
14,832 KB
testcase_13 AC 114 ms
14,568 KB
testcase_14 AC 156 ms
18,584 KB
testcase_15 AC 141 ms
17,196 KB
testcase_16 AC 130 ms
15,480 KB
testcase_17 AC 81 ms
10,968 KB
testcase_18 AC 89 ms
12,048 KB
testcase_19 AC 111 ms
13,568 KB
testcase_20 AC 111 ms
13,832 KB
testcase_21 AC 106 ms
13,572 KB
testcase_22 AC 95 ms
12,836 KB
testcase_23 AC 137 ms
16,400 KB
testcase_24 AC 101 ms
13,124 KB
testcase_25 AC 89 ms
11,724 KB
testcase_26 AC 88 ms
11,460 KB
testcase_27 AC 96 ms
12,580 KB
testcase_28 AC 148 ms
17,464 KB
testcase_29 AC 103 ms
13,572 KB
testcase_30 AC 141 ms
17,256 KB
testcase_31 AC 99 ms
12,724 KB
testcase_32 AC 98 ms
13,056 KB
testcase_33 AC 81 ms
18,584 KB
testcase_34 AC 134 ms
18,668 KB
testcase_35 AC 106 ms
18,520 KB
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
int INF = 3e18+7;
int mod = 998244353;
int dx[] = {1, 0,-1, 0, 1, 1,-1,-1};
int dy[] = {0, 1, 0,-1, 1,-1, 1,-1};
struct BIT{
    int N;
    vector<int>bit;
    BIT(int x) {
        N = x;
        bit.resize(x+1);
    }
    void add(int x,int y) {
        while(x <= N) {
            bit[x] += y;
            x += x&-x;
        }
    }
    int sum(int x) {
        int res = 0;
        while(x) {
            res += bit[x];
            x-=x&-x;
        }
        return res;
    }
    int sum(int l, int r) {
        return sum(r+1)-sum(l);
    }
};
signed main() {
    int N;
    cin >> N;
    vector<P>A(N);
    for(int i = 0; i < N; i++) {
        cin >> A[i].first;
        A[i].second = i+1;
    }
    sort(A.begin(),A.end());
    vector<P>B = A;
    reverse(B.begin(),B.end());
    BIT bt(N+5),bt2(N+5),bt3(N+5),bt4(N+5);
    vector<int>cnt1(N+5),cnt2(N+5);
    for(int i = 0; i < N; i++) {
        cnt1[A[i].second] = bt.sum(A[i].second,N);
        cnt2[A[i].second] = bt2.sum(A[i].second,N);
        bt.add(A[i].second,A[i].first);
        bt2.add(A[i].second,1);
    }
    int ans = 0;
    for(int i = 0; i < N; i++) {
        ans += cnt2[B[i].second]%mod*bt3.sum(0,B[i].second-1)%mod;
        ans %= mod;
        ans += cnt1[B[i].second]%mod*bt4.sum(0,B[i].second-1)%mod;
        ans %= mod;
        ans += cnt2[B[i].second]%mod*bt4.sum(0,B[i].second-1)%mod*B[i].first%mod;
        ans %= mod;
        bt3.add(B[i].second,B[i].first);
        bt4.add(B[i].second,1);
    }
    cout << ans << endl;
}
0