链接:https://codeforces.ml/problemset/problem/1420/D
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 nn lamps with Spirit Tree's light. Sein knows the time of turning on and off for the ii-th lamp — lili and riri respectively. To open the door you have to choose kk 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 kk lamps to pick, Ori is interested: how many ways there are to pick such kk lamps that the door will open? It may happen that Sein may be wrong and there are no such kk lamps. The answer might be large, so print it modulo 998244353998244353.
Input
First line contains two integers nn and kk (1≤n≤3?1051≤n≤3?105, 1≤k≤n1≤k≤n) — total number of lamps and the number of lamps that must be turned on simultaneously.
Next nn lines contain two integers lili ans riri (1≤li≤ri≤1091≤li≤ri≤109) — period of time when ii-th lamp is turned on.
Output
Print one integer — the answer to the task modulo 998244353998244353.
Examples
input
7 3 1 7 3 8 4 5 6 7 1 3 5 10 8 9
output
9
input
3 1 1 1 2 2 3 3
output
3
input
3 2 1 1 2 2 3 3
output
0
input
3 3 1 3 2 3 3 3
output
1
input
5 2 1 3 2 4 3 5 4 6 5 7
output
7
Note
In first test case there are nine sets of kk lamps: (1,2,3)(1,2,3), (1,2,4)(1,2,4), (1,2,5)(1,2,5), (1,2,6)(1,2,6), (1,3,6)(1,3,6), (1,4,6)(1,4,6), (2,3,6)(2,3,6), (2,4,6)(2,4,6), (2,6,7)(2,6,7).
In second test case k=1k=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 33, so the answer is 1.
In fifth test case there are seven sets of kk lamps: (1,2)(1,2), (1,3)(1,3), (2,3)(2,3), (2,4)(2,4), (3,4)(3,4), (3,5)(3,5), (4,5)(4,5).
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int maxn=2e6+10;
const int inf=0x3f3f3f3f;
ll mod=998244353;
ll s;
ll b[maxn];
struct node{ll x,c;
}a[maxn];
bool cmp(node g,node h)
{if(g.x!=h.x)return g.x<h.x;elsereturn g.c<h.c;
}
ll q_sort(ll a,ll b)
{ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod;b/=2;}return ans%mod;
}
ll c(ll n,ll m)
{if(m>n) return 0;return (b[n]*q_sort(b[m], mod - 2) % mod * q_sort(b[n - m], mod- 2) % mod)%mod ;
}
int main()
{ll n,k;cin>>n>>k;b[0]=1;for(int i=1;i<=maxn;i++){b[i]=b[i-1]*i%mod;}int ss=0;for(int i=1;i<=n;i++){int l,r;cin>>l>>r;ss++;a[ss].x=l;a[ss].c=1;ss++;a[ss].x=r+1;a[ss].c=-1;}sort(a+1,a+1+ss,cmp);s=0;ll ans=0;for(int i=1;i<=ss;i++){if(a[i].c==1)ans+=c(s,k-1);s+=a[i].c;ans%=mod;}cout<<ans<<endl;
}