結果

問題 No.2616 中央番目の中央値
ユーザー FplusFplusFFplusFplusF
提出日時 2024-01-26 23:46:29
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 275 ms / 2,000 ms
コード長 2,859 bytes
コンパイル時間 3,340 ms
コンパイル使用メモリ 254,420 KB
実行使用メモリ 17,412 KB
最終ジャッジ日時 2024-01-26 23:46:38
合計ジャッジ時間 9,319 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
8,000 KB
testcase_01 AC 7 ms
8,004 KB
testcase_02 AC 8 ms
8,000 KB
testcase_03 AC 7 ms
8,000 KB
testcase_04 AC 8 ms
8,004 KB
testcase_05 AC 7 ms
8,004 KB
testcase_06 AC 7 ms
8,004 KB
testcase_07 AC 7 ms
8,004 KB
testcase_08 AC 8 ms
8,004 KB
testcase_09 AC 8 ms
8,004 KB
testcase_10 AC 8 ms
8,016 KB
testcase_11 AC 8 ms
8,020 KB
testcase_12 AC 8 ms
8,036 KB
testcase_13 AC 9 ms
8,100 KB
testcase_14 AC 10 ms
8,164 KB
testcase_15 AC 13 ms
8,320 KB
testcase_16 AC 18 ms
8,644 KB
testcase_17 AC 23 ms
8,996 KB
testcase_18 AC 35 ms
9,604 KB
testcase_19 AC 68 ms
11,204 KB
testcase_20 AC 67 ms
11,204 KB
testcase_21 AC 152 ms
14,244 KB
testcase_22 AC 256 ms
17,348 KB
testcase_23 AC 274 ms
17,412 KB
testcase_24 AC 167 ms
17,412 KB
testcase_25 AC 167 ms
17,412 KB
testcase_26 AC 261 ms
17,412 KB
testcase_27 AC 260 ms
17,412 KB
testcase_28 AC 268 ms
17,412 KB
testcase_29 AC 258 ms
17,412 KB
testcase_30 AC 250 ms
17,412 KB
testcase_31 AC 259 ms
17,412 KB
testcase_32 AC 256 ms
17,412 KB
testcase_33 AC 275 ms
17,412 KB
testcase_34 AC 247 ms
17,412 KB
testcase_35 AC 244 ms
17,412 KB
testcase_36 AC 254 ms
17,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll,ll>;
using ld=long double;
const ll INF=(1ll<<60);
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(v) v.begin(),v.end()
template<class T> void chmin(T &a,T b){
    if(a>b){
        a=b;
    }
}
template<class T> void chmax(T &a,T b){
    if(a<b){
        a=b;
    }
}
template<class T> struct fenwick_tree{
    vector<T> v;
    int n;
    fenwick_tree(int x){
        n=x;
        v.assign(n+1,0);
    }
    fenwick_tree(vector<T> &a){
        n=(int)a.size();
        v.assign(n+1,0);
        for(int i=0;i<n;i++) v[i+1]=a[i];
        for(int idx=1;idx<=n;idx++){
            if(n<idx+(idx&(-idx))) continue;
            v[idx+(idx&(-idx))]+=v[idx];
        }
    }
    void add(int i,T x){
        assert(0<=i&&i<n);
        i++;
        for(int idx=i;idx<=n;idx+=(idx&(-idx))){
            v[idx]+=x;
        }
    }
    T sum(int r){  //[0,r]
        r++;
        T ret=0;
        for(int idx=r;0<idx;idx-=(idx&(-idx))){
            ret+=v[idx];
        }
        return ret;
    }
    T sum(int l,int r){  //[l,r]
        assert(0<=l&&l<n);
        assert(0<=r&&r<n);
        assert(l<=r);
        if(l==0) return sum(r);
        return sum(r)-sum(l-1);
    }
    int lower_bound(T w){
        if(w<=0) return 0;
        int x=0;
        static int r=1;
        if(r==1){
            while((r<<1)<=n) r<<=1;
        }
        while((r<<1)<=n) r<<=1;
        for(int k=r;0<k;k>>=1){
            if(x+k<=n&&v[x+k]<w){
                w-=v[x+k];
                x+=k;
            }
        }
        return x;
    }
};
const ll mod=998244353;
ll modpow(ll a,ll b){
    if(mod==1) return 0;
    if(b==0) return 1;
    ll ret=1;
    a%=mod;
    while(0<b){
        if(b&1) ret=ret*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ret;
}
ll division(ll a,ll b){
    return (a*modpow(b,mod-2))%mod;
}
vector<ll> factorial;
ll nCr(ll n,ll r){
    if(n<0||r<0) return 0;
    if(n<r) return 0;
    if((ll)factorial.size()==0){
        ll mx=600001;  //mx(上限)は適宜変更
        factorial.resize(mx+1,0);
        ll x=1;
        factorial[0]=1;
        for(ll i=1;i<=mx;i++){
            x*=i;
            x%=mod;
            factorial[i]=x;
        }
    }
    return division(factorial[n],factorial[r]*factorial[n-r]);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    vector<ll> p(n);
    rep(i,n){
        cin >> p[i];
        p[i]--;
    }
    vector<ll> v(n,1);
    fenwick_tree<ll> f(n),b(v);
    ll ans=0;
    rep(i,n){
        b.add(p[i],-1);
        ll fmin=f.sum(p[i]),fmax=f.sum(p[i],n-1);
        ll bmin=b.sum(p[i]),bmax=b.sum(p[i],n-1);
        f.add(p[i],1);
        ans+=nCr(fmin+bmax,fmin)*nCr(fmax+bmin,fmax)%mod;
        ans%=mod;
    }
    cout << ans << '\n';
}
0