CodeForces 689E Mike and Geometry Problem

时间:2023-03-09 09:40:27
CodeForces 689E Mike and Geometry Problem

离散化,树状数组,组合数学。

这题的大致思路和$HDU$ $5700$一样。都是求区间交的问题。可以用树状数组维护一下。

这题的话只要计算每一个$i$被统计了几次,假设第$i$点被统计了$ans[i]$次,累加和就是答案。

$ans[i]$就是看$i$点之后有多少个区间右端点,假设有$m$个,那么$ans[i]$就等于$m$个里面选$k$个的方案数。

因为数据中$L$,$R$范围较大,所以需要先离散化,计算离散化之后的情况,然后再统计离散化之前的情况。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c=getchar(); x=;
while(!isdigit(c)) c=getchar();
while(isdigit(c)) {x=x*+c-''; c=getchar();}
} LL mod=1e9+;
const int maxn=;
int n,k;
LL f[maxn],a[maxn];
struct X { int L,R; }s[maxn];
int c[maxn],b[maxn],sz;
LL ans[maxn]; int lowbit(int x){return x&(-x);}
int sum(int x)
{
int res=;
while(x>) res=res+c[x],x=x-lowbit(x);
return res;
}
void update(int x,int v)
{
while(x<=) c[x]=c[x]+v,x=x+lowbit(x);
} LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
if(a==&&b==) return -;
if(b==){x=;y=;return a;}
LL d=extend_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
} LL mod_reverse(LL a,LL n)
{
LL x,y;
LL d=extend_gcd(a,n,x,y);
if(d==) return (x%n+n)%n;
else return -;
} int get(int x)
{
int L=,R=sz-,pos=;
while(L<=R)
{
int mid=(L+R)/;
if(b[mid]<x) L=mid+;
else if(b[mid]==x) pos=mid,R=mid-;
else R=mid-;
}
return pos+;
} bool cmp(X a,X b) { return a.L<b.L; } int main()
{
scanf("%d%d",&n,&k);
f[]=; for(int i=;i<=;i++) f[i]=i*f[i-]%mod;
for(int i=k;i<=;i++)
{
LL fz=f[i]%mod,fm=f[k]*f[i-k]%mod;
LL ni=mod_reverse(fm,mod);
a[i]=fz*ni%mod;
} for(int i=;i<=n;i++)
{
scanf("%d%d",&s[i].L,&s[i].R);
b[sz++]=s[i].L, b[sz++]=s[i].R;
b[sz++]=s[i].L-; b[sz++]=s[i].L+;
b[sz++]=s[i].R-; b[sz++]=s[i].R+;
} sort(b,b+sz);
sort(s+,s++n,cmp); int h=;
for(int i=;i<=sz;i++)
{
while(h<=n&&get(s[h].L)==i)
{
update(get(s[h].R),);
h++;
}
ans[i]=a[sum()-sum(i-)];
} LL Ans=;
for(int i=;i<sz;)
{
int pos=-;
for(int j=i;j<sz;j++)
{
if(b[j]>b[i]) { pos=j; break; }
}
if(pos==-) { Ans=(Ans+ans[i+])%mod; break; }
Ans=(Ans+(b[pos]-b[i])*ans[i+]%mod)%mod;
i=pos;
} printf("%lld\n",Ans);
return ;
}