結果

問題 No.1300 Sum of Inversions
ユーザー ぷらぷら
提出日時 2020-11-27 23:12:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 194 ms / 2,000 ms
コード長 2,844 bytes
コンパイル時間 1,782 ms
コンパイル使用メモリ 177,404 KB
実行使用メモリ 18,836 KB
最終ジャッジ日時 2023-10-09 21:05:41
合計ジャッジ時間 8,502 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 144 ms
15,248 KB
testcase_04 AC 140 ms
14,668 KB
testcase_05 AC 113 ms
12,776 KB
testcase_06 AC 168 ms
16,456 KB
testcase_07 AC 158 ms
15,944 KB
testcase_08 AC 180 ms
17,208 KB
testcase_09 AC 175 ms
17,192 KB
testcase_10 AC 95 ms
11,252 KB
testcase_11 AC 92 ms
11,192 KB
testcase_12 AC 144 ms
14,976 KB
testcase_13 AC 138 ms
14,676 KB
testcase_14 AC 194 ms
18,836 KB
testcase_15 AC 176 ms
17,304 KB
testcase_16 AC 152 ms
15,584 KB
testcase_17 AC 90 ms
11,192 KB
testcase_18 AC 107 ms
11,992 KB
testcase_19 AC 127 ms
13,652 KB
testcase_20 AC 132 ms
13,964 KB
testcase_21 AC 130 ms
13,708 KB
testcase_22 AC 113 ms
12,820 KB
testcase_23 AC 164 ms
16,548 KB
testcase_24 AC 120 ms
13,076 KB
testcase_25 AC 98 ms
11,756 KB
testcase_26 AC 95 ms
11,576 KB
testcase_27 AC 110 ms
12,496 KB
testcase_28 AC 187 ms
17,572 KB
testcase_29 AC 124 ms
13,680 KB
testcase_30 AC 180 ms
16,956 KB
testcase_31 AC 118 ms
12,720 KB
testcase_32 AC 121 ms
12,992 KB
testcase_33 AC 94 ms
18,664 KB
testcase_34 AC 148 ms
18,616 KB
testcase_35 AC 121 ms
18,548 KB
testcase_36 AC 141 ms
18,508 KB
権限があれば一括ダウンロードができます

ソースコード

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)%mod;
        ans %= mod;
        ans += cnt1[B[i].second]%mod*(bt4.sum(1,B[i].second)%mod)%mod;
        ans %= mod;
        ans += cnt2[B[i].second]%mod*(bt4.sum(1,B[i].second)%mod)%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)%mod;
            ans %= mod;
            ans += cnt1[B[j].second]%mod*(bt4.sum(1,B[j].second)%mod)%mod;
            ans %= mod;
            ans += cnt2[B[j].second]%mod*(bt4.sum(1,B[j].second)%mod)%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