1420D - Rescue Nibel

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
using vi = vector<ll>;
ll const MOD = 998244353;
int const NMAX = 3e5 + 11;
ll fact[NMAX];
void precompute()
{
    fact[0] = 1;
    for (int i = 1; i < NMAX; i++)
        fact[i] = fact[i - 1] * i % MOD;
}
ll gcd(ll a, ll b, ll &x, ll &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    ll x1, y1;
    ll d = gcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}
ll inv(int a)
{
    ll x, y;
    ll g = gcd(a, MOD, x, y);
    if (g != 1)
    {
        return 0;
    }
    else
    {
        return (x % MOD + MOD) % MOD;
    }
}
ll C(ll n, ll k)
{
    return fact[n] * inv(fact[k] * fact[n - k] % MOD) % MOD;
}
int main(void)
{
    ll n, k;
    cin >> n >> k;
    vector<ii> events(2 * n);
    for (int i = 0; i < n; ++i)
    {
        ll l, r;
        cin >> l >> r;
        events[2 * i] = {l, -1};
        events[2 * i + 1] = {r, +1};
    }
    sort(events.begin(), events.end());
    precompute();
    ll turned = 0;
    ll ans = 0;
    for (auto [t, sign] : events)
    {
        sign *= -1;
        if (sign == +1 and turned + sign >= k)
        {
            ans += (C(turned, k - 1)) % MOD;
        }
        turned += sign;
    }
    cout << ans % MOD << endl;
    return 0;
}