結果

問題 No.1300 Sum of Inversions
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2024-02-29 20:47:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 684 ms / 2,000 ms
コード長 3,358 bytes
コンパイル時間 2,615 ms
コンパイル使用メモリ 217,792 KB
実行使用メモリ 23,680 KB
最終ジャッジ日時 2024-02-29 20:47:25
合計ジャッジ時間 22,089 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 669 ms
19,072 KB
testcase_04 AC 596 ms
18,560 KB
testcase_05 AC 451 ms
16,000 KB
testcase_06 AC 684 ms
20,864 KB
testcase_07 AC 585 ms
20,096 KB
testcase_08 AC 629 ms
21,888 KB
testcase_09 AC 625 ms
21,760 KB
testcase_10 AC 286 ms
13,824 KB
testcase_11 AC 289 ms
13,952 KB
testcase_12 AC 465 ms
18,688 KB
testcase_13 AC 478 ms
18,304 KB
testcase_14 AC 643 ms
23,424 KB
testcase_15 AC 654 ms
21,632 KB
testcase_16 AC 502 ms
19,456 KB
testcase_17 AC 318 ms
13,568 KB
testcase_18 AC 310 ms
14,976 KB
testcase_19 AC 397 ms
17,024 KB
testcase_20 AC 420 ms
17,280 KB
testcase_21 AC 432 ms
17,280 KB
testcase_22 AC 389 ms
16,000 KB
testcase_23 AC 549 ms
20,864 KB
testcase_24 AC 407 ms
16,384 KB
testcase_25 AC 314 ms
14,592 KB
testcase_26 AC 299 ms
14,464 KB
testcase_27 AC 363 ms
15,616 KB
testcase_28 AC 644 ms
22,272 KB
testcase_29 AC 402 ms
17,152 KB
testcase_30 AC 651 ms
21,760 KB
testcase_31 AC 386 ms
16,128 KB
testcase_32 AC 376 ms
16,512 KB
testcase_33 AC 90 ms
11,136 KB
testcase_34 AC 101 ms
11,136 KB
testcase_35 AC 422 ms
23,680 KB
testcase_36 AC 430 ms
23,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const ll modc = 998244353;
class mint {
    ll x;
public:
    mint(ll x=0) : x((x%modc+modc)%modc) {}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint& a) {
        if ((x += a.x) >= modc) x -= modc;
        return *this;
    }
    mint& operator-=(const mint& a) {
        if ((x += modc-a.x) >= modc) x -= modc;
        return *this;
    }
    mint& operator*=(const  mint& a) {
        (x *= a.x) %= modc;
        return *this;
    }
    mint operator+(const mint& a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint& a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint& a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    mint inv() const {return pow(modc-2);}
    mint& operator/=(const mint& a){ return (*this) *= a.inv();}
    mint operator/(const mint& a) const {
        mint res(*this);
        return res/=a;
    }
    bool operator == (const mint& a) const{ return x == a.x;}
    bool operator != (const mint& a) const{ return x != a.x;}
    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
    friend istream& operator>>(istream& ip, mint &m) {
        ll t;
        ip >> t;
        m = mint(t);
        return ip;
    }
    ll val(){ return x;}
};

//RSQ (0-indexed)
template<class T> struct BIT {
    private:
        vector<T> bit; int N;
        T _sum(int r){
            r++; T res = 0;
            while(r > 0) res += bit[r-1], r -= r & -r;
            return res;
        }

    public:
        BIT (int n){ N = n; bit.resize(N);}
        BIT (vector<T> &a) : BIT(a.size()) { for (int i=0; i<N; i++) add(i, a[i]);}
        void add(int loc, T val){
            loc++;
            while(loc <= N) bit[loc-1] += val, loc += loc & -loc;
        }
        void change(int loc, T val){add(loc, -get(loc)+val);}
        T sum(int l, int r) {return _sum(r) - _sum(l-1);}
        T get(int l) {return sum(l, l);}
        void clear() {for (int i=0; i<N; i++) bit[i] = 0;}
};

template <typename T>
map<T, T> compress(vector<T> &A){

    map<T, T> comp;
    int N = A.size(), i=0;
    for (int i=0; i<N; i++) comp[A[i]];

    for (auto &e : comp){
        e.second = i;
        i++;
    }

    return comp;
}

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    ll N;
    cin >> N;
    vector<ll> A(N);
    for (int i=0; i<N; i++) cin >> A[i];
    map<ll, ll> mp = compress(A);

    BIT<mint> bitlc(N), bitrc(N), bitls(N), bitrs(N);
    mint ans = 0, lc, rc, ls, rs;

    for (int i=0; i<N; i++){
        bitrc.add(mp[A[i]], 1);
        bitrs.add(mp[A[i]], A[i]);
    }

    for (int i=0; i<N; i++){
        bitrc.add(mp[A[i]], -1);
        bitrs.add(mp[A[i]], -A[i]);
        lc = bitlc.sum(mp[A[i]]+1, N-1);
        rc = bitrc.sum(0, mp[A[i]]-1);
        ls = bitls.sum(mp[A[i]]+1, N-1);
        rs = bitrs.sum(0, mp[A[i]]-1);
        ans += lc*rc*A[i];
        ans += ls*rc;
        ans += lc*rs;
        bitlc.add(mp[A[i]], 1);
        bitls.add(mp[A[i]], A[i]);
    }

    cout << ans << endl;

    return 0;
}
0