結果

問題 No.1300 Sum of Inversions
ユーザー milanis48663220milanis48663220
提出日時 2020-11-27 21:51:48
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 932 ms / 2,000 ms
コード長 4,821 bytes
コンパイル時間 2,164 ms
コンパイル使用メモリ 114,480 KB
実行使用メモリ 48,672 KB
最終ジャッジ日時 2023-10-01 05:24:37
合計ジャッジ時間 24,098 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,668 KB
testcase_01 AC 4 ms
9,648 KB
testcase_02 AC 4 ms
9,752 KB
testcase_03 AC 716 ms
43,736 KB
testcase_04 AC 692 ms
43,168 KB
testcase_05 AC 542 ms
32,000 KB
testcase_06 AC 831 ms
46,168 KB
testcase_07 AC 804 ms
44,972 KB
testcase_08 AC 889 ms
47,168 KB
testcase_09 AC 863 ms
46,776 KB
testcase_10 AC 429 ms
29,720 KB
testcase_11 AC 430 ms
29,680 KB
testcase_12 AC 683 ms
43,384 KB
testcase_13 AC 653 ms
42,940 KB
testcase_14 AC 932 ms
48,672 KB
testcase_15 AC 854 ms
46,808 KB
testcase_16 AC 718 ms
44,228 KB
testcase_17 AC 410 ms
29,220 KB
testcase_18 AC 471 ms
31,048 KB
testcase_19 AC 602 ms
41,372 KB
testcase_20 AC 611 ms
41,760 KB
testcase_21 AC 597 ms
41,888 KB
testcase_22 AC 520 ms
32,088 KB
testcase_23 AC 811 ms
45,844 KB
testcase_24 AC 536 ms
32,656 KB
testcase_25 AC 473 ms
30,404 KB
testcase_26 AC 441 ms
30,188 KB
testcase_27 AC 508 ms
31,864 KB
testcase_28 AC 903 ms
47,868 KB
testcase_29 AC 613 ms
41,620 KB
testcase_30 AC 838 ms
46,824 KB
testcase_31 AC 555 ms
32,032 KB
testcase_32 AC 582 ms
32,796 KB
testcase_33 AC 23 ms
11,484 KB
testcase_34 AC 37 ms
11,308 KB
testcase_35 AC 460 ms
41,004 KB
testcase_36 AC 468 ms
41,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;


const ll MOD = 998244353;

class ModInt{
    public:
    ll v;
    ModInt(ll _v = 0){
        if(_v >= MOD) _v %= MOD;
        v = _v;
    }
    ModInt operator+(ll n){
        return ModInt((v+n)%MOD);
    }
    ModInt operator-(ll n){
        return ModInt((v-n+MOD)%MOD);
    }
    ModInt operator*(ll n){
        if(n >= MOD) n %= MOD;
        return ModInt((v*n)%MOD);
    }
    ModInt operator/(ll n){
        return ModInt((ModInt(n).inv()*v).v%MOD);
    }

    void operator+=(ll n){
        v = (v+n)%MOD;
    }
    void operator-=(ll n){
        v = (v-n+MOD)%MOD;
    }
    void operator*=(ll n){
        v = (v*n)%MOD;
    }
    
    
    ModInt operator+(ModInt n){
        return ModInt((v+n.v)%MOD);
    }
    ModInt operator-(ModInt n){
        return ModInt((v-n.v+MOD)%MOD);
    }
    ModInt operator*(ModInt n){
        return ModInt((v*n.v)%MOD);
    }
    ModInt operator/(ModInt n){
        return ModInt((n.inv()*v).v%MOD);
    }

    void operator+=(ModInt n){
        v = (v+n.v)%MOD;
    }
    void operator-=(ModInt n){
        v = (v-n.v+MOD)%MOD;
    }
    void operator*=(ModInt n){
        v = (v*n.v)%MOD;
    }

    void operator=(ModInt n){
        v = n.v;
    }
    void operator=(ll n){
        v = n;
    }

    ModInt inv(){
        if(v == 1) return ModInt(1);
        else return ModInt(MOD-ModInt(MOD%v).inv().v*(MOD/v)%MOD);
    }
};

ostream& operator<<(ostream& os, const ModInt& m){
    os << m.v;
    return os;
}

istream & operator >> (istream &in,  ModInt &m){
    in >> m.v;
    return in;
}


int n;
ll a[200000];

ModInt cntl[200000], cntr[200000], suml[200000], sumr[200000];

map<ll, int> compress;

void make_compress(){
    set<ll> st;
    for(int i = 0; i < n; i++) st.insert(a[i]);
    int cnt = 0;
    for(ll x : st){
        compress[x] = cnt;
        cnt++;
    }
}


template <typename T>
struct segtree{
    int n;
    T UNIT;
    vector<T> dat;
    T (*calc)(T, T);

    segtree(int n_, T unit, T (*_calc)(T, T)){
        UNIT = unit;
        calc = _calc;
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(2*n);
        for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
    }

    void insert(int k, T a){
        dat[k+n-1] = a;
    }
    void update_all(){
        for(int i = n-2; i >= 0; i--){
            dat[i] = calc(dat[i*2+1], dat[i*2+2]);
        }
    }
    //k番目の値(0-indexed)をaに変更
    void update(int k, T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = calc(dat[k*2+1], dat[k*2+2]);
        }
    }

    //[a, b)
    //区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
    T query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;
        if(r <= a || b <= l) return UNIT;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a, b, k*2+1, l, (l+r)/2);
            T vr = query(a, b, k*2+2, (l+r)/2, r);
            return calc(vl, vr);
        }
    }
};

struct node{
    ModInt cnt, sum;
};

node add_node(node a, node b){
    return (node){a.cnt+b.cnt, a.sum+b.sum};
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    make_compress();
    int m = compress.size();
    node unit = (node){0, 0};
    segtree<node> sgtl(m, unit, add_node);
    segtree<node> sgtr(m, unit, add_node);
    for(int i = 0; i < n; i++){
        int idx = compress[a[i]];
        auto nd = sgtl.query(idx+1, m);
        cntl[i] = nd.cnt;
        suml[i] = nd.sum;
        auto tmp = sgtl.query(idx, idx+1);
        tmp.cnt+=1;
        tmp.sum+=a[i];
        sgtl.update(idx, tmp);
    }

    for(int i = n-1; i >= 0; i--){
        int idx = compress[a[i]];
        auto nd = sgtr.query(0, idx);
        cntr[i] = nd.cnt;
        sumr[i] = nd.sum;
        auto tmp = sgtr.query(idx, idx+1);
        tmp.cnt+=1;
        tmp.sum+=a[i]; 
        sgtr.update(idx, tmp);
    }

    ModInt ans = 0;
    for(int i = 1; i < n-1; i++){
        ans += cntl[i]*sumr[i];
        ans += cntr[i]*suml[i];
        ans += cntl[i]*cntr[i]*a[i];
        // cout << suml[i] << ' ' << sumr[i] << endl;
    }
    cout << ans << endl;
}
0