結果

問題 No.2197 Same Dish
ユーザー HIcoderHIcoder
提出日時 2023-07-07 11:22:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 76 ms / 2,000 ms
コード長 2,908 bytes
コンパイル時間 1,390 ms
コンパイル使用メモリ 110,272 KB
実行使用メモリ 6,280 KB
最終ジャッジ日時 2023-09-28 11:44:14
合計ジャッジ時間 3,310 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,620 KB
testcase_01 AC 3 ms
4,688 KB
testcase_02 AC 3 ms
4,616 KB
testcase_03 AC 3 ms
4,548 KB
testcase_04 AC 3 ms
4,728 KB
testcase_05 AC 3 ms
4,572 KB
testcase_06 AC 3 ms
4,724 KB
testcase_07 AC 3 ms
4,572 KB
testcase_08 AC 3 ms
4,572 KB
testcase_09 AC 3 ms
4,724 KB
testcase_10 AC 3 ms
4,892 KB
testcase_11 AC 3 ms
4,548 KB
testcase_12 AC 3 ms
4,632 KB
testcase_13 AC 13 ms
4,836 KB
testcase_14 AC 66 ms
6,200 KB
testcase_15 AC 74 ms
6,216 KB
testcase_16 AC 50 ms
5,780 KB
testcase_17 AC 45 ms
5,508 KB
testcase_18 AC 75 ms
6,232 KB
testcase_19 AC 75 ms
6,104 KB
testcase_20 AC 75 ms
6,128 KB
testcase_21 AC 76 ms
6,280 KB
testcase_22 AC 71 ms
6,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<cmath>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<int,int> P;
typedef pair<int,P> PP;
const ll MOD=998244353;
const double PI=acos(-1);



//xのy乗
ll mod_pow(ll x,ll y,ll mod){
    ll power=x;
    ll ret = 1;
    while(y>0){
        if(y&1){
           ret = ret*power%mod;
        }
        power = power*power%mod;
        y = y>>1;
    }
    return ret;

}


/*
struct combination{
    typedef long long ll;
    std::vector<ll> fact,ifact;
    combination(ll n): fact(n+1),ifact(n+1){
        fact[0]=1;//0!=1通り
        for(ll i=1;i<=n;i++) fact[i] = fact[i-1]*i%MOD;
        //ifact[n] = fact[n].inv();
        
        // n!の逆元がifact[n]
        ifact[n] = mod_pow(fact[n],MOD-2,MOD);// n!を(mod-2)乗した後でmodであまりをとった
        for(ll i=n;i>=1;i--) ifact[i-1] = ifact[i]*i%MOD;
    }

    ll operator()(ll n,ll k){
        if(k<0 || k>n) return 0;
        // n!/( k!*(n-k)! )
        return (fact[n]*ifact[k])%MOD * ifact[n-k]%MOD;
    }

};



combination comb(2*100000);

*/



template<class Type> struct binary_indexed_tree{
    int N;
    vector<Type> bit;
    
  

    binary_indexed_tree(int n):N(n+1){
        bit = vector<Type>(n+1,0);
        N=n;
    }



    void add(int x,Type a){
        x++;//1から始めるための補正
        //for(int i=x; i<=N; i+=(i&-i)) bit[i] = addition(bit[i],a);
        while(x<=N){
            //bit[x] = addition(bit[x],a);
            bit[x] =bit[x]+a;
            x += x&-x;
        }
    }

    
    Type sum(int x){
        x++;//1から始まることに注意
        Type ret=0;
        //for(int i=x; i>0; i-=(i&-i)) ret = addition(ret,bit[i]);
        while(x>0){
            ret = ret+bit[x];
            x -= x&-x;
        }
        
        return ret;
    }

    //[l,r]の範囲
    // rはN-1以下
    Type get(int l,int r){
        if(r>N) return 0;//配列の外へのアクセス
        if(l>r) return 0;//本来は l<=r となるのでおかしい
        if(l==0) return sum(r);//[0,r]//ここでoutなわけか
        else return (sum(r) - sum(l-1));
    }
};




int main(){
    int N,K;
    cin>>N>>K;
    vector<int> L(N),R(N);
    vector<P> range;
    for(int i=0;i<N;i++){
        cin>>L[i]>>R[i];
        range.emplace_back(L[i],i);
        
    }

    sort(range.begin(),range.end());

   
    int n=range.size();

    ll res=1;

    ll tot=mod_pow(K,N,MOD);//K^N
    
   

    binary_indexed_tree<ll> bit(200000+5);

    for(int i=0;i<N;i++){
        auto [x,id]=range[i];
        
        int m=bit.get(L[id]+1,200000+1);//(L[id])
        res*=max(0LL,1LL*(K-m));
        res%=MOD;
        bit.add(R[id],1);
    }
    


    ll ans=tot-res;
    ans+=MOD;
    ans%=MOD;
    cout<<ans<<endl;
}
0