結果

問題 No.1300 Sum of Inversions
ユーザー IKyoproIKyopro
提出日時 2020-12-02 22:11:21
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 570 ms / 2,000 ms
コード長 4,475 bytes
コンパイル時間 2,683 ms
コンパイル使用メモリ 218,968 KB
実行使用メモリ 34,276 KB
最終ジャッジ日時 2023-10-11 12:01:08
合計ジャッジ時間 16,815 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,368 KB
testcase_01 AC 1 ms
4,372 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 418 ms
33,540 KB
testcase_04 AC 408 ms
33,508 KB
testcase_05 AC 322 ms
19,012 KB
testcase_06 AC 478 ms
34,104 KB
testcase_07 AC 466 ms
33,976 KB
testcase_08 AC 519 ms
34,136 KB
testcase_09 AC 517 ms
34,124 KB
testcase_10 AC 252 ms
18,584 KB
testcase_11 AC 254 ms
18,664 KB
testcase_12 AC 410 ms
33,520 KB
testcase_13 AC 400 ms
33,604 KB
testcase_14 AC 570 ms
34,156 KB
testcase_15 AC 507 ms
34,220 KB
testcase_16 AC 426 ms
33,692 KB
testcase_17 AC 239 ms
18,676 KB
testcase_18 AC 286 ms
18,952 KB
testcase_19 AC 371 ms
33,640 KB
testcase_20 AC 368 ms
33,484 KB
testcase_21 AC 370 ms
33,556 KB
testcase_22 AC 316 ms
19,084 KB
testcase_23 AC 477 ms
34,020 KB
testcase_24 AC 341 ms
19,036 KB
testcase_25 AC 264 ms
18,976 KB
testcase_26 AC 264 ms
18,912 KB
testcase_27 AC 305 ms
19,064 KB
testcase_28 AC 521 ms
34,060 KB
testcase_29 AC 350 ms
33,388 KB
testcase_30 AC 500 ms
34,088 KB
testcase_31 AC 304 ms
19,068 KB
testcase_32 AC 332 ms
19,100 KB
testcase_33 AC 29 ms
6,256 KB
testcase_34 AC 41 ms
6,228 KB
testcase_35 AC 245 ms
34,276 KB
testcase_36 AC 251 ms
34,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template<class T> bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b){if(a<b) {a = b; return true;} return false;}
#define rep(i,n) for(int i=0;i<(n);i++)
#define drep(i,n) for(int i=(n)-1;i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define debug(x)  cerr << #x << " = " << (x) << endl;

constexpr ll mod = 998244353;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod){}
    
    friend ostream &operator<<(ostream& os,const mint& a){
        return os << a.x;
    }

    friend istream &operator>>(istream& is,mint& a){
        ll t;
        is >> t;
        a = mint(t);
        return (is);
    }

    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        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;
    }
    // for prime mod
    mint inv() const {
        return pow(mod-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;
    }
};

template<typename Monoid,typename F>
class SegmentTree{
private:
    int sz;
    vector<Monoid> seg;
    const F op;
    const Monoid e;
public:
    SegmentTree(int n,const F op,const Monoid &e):op(op),e(e){
        sz = 1;
        while(sz<=n) sz <<= 1;
        seg.assign(2*sz,e);
    }
    void set(int k, const Monoid &x){
        seg[k+sz] = x;
    }
    void build(){
        for(int i=sz-1;i>0;i--){
            seg[i] = op(seg[2*i],seg[2*i+1]);
        }
    }
    void update(int k,const Monoid &x){
        k += sz;
        seg[k] += x;
        while(k>>=1){
            seg[k] = op(seg[2*k],seg[2*k+1]);
        }
    }
    Monoid query(int l,int r){
        Monoid L = e,R = e;
        for(l+=sz,r+=sz;l<r;l>>=1,r>>=1){
            if(l&1) L = op(L,seg[l++]);
            if(r&1) R = op(seg[--r],R);
        }
        return op(L,R);
    }
    Monoid operator[](const int &k)const{
        return seg[k+sz];
    }
};

template<class T>
class Compress {
    int N;
    map<int,T> value;
    vec<T> v;
    vec<T> cmp;
public:
    Compress(){}
    void add(T x){v.push_back(x);}
    void build(){
        for(auto &x:v) cmp.push_back(x);
        sort(cmp.begin(),cmp.end());
        cmp.erase(unique(cmp.begin(),cmp.end()),cmp.end());
        N = cmp.size();
    }
    int id(T val) {return lower_bound(cmp.begin(),cmp.end(),val)-cmp.begin();}
    T val(int id) {return cmp[id];}
    int size(){return N;}
};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    vec<int> A(N);
    Compress<int> cmp;
    rep(i,N){
        cin >> A[i];
        cmp.add(A[i]);
    }
    cmp.build();
    int M = cmp.size();
    auto op = [](mint a,mint b){return a+b;};
    SegmentTree<mint,decltype(op)> seg(M,op,(mint) 0);
    vec<SegmentTree<mint,decltype(op)>> L;
    vec<SegmentTree<mint,decltype(op)>> R;
    rep(i,3){
        L.push_back(seg);
        R.push_back(seg);
    }
    mint ans = 0;
    rep(i,N){
        int id = cmp.id(A[i]);
        ans += L[1].query(id+1,M)*A[i];
        drep(j,3){
            if(j) L[j].update(id,L[j-1].query(id+1,M));
            else L[j].update(id,1);
        }
    }
    drep(i,N){
        int id = cmp.id(A[i]);
        ans += R[1].query(0,id)*A[i];
        drep(j,3){
            if(j) R[j].update(id,R[j-1].query(0,id));
            else R[j].update(id,1);
        }
    }
    rep(i,M) L[0].set(i,0);
    L[0].build();
    rep(i,N){
        int id = cmp.id(A[i]);
        ans += L[0].query(id+1,M)*R[0].query(0,id)*A[i];
        L[0].update(id,1);
        R[0].update(id,-1);
    }
    cout << ans << "\n";
}
0