Codeforces1420 D. Rescue Nibel!(區間,組合數)

tomjobs發表於2020-09-25

Ori and Sein have overcome many difficult challenges. They finally lit the Shrouded Lantern and found Gumon Seal, the key to the Forlorn Ruins. When they tried to open the door to the ruins… nothing happened.

Ori was very surprised, but Sein gave the explanation quickly: clever Gumon decided to make an additional defence for the door.

There are ? lamps with Spirit Tree’s light. Sein knows the time of turning on and off for the ?-th lamp — ?? and ?? respectively. To open the door you have to choose ? lamps in such a way that there will be a moment of time when they all will be turned on.

While Sein decides which of the ? lamps to pick, Ori is interested: how many ways there are to pick such ? lamps that the door will open? It may happen that Sein may be wrong and there are no such ? lamps. The answer might be large, so print it modulo 998244353.

Input
First line contains two integers ? and ? (1≤?≤3⋅105, 1≤?≤?) — total number of lamps and the number of lamps that must be turned on simultaneously.

Next ? lines contain two integers ?? ans ?? (1≤??≤??≤109) — period of time when ?-th lamp is turned on.

Output
Print one integer — the answer to the task modulo 998244353.

Examples
inputCopy
7 3
1 7
3 8
4 5
6 7
1 3
5 10
8 9
outputCopy
9
inputCopy
3 1
1 1
2 2
3 3
outputCopy
3
inputCopy
3 2
1 1
2 2
3 3
outputCopy
0
inputCopy
3 3
1 3
2 3
3 3
outputCopy
1
inputCopy
5 2
1 3
2 4
3 5
4 6
5 7
outputCopy
7
Note
In first test case there are nine sets of ? lamps: (1,2,3), (1,2,4), (1,2,5), (1,2,6), (1,3,6), (1,4,6), (2,3,6), (2,4,6), (2,6,7).

In second test case ?=1, so the answer is 3.

In third test case there are no such pairs of lamps.

In forth test case all lamps are turned on in a time 3, so the answer is 1.

In fifth test case there are seven sets of ? lamps: (1,2), (1,3), (2,3), (2,4), (3,4), (3,5), (4,5).

題意:
n個區間段。求多少種方式取 k k k個區間段使得其有交集。

思路:
先按左端點排序,然後對於對於每個區間段,尋找之前有多少個區間右端點值大於當前區間段左端點值。意思是在選擇當前區間段的前提下,有多少種方式在前面找k-1個區間與當前區間有交集。

然後組合數搞一下沒了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;

typedef long long ll;
const int maxn = 3e5 + 7;
const int mod = 998244353;

struct Node {
    int x,y;
}a[maxn];

int n,k;
int c[maxn * 5];
map<int,int>mp;
vector<int>vec;

int cmp(Node a,Node b) {
    if(a.x == b.x) return a.y < b.y;
    return a.x < b.x;
}

void add(int x,int v) {
    while(x <= n * 5) {
        c[x] += v;
        x += x & -x;
    }
}

int sum(int x) {
    int res = 0;
    while(x) {
        res += c[x];
        x -= x & -x;
    }
    return res;
}

ll fac[maxn],inv[maxn];

ll qpow(ll a,ll b)
{
    ll res = 1;
    while(b)
    {
        if(b & 1)
        {
            res = (res * a) % mod;
        }
        a = (a * a) % mod;
        b = b >> 1;
    }
    return res % mod;
}

ll C(ll n,ll m)
{
    if(m > n || m < 0)
        return 0;
    return fac[n] * ((inv[n - m] * inv[m]) % mod) % mod;
}

void init()
{
    fac[0] = 1;
    inv[0] = 1;
    for(int i = 1;i <= maxn - 2;i++)
    {
        fac[i] = (fac[i - 1] * i) % mod;
        inv[i] = qpow(fac[i],mod - 2);
    }
}

int main() {
    init();
    scanf("%d%d",&n,&k);
    mp.clear();
    vec.clear();
    for(int i = 1;i <= n * 5;i++) c[i] = 0;
    for(int i = 1;i <= n;i++) {
        scanf("%d%d",&a[i].x,&a[i].y);
        vec.push_back(a[i].x);
        vec.push_back(a[i].y);
        vec.push_back(a[i].x - 1);
        vec.push_back(a[i].y - 1);
    }
    sort(a + 1,a + 1 + n,cmp);
    sort(vec.begin(),vec.end());
    int cnt = 0;
    
    for(int i = 0;i < vec.size();i++) {
        if(!mp[vec[i]]) mp[vec[i]] = ++cnt;
    }
    
    ll ans = 0;
    for(int i = 1;i <= n;i++) {
        int num = sum(n * 5) - sum(mp[a[i].x - 1]);
        if(num >= k - 1) {
            ans += C(num,k - 1);
            ans %= mod;
        }
        add(mp[a[i].y],1);
    }
    printf("%lld\n",ans);
    
    return 0;
}

相關文章