結果

問題 No.1300 Sum of Inversions
ユーザー ぷらぷら
提出日時 2020-11-27 23:12:03
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 2,844 bytes
コンパイル時間 2,033 ms
コンパイル使用メモリ 180,368 KB
実行使用メモリ 18,876 KB
最終ジャッジ日時 2024-07-26 19:31:57
合計ジャッジ時間 8,205 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

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