結果

問題 No.2616 中央番目の中央値
ユーザー HaaHaa
提出日時 2024-01-26 22:18:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 253 ms / 2,000 ms
コード長 2,470 bytes
コンパイル時間 1,754 ms
コンパイル使用メモリ 176,868 KB
実行使用メモリ 23,948 KB
最終ジャッジ日時 2024-01-26 22:18:23
合計ジャッジ時間 7,335 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 4 ms
6,676 KB
testcase_14 AC 5 ms
6,676 KB
testcase_15 AC 7 ms
6,676 KB
testcase_16 AC 14 ms
6,676 KB
testcase_17 AC 19 ms
6,676 KB
testcase_18 AC 33 ms
6,676 KB
testcase_19 AC 69 ms
7,912 KB
testcase_20 AC 68 ms
8,296 KB
testcase_21 AC 143 ms
13,544 KB
testcase_22 AC 225 ms
23,524 KB
testcase_23 AC 221 ms
22,036 KB
testcase_24 AC 189 ms
23,948 KB
testcase_25 AC 213 ms
23,948 KB
testcase_26 AC 226 ms
22,036 KB
testcase_27 AC 222 ms
22,036 KB
testcase_28 AC 228 ms
22,036 KB
testcase_29 AC 227 ms
22,616 KB
testcase_30 AC 253 ms
20,816 KB
testcase_31 AC 232 ms
22,036 KB
testcase_32 AC 230 ms
20,816 KB
testcase_33 AC 228 ms
21,832 KB
testcase_34 AC 229 ms
23,948 KB
testcase_35 AC 250 ms
22,036 KB
testcase_36 AC 225 ms
20,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
template<typename T> bool chmax(T &x, const T &y){return (x<y)?(x=y,true):false;};
template<typename T> bool chmin(T &x, const T &y){return (x>y)?(x=y,true):false;};
constexpr ll MOD=998244353;
constexpr ll INF=2e18;

long long inv(int n){
    static vector<long long> a={1,1};
    if((int)a.size()<=n){
        for(int i=a.size();i<=n;i++)
            a.push_back(MOD-a[MOD%i]*(MOD/i)%MOD);
    }
    return a[n];
}
long long fact(int n){
    static vector<long long> a={1,1};
    if((int)a.size()<=n){
        for(int i=a.size();i<=n;i++)
            a.push_back(a.back()*i%MOD);
    }
    return a[n];
}
long long finv(int n){
    static vector<long long> a={1,1};
    if((int)a.size()<=n){
        for(int i=a.size();i<=n;i++)
            a.push_back(a.back()*inv(i)%MOD);
    }
    return a[n];
}
long long com(int n, int k){
    if(n<k||n<0||k<0) return 0;
    return fact(n)*finv(k)%MOD*finv(n-k)%MOD;
}

template<typename X>
struct Segtree{
    using F=function<X(X,X)>;
    int n; F f; X ex;
    vector<X> dat;
    Segtree(int n_, F f_, X ex_):f(f_), ex(ex_){
        n=1;
        while(n_>n){n*=2;}
        dat.assign(2*n,ex);
    }
    void set(int i, X x){
        dat[i+n-1]=x;
    }
    void build(){
        for(int k=n-2;k>=0;k--)
            dat[k]=f(dat[2*k+1],dat[2*k+2]);
    }
    void update(int i, X x){
        i+=n-1;
        dat[i]=x;
        while(i>0){
            i=(i-1)/2;
            dat[i]=f(dat[i*2+1],dat[i*2+2]);
        }
    }
    X query(int a, int b){
        return query_sub(a,b,0,0,n);
    }
    X query_sub(int a, int b, int k, int l, int r){
        if(r<=a||b<=l)
            return ex;
        else if(a<=l&&r<=b)
            return dat[k];
        else{
            X vl=query_sub(a,b,k*2+1,l,(l+r)/2);
            X vr=query_sub(a,b,k*2+2,(l+r)/2,r);
            return f(vl,vr);
        }
    }
};

int main(){
    int n; cin >> n;
    VI p(n); REP(i,n) cin >> p[i], p[i]--;
    Segtree<int> seg(n,[](int l, int r){return l+r;},0);
    ll ans=0;
    REP(i,n){
        int c=seg.query(0,p[i]);
        int a=i-c;
        int d=p[i]-c;
        int b=n-1-a-d-c;
        ans+=com(a+d,min(a,d))*com(b+c,min(b,c))%MOD;
        ans%=MOD;
        seg.update(p[i],1);
    }
    cout << ans << endl;
    return 0;
}
0