結果

問題 No.1300 Sum of Inversions
ユーザー carrot46carrot46
提出日時 2020-11-28 11:43:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 206 ms / 2,000 ms
コード長 3,759 bytes
コンパイル時間 1,591 ms
コンパイル使用メモリ 175,252 KB
実行使用メモリ 14,664 KB
最終ジャッジ日時 2023-10-09 23:49:46
合計ジャッジ時間 8,075 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 139 ms
12,148 KB
testcase_04 AC 136 ms
11,236 KB
testcase_05 AC 106 ms
9,828 KB
testcase_06 AC 162 ms
12,284 KB
testcase_07 AC 160 ms
12,084 KB
testcase_08 AC 176 ms
13,208 KB
testcase_09 AC 169 ms
12,864 KB
testcase_10 AC 90 ms
8,772 KB
testcase_11 AC 88 ms
8,724 KB
testcase_12 AC 140 ms
11,268 KB
testcase_13 AC 134 ms
11,076 KB
testcase_14 AC 206 ms
13,728 KB
testcase_15 AC 168 ms
12,980 KB
testcase_16 AC 150 ms
12,904 KB
testcase_17 AC 86 ms
8,656 KB
testcase_18 AC 100 ms
9,384 KB
testcase_19 AC 128 ms
11,044 KB
testcase_20 AC 127 ms
11,620 KB
testcase_21 AC 128 ms
11,232 KB
testcase_22 AC 108 ms
9,680 KB
testcase_23 AC 166 ms
12,824 KB
testcase_24 AC 115 ms
10,132 KB
testcase_25 AC 94 ms
9,272 KB
testcase_26 AC 93 ms
9,240 KB
testcase_27 AC 112 ms
9,652 KB
testcase_28 AC 187 ms
13,948 KB
testcase_29 AC 124 ms
10,552 KB
testcase_30 AC 176 ms
12,976 KB
testcase_31 AC 114 ms
9,836 KB
testcase_32 AC 115 ms
10,180 KB
testcase_33 AC 99 ms
14,664 KB
testcase_34 AC 148 ms
14,296 KB
testcase_35 AC 119 ms
13,816 KB
testcase_36 AC 140 ms
14,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("O3")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T &b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T &b){
    if(a < b) {a = b; return true;}
    else return false;
}

template <unsigned long long mod > class modint{
public:
    ll x;
    constexpr modint(){x = 0;}
    constexpr modint(ll _x) : x((_x < 0 ? ((_x += (LLONG_MAX / mod) * mod) < 0 ? _x + (LLONG_MAX / mod) * mod : _x) : _x)%mod){}
    constexpr modint set_raw(ll _x){
        //_x in [0, mod)
        x = _x;
        return *this;
    }
    constexpr modint operator-(){
        return x == 0 ? 0 : mod - x;
    }
    constexpr modint& operator+=(const modint& a){
        if((x += a.x) >= mod) x -= mod;
        return *this;
    }
    constexpr modint operator+(const modint& a) const{
        return modint(*this) += a;
    }
    constexpr modint& operator-=(const modint& a){
        if((x -= a.x) < 0) x += mod;
        return *this;
    }
    constexpr modint operator-(const modint& a) const{
        return modint(*this) -= a;
    }
    constexpr modint& operator*=(const modint& a){
        (x *= a.x)%=mod;
        return *this;
    }
    constexpr modint operator*(const modint& a) const{
        return modint(*this) *= a;
    }
    constexpr modint pow(unsigned long long pw) const{
        modint res(1), comp(*this);
        while(pw){
            if(pw&1) res *= comp;
            comp *= comp;
            pw >>= 1;
        }
        return res;
    }
    //以下、modが素数のときのみ
    constexpr modint inv() const{
        if(x == 2) return (mod + 1) >> 1;
        return modint(*this).pow(mod - 2);
    }
    constexpr modint& operator/=(const modint &a){
        (x *= a.inv().x)%=mod;
        return *this;
    }
    constexpr modint operator/(const modint &a) const{
        return modint(*this) /= a;
    }
};
#define mod1 998244353
using mint = modint<mod1>;

ostream& operator<<(ostream& os, const mint& a){
    os << a.x;
    return os;
}
using vm = vector<mint>;

template <class T> class BIT {
    //T has operator "+=" and can be initialized with 0.
    unsigned int n;
    vector<T> bitree;
public:
    BIT(unsigned long _n) : bitree(_n + 1, 0) {
        n = _n;
    }

    void add(int id, T x) {
        ++id;
        while (id <= n) {
            bitree[id] += x;
            id += id & -id;
        }
    }

    T sum(int id) {
        ++id;
        T temp(0);
        while (id > 0) {
            temp += bitree[id];
            id -= id & -id;
        }
        return temp;
    }

    void print(int sz = -1){
        if(sz == -1) sz = n;
        rep(i, sz) cout<<sum(i)<<" \n"[i == sz - 1];
    }
};

int main(){
    cin>>N;
    vec a(N);
    priority_queue<P> pque;
    rep(i, N) {
        cin>>a[i];
        pque.emplace(a[i], i);
    }
    BIT<mint> ni(N), si(N), nj(N), sj(N);
    mint ans(0);
    while(!pque.empty()){
        P p = pque.top(); pque.pop();
        //cout<<p.first<<' '<<p.second<<endl;
        int id = p.second;
        ans += sj.sum(id) + nj.sum(id) * a[id];
        sj.add(id, si.sum(id) + ni.sum(id) * a[id]);
        nj.add(id, ni.sum(id));
        si.add(id, a[id]);
        ni.add(id, 1);
        /*
        sj.print();
        nj.print();
        si.print();
        ni.print();
         */
    }
    cout<<ans<<endl;
}
0