結果

問題 No.2327 Inversion Sum
ユーザー momoyuumomoyuu
提出日時 2023-05-28 16:59:14
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 3,237 bytes
コンパイル時間 2,559 ms
コンパイル使用メモリ 249,732 KB
実行使用メモリ 7,660 KB
最終ジャッジ日時 2023-08-27 14:20:37
合計ジャッジ時間 4,318 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
7,544 KB
testcase_01 AC 36 ms
7,660 KB
testcase_02 AC 28 ms
7,548 KB
testcase_03 AC 9 ms
7,080 KB
testcase_04 AC 38 ms
7,080 KB
testcase_05 AC 9 ms
5,904 KB
testcase_06 AC 28 ms
7,268 KB
testcase_07 AC 17 ms
5,948 KB
testcase_08 AC 9 ms
5,772 KB
testcase_09 AC 34 ms
7,348 KB
testcase_10 AC 13 ms
5,752 KB
testcase_11 AC 6 ms
6,476 KB
testcase_12 AC 6 ms
5,904 KB
testcase_13 AC 5 ms
5,664 KB
testcase_14 AC 25 ms
6,172 KB
testcase_15 AC 39 ms
7,080 KB
testcase_16 AC 17 ms
7,008 KB
testcase_17 AC 7 ms
6,800 KB
testcase_18 AC 8 ms
5,712 KB
testcase_19 AC 14 ms
7,136 KB
testcase_20 AC 4 ms
5,404 KB
testcase_21 AC 4 ms
5,460 KB
testcase_22 AC 4 ms
5,404 KB
testcase_23 AC 5 ms
5,424 KB
testcase_24 AC 4 ms
5,408 KB
testcase_25 AC 5 ms
5,464 KB
testcase_26 AC 4 ms
5,400 KB
testcase_27 AC 4 ms
5,412 KB
testcase_28 AC 4 ms
5,556 KB
testcase_29 AC 4 ms
5,420 KB
testcase_30 AC 4 ms
5,408 KB
testcase_31 AC 5 ms
5,428 KB
testcase_32 AC 4 ms
5,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


const ll mod = 998'244'353;
//const ll mod = 1'000'000'007;
//const ll mod = 67'280'421'310'721;
struct mint{
    long long x;
    mint(long long x=0):x((x%mod+mod)%mod){}
    mint operator-() const{
        return mint(-x);
    }
    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(long long n) const {
        assert(0 <= n);
        mint a = *this, r = 1;
        while (n) {
            if (n & 1) r *= a;
            a *= a;
            n >>= 1;
        }
        return r;
    }
    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;
    }
    friend ostream& operator<<(ostream& os, const mint& m){
        os << m.x;
        return os;
    }
    bool operator==(const mint& a) const {
        return x == a.x;
    }
    bool operator<(const mint& a) const{
        return x < a.x;
    }
};

template< typename T >
struct BIT{
    int n;
    vector<T> data;
    BIT(int n):n(n),data(n+1,0){}

    void add(int i,T x){
        i++;
        while(i<=n){
            data[i] += x;
            i += i & (-i);
        }
    }

    //sum of [l,r)
    T sum(int l,int r){
        return sum(r-1)-sum(l-1);
    }

    T sum(int i){
        i++;
        T now = 0;
        while(i>0){
            now += data[i];
            i -= i & (-i);
        }
        return now;
    }
};

mint fac[2<<17];
int main(){
    fac[0] = 1;
    for(int i = 1;i<2<<17;i++) fac[i] = fac[i-1] * mint(i);
    mint sum = 0;
    int n,m;
    cin>>n>>m;
    vector<int> now(n,-1);
    vector<int> use(n,-1);
    for(int i = 0;i<m;i++){
        int p,k;
        cin>>p>>k;
        p--;k--;
        use[p] = k;
        now[k] = p;
    }
    BIT<mint> bit(n);
    int can = n - m;
    for(int i = 0;i<n;i++){
        if(now[i] == -1) continue;
        sum += bit.sum(now[i],n);
        bit.add(now[i],1);
    }
    sum *= fac[can];
    if(can==0){
        cout<<sum<<endl;
        return 0;
    }
    BIT<mint> b(n);
    mint tmp = 0;
    mint ans = 0;
    for(int i= 0;i<n;i++) if(now[i]==-1) b.add(i,1);
    for(int i = 0;i<n;i++) {
        if(use[i]==-1) ans += tmp;
        else tmp += b.sum(0,use[i]);
    }
    ans *= fac[can-1];
    ans += sum;
    tmp = 0;
    mint nn = 0;
    for(int i = n - 1;i>=0;i--){
        if(use[i]==-1) nn += tmp;
        else tmp += b.sum(use[i],n);
    }
    ans += nn * fac[can-1];
    if(can>=2){
        tmp = fac[can] * fac[2].inv() * fac[can-2].inv();
        tmp *= tmp;
        tmp *= fac[can-2];
        ans += tmp;
    }
    cout<<ans<<endl;

}
0