結果

問題 No.1300 Sum of Inversions
ユーザー ぷらぷら
提出日時 2020-11-27 23:08:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,808 bytes
コンパイル時間 2,135 ms
コンパイル使用メモリ 177,520 KB
実行使用メモリ 19,100 KB
最終ジャッジ日時 2023-10-09 21:03:25
合計ジャッジ時間 8,221 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 131 ms
15,132 KB
testcase_04 AC 131 ms
14,556 KB
testcase_05 AC 108 ms
12,744 KB
testcase_06 AC 153 ms
16,636 KB
testcase_07 AC 147 ms
16,032 KB
testcase_08 AC 164 ms
17,192 KB
testcase_09 AC 160 ms
17,328 KB
testcase_10 AC 87 ms
11,204 KB
testcase_11 AC 87 ms
11,208 KB
testcase_12 AC 130 ms
14,824 KB
testcase_13 AC 126 ms
14,616 KB
testcase_14 AC 176 ms
18,796 KB
testcase_15 AC 163 ms
17,064 KB
testcase_16 AC 137 ms
15,352 KB
testcase_17 AC 83 ms
11,084 KB
testcase_18 AC 99 ms
12,040 KB
testcase_19 AC 117 ms
13,536 KB
testcase_20 AC 123 ms
13,844 KB
testcase_21 AC 118 ms
13,560 KB
testcase_22 AC 106 ms
12,700 KB
testcase_23 AC 150 ms
16,468 KB
testcase_24 AC 112 ms
13,104 KB
testcase_25 AC 93 ms
11,712 KB
testcase_26 AC 91 ms
11,504 KB
testcase_27 AC 102 ms
12,440 KB
testcase_28 AC 169 ms
17,456 KB
testcase_29 AC 120 ms
13,548 KB
testcase_30 AC 169 ms
17,160 KB
testcase_31 AC 108 ms
12,864 KB
testcase_32 AC 112 ms
13,096 KB
testcase_33 AC 88 ms
19,040 KB
testcase_34 AC 141 ms
18,812 KB
testcase_35 AC 114 ms
18,440 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)-sum(l-1);
    }
};
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);
        int j = i+1;
        while (j < N) {
            if(A[j-1].first != A[j].first) {
                break;
            }
            cnt1[A[j].second] = bt.sum(A[j].second,N);
            cnt2[A[j].second] = bt2.sum(A[j].second,N);
            j++;
        }
        bt.add(A[i].second,A[i].first);
        bt2.add(A[i].second,1);
        j = i+1;
        while (j < N) {
            if(A[j-1].first != A[j].first) {
                break;
            }
            i = j;
            bt.add(A[j].second,A[j].first);
            bt2.add(A[j].second,1);
            j++;
        }
    }
    int ans = 0;
    for(int i = 0; i < N; i++) {
        ans += cnt2[B[i].second]%mod*bt3.sum(1,B[i].second)%mod;
        ans %= mod;
        ans += cnt1[B[i].second]%mod*bt4.sum(1,B[i].second)%mod;
        ans %= mod;
        ans += cnt2[B[i].second]%mod*bt4.sum(1,B[i].second)%mod*B[i].first%mod;
        ans %= mod;
        int j = i+1;
        while (j < N) {
            if(B[j-1].first != B[j].first) {
                break;
            }
            ans += cnt2[B[j].second]%mod*bt3.sum(1,B[j].second)%mod;
            ans %= mod;
            ans += cnt1[B[j].second]%mod*bt4.sum(1,B[j].second)%mod;
            ans %= mod;
            ans += cnt2[B[j].second]%mod*bt4.sum(1,B[j].second)%mod*B[j].first%mod;
            ans %= mod;
            j++;
        }
        bt3.add(B[i].second,B[i].first);
        bt4.add(B[i].second,1);
        j = i+1;
        while (j < N) {
            if(B[j-1].first != B[j].first) {
                break;
            }
            i = j;
            bt3.add(B[j].second,B[j].first);
            bt4.add(B[j].second,1);
            j++;
        }
    }
    cout << ans << endl;
}
0