您的位置:首页 > 其它

Codeforces 689E Mike and Geometry Problem(离散化+懒标记)

2016-07-09 09:03 337 查看
题意:给出n个线段[li,ri],求任意k个线段所共同覆盖的点的总和。

解析:

对于每一条线段,区间[li,ri]加一,如果一个位置的值为m,那么最后的结果ans += C(m,k);

如果只是离散化端点的话,没法求得中间部分被覆盖的点,所以在离散化时保证相邻两点中间留出一个1,对于中间的点,求出当前的值*(相邻两点实际距离-1)即是结果。

[code]:

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

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
typedef long long LL;
const int maxn = 2e5+5;
const LL MOD = 1e9+7;

int mc[2*maxn],hah;
int n,m,L[maxn],R[maxn],C[4*maxn];
LL mul[maxn];

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

void init(){
mul[0] = 1;hah = 0;
for(int i = 1;i < maxn;i++) mul[i] = (i*mul[i-1])%MOD;
}

LL Comb(LL n,LL m,LL mod){ //C(n,m)%mod
if(n<m) return 0;
if(m==0||m==n) return 1;
LL res=(mul
*mod_pow((mul[m]*mul[n-m])%mod,mod-2,mod))%mod;
return res;
}

int main(){
int i,j,cas;
scanf("%d%d",&n,&m);
init();
for(i = 0;i < n;i++){
scanf("%d%d",&L[i],&R[i]);
mc[hah++] = L[i];
mc[hah++] = R[i];
}
sort(mc,mc+hah);
hah = unique(mc,mc+hah)-mc;

for(i = 0;i < n;i++){
int l,r;
l = lower_bound(mc,mc+hah,L[i])-mc;
r = lower_bound(mc,mc+hah,R[i])-mc;
l *= 2;r *= 2;
C[l]++;C[r+1]--;
}
LL ans = 0,sum = 0,tmp;
for(i = 0;i < 2*hah;i++){
sum += C[i];
tmp = Comb(sum,m,MOD);
if(i&1){
ans = (ans + tmp*(mc[(i+1)/2]-mc[(i-1)/2]-1))%MOD;
}else{
ans = (ans+tmp)%MOD;
}
}
printf("%I64d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: