結果

問題 No.1300 Sum of Inversions
ユーザー IKyoproIKyopro
提出日時 2020-12-02 22:11:21
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 545 ms / 2,000 ms
コード長 4,475 bytes
コンパイル時間 3,232 ms
コンパイル使用メモリ 213,092 KB
最終ジャッジ日時 2025-01-16 13:36:56
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

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