Test instructions: give you multiple intervals and select at least k intervals in a given interval to calculate the interval size after they intersect. And then figure out all the things and.
Analysis: Stacking all the intervals, then calculating the number of tk>=k of the number of sub-intervals of C (tk,k), and then summing.
Points of knowledge involved: multiplication inverse + discretization + (segment tree)
Code 1: (Simple and fast and requires little space and short code)
/************************************************author:D arktongcreated time:2016/7/8 9:12:58File name:e_ 3.cpp*************************************************/#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<cmath>#include<cstdlib>#include<ctime>#defineINF 0x3f3f3f3f#defineESP 1e-9typedefLong LongLL;using namespacestd;Const intMAXN =200000+Ten;Const intMOD = 1e9+7;intL[MAXN], R[MAXN], sum[maxn*2], rec[maxn*2]; LL FAC[MAXN];intQuick (LL x,intN) {LL ans=1; if(x==0)return 0; while(n) {if(n&1) ans= (ans*x)%MOD; X= (x*x)%MOD; N>>=1; } returnans;}voidinit () {fac[0]=1; for(intI=1; i<maxn;++i) fac[i]= (fac[i-1]*i)%MOD;}intINV (intN) { returnQuick (n, mod-2);} LL Cnm (intNintm) { returnfac[n]* (INV (fac[m]*fac[n-m]%mod))%MOD;}intMain () {//freopen ("In.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout);init (); intN, K, cnt=0; scanf ("%d%d", &n, &k); for(intI=0; i<n;++i) {scanf ("%d%d", &l[i], &R[i]); Rec[cnt++]=L[i]; Rec[cnt++]=++R[i]; } sort (REC, rec+CNT); CNT=unique (REC, rec+cnt)-Rec; for(intI=0; i<n;++i) {intT; T= Lower_bound (REC, rec+cnt, l[i])-rec; sum[t]++; T= Lower_bound (REC, rec+cnt, r[i])-rec; sum[t]--; } intTK = sum[0], ans=0; for(intI=1; i<cnt;++i) {if(tk>=k) ans = (ANS+CNM (tk, k) * (rec[i]-rec[i-1])%mod)%MOD; TK+=Sum[i]; } printf ("%d\n", ans); return 0;}
Code Listing 2: (Segment tree-complex and requires more space and code length)
/************************************************author:D arktongcreated TIME:2016/7/7 21:58:31File name:e _2.cpp*************************************************/#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<cmath>#include<cstdlib>#include<ctime>#defineINF 0x3f3f3f3f#defineESP 1e-9typedefLong LongLL;using namespacestd;Const intMAXN =200000+ -;Const intMOD = 1e9+7;structnode{intL, R, W, F;} A[MAXN* -];intN, K, L[MAXN], R[MAXN], tt[maxn*4];map<int,int>ID; LL FAC[MAXN];voidinit () {fac[0]=1; for(intI=1; i<maxn;++i) fac[i]= (fac[i-1]*i)%MOD;} ll Quick (ll X,intN) {LL ans=1; if(x==0)return 0; while(n) {if(n&1) ans= (ans*x)%MOD; X=x*x%MOD; N>>=1; } returnans;} ll Inv (ll x) {returnQuick (x, mod-2);} ll Cnm (ll N, ll m) {ll T= (FAC[N]*INV (fac[m]*fac[n-m]%mod))%MOD; returnt;}voidPushdown (intth) {a[th<<1].W + =A[TH].W; A[th<<1|1].w+=A[TH].W; A[TH].W=0; A[th<<1].f=a[th<<1|1].f=1; A[TH].F=0;}voidBuild (intThintLintR) {A[TH].L=m; A[TH].R=s; A[TH].F=a[th].w=0; if(L +1==R)return; intM= (L+R)/2; Build (Th<<1, L, M); Build (Th<<1|1, M, R);}voidUpdate (intThintLintR) { if(l<=a[th].l&&a[th].r<=R) {A[TH].W++; A[TH].F=1; return; } if(A[TH].F) pushdown (TH); intM= (A[TH].L+A[TH].R)/2; if(L<M&&M<=R) Update (th<<1, L, M); Else if(l<m) Update (th<<1, L, R); if(L<=m&&m<r) Update (th<<1|1, M, R); Else if(M<r) Update (th<<1|1, L, R);} LL Tra (intThintLintR) { if(L +1==R) {if(a[th].w>=k)returnCnm (A[TH].W, k) * (TT[A[TH].R]-TT[A[TH].L])%MOD; Else return 0; } if(A[TH].F) pushdown (TH); intm = (A[TH].L+A[TH].R)/2; LL ans=0; Ans+=tra (th<<1, L, M); Ans+=tra (th<<1|1, M, R); returnans%MOD;}intMain () {init (); scanf ("%d%d", &n, &k); intTL, TR, cnt=0; for(intI=0; i<n;++i) {scanf ("%d%d", &TL, &tr); L[i]=TL; r[i]=TR; Tt[cnt++]=TL; tt[cnt++]=tl+1; Tt[cnt++]=TR; tt[cnt++]=tr+1; } //LisanhuaSort (TT, tt+CNT); CNT= Unique (TT, TT+CNT)-tt; for(intI=0; i<cnt;++i) id[tt[i]]=i; Build (1,0, cnt-1); for(intI=0; i<n;++i) Update (1, Id[l[i]], id[r[i]+1]); cout<<tra (1,0, cnt-1) <<Endl; return 0;}
Codeforces 689e-mike and Geometry problem