結果

問題 No.1300 Sum of Inversions
ユーザー IKyoproIKyopro
提出日時 2020-12-02 22:04:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,457 bytes
コンパイル時間 2,552 ms
コンパイル使用メモリ 217,252 KB
実行使用メモリ 34,404 KB
最終ジャッジ日時 2023-10-11 12:00:51
合計ジャッジ時間 15,133 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 2 ms
4,368 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 AC 27 ms
6,180 KB
testcase_34 AC 41 ms
6,344 KB
testcase_35 AC 224 ms
34,276 KB
testcase_36 AC 232 ms
34,164 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]);
        drep(j,3){
            if(j) L[j].update(id,L[j-1].query(id+1,M));
            else L[j].update(id,1);
        }
        ans += L[2][id]*A[i];
    }
    drep(i,N){
        int id = cmp.id(A[i]);
        drep(j,3){
            if(j) R[j].update(id,R[j-1].query(0,id));
            else R[j].update(id,1);
        }
        ans += R[2][id]*A[i];
    }
    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