codeforces689e(树状数组)

题目链接:点这里!!!


题意:给你n(n<=200000)条线段,叫你求从这n条线段中取出k(k<=n)条线段,取这k条线段的并,问所有情况线段长度相加的和为多少?(对答案mod1e9+7)


题解:

我们直接算一段区间对答案的贡献,然后加起来就可以了!!当然要注意怎么处理,我是端点单独拿出来算,然后再算区间。这里有个check点,注意最后一个端点,确保所有线段都加进来了!!!有树状数组随便搞就是了!!


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
#pragma comment(linker, "/STACK:102400000000,102400000000")
typedef  long long LL;
const LL  MOD = 1000000007;
const int N = 2e5+15;
const int maxn = 1e6+15;
const int letter = 130;
const int INF = 1e9;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,k;
int b[N*2];
int f[N*2];
LL F[N],Fn[N];
inline LL cc(LL a,LL k){
    LL ans=1;
    while(k){
        if(k&1ll) ans=ans*a%MOD;
        a=a*a%MOD;
        k/=2ll;
    }
    return ans%MOD;
}
inline void init(){
    F[1]=F[0]=Fn[0]=Fn[1]=1;
    for(LL i =2;i < N;i++){
        F[i] = F[i-1]*1ll*i % (MOD);
        Fn[i]=cc(F[i],MOD-2);
    }
}
LL c(LL n,LL m){
    if(n<m)return 0ll;
    return 1ll*F[n]*Fn[m]%MOD*Fn[n-m]%MOD;
}
struct node{
    int l,r;
    bool operator <(const node& p)const{
        return l<p.l;
    }
}a[N];
int lowbit(int x){return x&(-x);}
void update(int x,int val){
    while(x<2*N){f[x]+=val,x+=lowbit(x);}
}
int get(int x){
    int sum=0;
    while(x>0){sum+=f[x],x-=lowbit(x);}
    return sum;
}
int main(){
    init();
    scanf("%d%d",&n,&k);
    int cnt=0;
    for(int i=1;i<=n;i++)
        scanf("%d%d",&a[i].l,&a[i].r),b[++cnt]=a[i].l,b[++cnt]=a[i].r;
    sort(a+1,a+n+1);
    sort(b+1,b+cnt+1);
    cnt=unique(b+1,b+cnt+1)-(b+1);
    int num=0;
    LL sum=0;
    for(int i=1;i<cnt;i++){
        while(num<n&&a[num+1].l<=b[i]){
            int vs=lower_bound(b+1,b+cnt+1,a[++num].r)-b;
            update(vs,1);
        }
        int px=num-get(i-1);
        sum+=1ll*c(px,k)%MOD,sum%=MOD;
        int l=b[i]+1,r=b[i+1]-1;
        if(l>r) continue;
        px=num-get(i);
        sum+=1ll*c(px,k)*1ll*(r-l+1)%MOD,sum%=MOD;

    }
    while(num<n&&a[num+1].l<=b[cnt]){
            int vs=lower_bound(b+1,b+cnt+1,a[++num].r)-b;
            update(vs,1);
    }
    int px=num-get(cnt-1);
    sum+=1ll*c(px,k)%MOD,sum%=MOD;
    printf("%I64d\n",sum);
    return 0;
}
/*
5 2
1 1000
2 10
10 1010
4 70
8 100

3 1
1 1
2 2
3 3

*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值