結果

問題 No.2616 中央番目の中央値
ユーザー Today03
提出日時 2025-04-23 14:24:21
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,371 ms / 2,000 ms
コード長 6,008 bytes
コンパイル時間 3,469 ms
コンパイル使用メモリ 285,876 KB
実行使用メモリ 62,528 KB
最終ジャッジ日時 2025-04-23 14:24:51
合計ジャッジ時間 26,695 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define ALL(x) (x).begin(),(x).end()
#define IO ios::sync_with_stdio(false),cin.tie(nullptr);
#define LB(v, x) (ll)(lower_bound(ALL(v),x)-(v).begin())
#define UQ(v) sort(ALL(v)),(v).erase(unique(ALL(v)),v.end())
#define REP(i, n) for(ll i=0; i<(ll)(n); i++)
#define FOR(i, a, b) for(ll i=(ll)(a); (a)<(b) ? i<(b) : i>(b); i+=((a)<(b) ? 1 : -1))
#define chmax(a, b) ((a)<(b) ? ((a)=(b), 1) : 0)
#define chmin(a, b) ((a)>(b) ? ((a)=(b), 1) : 0)

template<typename T> using rpriority_queue=priority_queue<T,vector<T>,greater<T>>;
using ll=long long; const int INF=1e9+10; const ll INFL=4e18; using ld=long double; using ull=uint64_t;
using VI=vector<int>; using VVI=vector<VI>; using VL=vector<ll>; using VVL=vector<VL>;
using PL=pair<ll,ll>; using VP=vector<PL>; using WG=vector<vector<pair<int,ll>>>;



/// @file modint.hpp
/// @brief ModInt
template<ll MOD>
struct ModInt{
    ModInt(ll x=0){ value=(x>=0?x%MOD:MOD-(-x)%MOD); }

    ModInt operator-() const { return ModInt(-value); }
    ModInt operator+() const { return ModInt(*this); }
    ModInt& operator+=(const ModInt& other) {
        value+=other.value;
        if(value>=MOD) value-=MOD;
        return*this;
    }
    ModInt& operator-=(const ModInt& other) {
        value+=MOD-other.value;
        if(value>=MOD) value-=MOD;
        return*this;
    }
    ModInt& operator*=(const ModInt other) {
        value=value*other.value%MOD;
        return*this;
    }
    ModInt& operator/=(ModInt other) {
        (*this)*=other.inv();
        return*this;
    }
    ModInt operator+(const ModInt& other) const { return ModInt(*this)+=other; }
    ModInt operator-(const ModInt& other) const { return ModInt(*this)-=other; }
    ModInt operator*(const ModInt& other) const { return ModInt(*this)*=other; }
    ModInt operator/(const ModInt& other) const { return ModInt(*this)/=other; }
    bool operator==(const ModInt& other) const { return value==other.value; }
    bool operator!=(const ModInt& other) const { return value!=other.value; }
    friend ostream& operator<<(ostream& os, const ModInt& x) { return os<<x.value; }
    friend istream& operator>>(istream& is, ModInt& x) {
        ll v;
        is>>v;
        x=ModInt<MOD>(v);
        return is;
    }

    ModInt pow(ll x) const { 
        ModInt ret(1),mul(value);
        while(x) {
            if(x&1) ret*=mul;
            mul*=mul;
            x>>=1;
        }
        return ret;
    }
    ModInt inv() const { return pow(MOD-2); }
    ll val() {return value; }
    static constexpr ll get_mod() { return MOD; }

private:
    ll value;
};

using Mod998=ModInt<998244353>;
using Mod107=ModInt<1000000007>;
using VM=vector<Mod998>;

using mint=Mod998;

/// @brief 二項係数・階乗計算
template<typename T>
struct Combinatorics{
    Combinatorics()=default;

    /// @brief 二項係数の前計算
    /// @note O(N)
    Combinatorics(int n){
        fac=vector<T>(n+1);
        finv=vector<T>(n+1);
        fac[0]=1;
        for(int i=1; i<=n; i++) fac[i]=fac[i-1]*i;
        finv[n]=fac[n].inv();
        for(int i=n; i>=1; i--) finv[i-1]=finv[i]*i;
    }

    /// @brief nCr を返す
    /// @note n < 0, r < 0, n < r のときは 0 を返す
    T comb(ll n, ll r){
        if(n<0||r<0||n-r<0) return 0;
        return fac[n]*finv[r]*finv[n-r];
    }

    /// @brief nPr を返す
    /// @note n < 0, r < 0, n < r のときは 0 を返す
    T perm(ll n, ll r){
        if(n<0||r<0||n-r<0) return 0;
        return fac[n]*finv[n-r];
    }

    /// @brief n! を返す
    T factrial(int n){ return fac[n]; }

    /// @brief (n!)^-1 を返す
    T factinv(int n){ return finv[n]; }

    /// @brief nCr を返す
    T operator()(ll n, ll r){ return comb(n,r); }

private:
    vector<T> fac,finv;
};


/// @brief マージソート木
template<typename T>
struct MergeSortTree{
    MergeSortTree()=default;

    /// @brief 配列 v からマージソート木を構築する
    MergeSortTree(const vector<T>& v){
        n=v.size();
        mx=*max_element(ALL(v)),mn=*min_element(ALL(v));
        dat=vector<vector<T>>(n<<1);
        REP(i,n) dat[n+i]={v[i]};
        for(int i=n-1; i>0; i--){
            merge(ALL(dat[i<<1]),ALL(dat[i<<1|1]),back_inserter(dat[i]));
        }
    }

    /// @brief 区間 [l, r) に存在する val 未満の値の個数を返す
    /// @note O(log(N)^2)
    int count_lt(int l, int r, T val){
        l+=n,r+=n;
        int ret=0;
        while(l<r){
            if(l&1) ret+=LB(dat[l],val),l++;
            if(r&1) --r,ret+=LB(dat[r],val);
            l>>=1,r>>=1;
        }
        return ret;
    }

    /// @brief 区間 [l, r) に存在する val 以下の値の個数を返す
    /// @note O(log(N)^2)
    int count_le(int l, int r, T val){
        return count_lt(l,r,val+1);
    }

    /// @brief 区間 [l, r) に存在する val より大きい値の個数を返す
    /// @note O(log(N)^2)
    int count_gt(int l, int r, T val){
        return r-l-count_le(l,r,val);
    }

    /// @brief 区間 [l, r) に存在する val 以上の値の個数を返す
    /// @note O(log(N)^2)
    int count_ge(int l, int r, T val){
        return r-l-count_lt(l,r,val);
    }

    /// @brief 区間 [l, r) の小さい方から k(1-indexed) 番目の値を返す
    /// @note O(log(N)^3)
    T kth(int l, int r, int k){
        T lo=mn-1,hi=mx+1;
        while(hi-lo>1){
            T mid=(hi+lo)/2;
            if(count_lt(l,r,mid)>=k) hi=mid;
            else lo=mid;
        }
        return lo;
    }

private:
    int n;
    vector<vector<T>> dat;
    T mx,mn;
};

//----------------------------------------------------------

int main() {
    int N; cin>>N;
    VI P(N); REP(i,N) cin>>P[i];

    Combinatorics<mint> com(4e5);
    MergeSortTree<int> mst(P);

    mint ans=0;
    REP(i,N) {
        int p=P[i];
        int A=mst.count_gt(0,i,p),B=mst.count_lt(0,i,p),C=mst.count_lt(i,N,p),D=mst.count_gt(i,N,p);
        ans+=com(A+C,A)*com(B+D,B);
    }

    cout<<ans<<endl;
}
0