結果

問題 No.2197 Same Dish
ユーザー ぷら
提出日時 2023-01-29 18:59:24
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,574 bytes
コンパイル時間 4,276 ms
コンパイル使用メモリ 202,880 KB
最終ジャッジ日時 2025-02-10 07:40:19
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 7 WA * 13
権限があれば一括ダウンロードができます

ソースコード

diff #

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

constexpr int mod = 998244353;

long long modpow(long long a,long long b) {
    long long ans = 1;
    while(b) {
        if(b & 1) {
            (ans *= a) %= mod;
        }
        (a *= a) %= mod;
        b /= 2;
    }
    return ans;
}

template <typename T>
struct BinaryIndexedTree {
    int N;
    vector<T>bit;
    BinaryIndexedTree(){}
    BinaryIndexedTree(int x) {
        N = x;
        bit.resize(x+1);
    }
    void add(int x,T y) {
        x++;
        while(x <= N) {
            bit[x] += y;
            x += x&-x;
        }
    }
    T sum(int x) {
        x++;
        T res = 0;
        while(x) {
            res += bit[x];
            x -= x&-x;
        }
        return res;
    }
    inline T sum(int l, int r) {//[l,r)
        return sum(r-1)-sum(l-1);
    }
    inline T operator[](int k) {
        return sum(k)-sum(k-1);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,K;
    cin >> N >> K;
    vector<int>L(N),R(N);
    for(int i = 0; i < N; i++) {
        cin >> L[i] >> R[i];
    }
    long long ans = modpow(K,N);
    vector<pair<int,int>>tmp;
    for(int i = 0; i < N; i++) {
        tmp.push_back({R[i],L[i]});
    }
    sort(tmp.begin(),tmp.end());
    long long res = 1;
    BinaryIndexedTree<int>bit(200200);
    for(int i = 0; i < N; i++) {
        int cnt = bit.sum(tmp[i].second,tmp[i].first);
        res *= max(0,K-cnt);
        res %= mod;
        bit.add(tmp[i].first-1,1);
    }
    ans += mod-res;
    ans %= mod;
    cout << ans << endl;
}
0