CodeForces 689E Mike and Geometry Problem

#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-6;
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=0;
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) {x=x*10+c-‘0‘; c=getchar();}
}

LL mod=1e9+7;
const int maxn=1200010;
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=0;
    while(x>0) res=res+c[x],x=x-lowbit(x);
    return res;
}
void update(int x,int v)
{
    while(x<=1200000) c[x]=c[x]+v,x=x+lowbit(x);
}

LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
    if(a==0&&b==0) return -1;
    if(b==0){x=1;y=0;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==1) return (x%n+n)%n;
    else return -1;
}

int get(int x)
{
    int L=0,R=sz-1,pos=0;
    while(L<=R)
    {
        int mid=(L+R)/2;
        if(b[mid]<x) L=mid+1;
        else if(b[mid]==x) pos=mid,R=mid-1;
        else R=mid-1;
    }
    return pos+1;
}

bool cmp(X a,X b) { return a.L<b.L; }

int main()
{
    scanf("%d%d",&n,&k);
    f[0]=1; for(int i=1;i<=400000;i++) f[i]=i*f[i-1]%mod;
    for(int i=k;i<=400000;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=1;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-1; b[sz++]=s[i].L+1;
        b[sz++]=s[i].R-1; b[sz++]=s[i].R+1;
    }

    sort(b,b+sz);
    sort(s+1,s+1+n,cmp);

    int h=1;
    for(int i=1;i<=sz;i++)
    {
        while(h<=n&&get(s[h].L)==i)
        {
            update(get(s[h].R),1);
            h++;
        }
        ans[i]=a[sum(1200000)-sum(i-1)];
    }

    LL Ans=0;
    for(int i=0;i<sz;)
    {
        int pos=-1;
        for(int j=i;j<sz;j++)
        {
            if(b[j]>b[i]) { pos=j; break; }
        }
        if(pos==-1) { Ans=(Ans+ans[i+1])%mod; break; }
        Ans=(Ans+(b[pos]-b[i])*ans[i+1]%mod)%mod;
        i=pos;
    }

    printf("%lld\n",Ans);
    return 0;
}
时间: 2024-10-18 16:18:45

CodeForces 689E Mike and Geometry Problem的相关文章

codeforces 798C Mike and gcd problem

C.Mike and gcd problem Mike has a sequence A?=?[a1,?a2,?...,?an] of length n. He considers the sequence B?=?[b1,?b2,?...,?bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beau

【算法系列学习】codeforces C. Mike and gcd problem

C. Mike and gcd problem http://www.cnblogs.com/BBBob/p/6746721.html 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 8 using namespace std; 9 const

Codeforces Round #361 (Div. 2) E. Mike and Geometry Problem

题目链接:传送门 题目大意:给你n个区间,求任意k个区间交所包含点的数目之和. 题目思路:将n个区间都离散化掉,然后对于一个覆盖的区间,如果覆盖数cnt>=k,则数目应该加上 区间长度*(cnt与k的组合数) ans=ans+(len*C(cnt,k))%mod; #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorith

codeforces 798C.Mike and gcd problem 解题报告

题目意思:给出一个n个数序列:a1,a2,...,an (n的范围[2,100000],ax的范围[1,1e9] 然后想构造一个beautiful的序列 b1,b2, ..., bn,使得最大公约数 gcd(b1,b2,...,bn) > 1.任意ai,ai+1 可以用 ai-ai+1, ai+ai+1 来替换. 问序列 a 构造成 b 的最小操作次数 首先,这个题目是肯定有解的,也就是恒输出yes 试想一下,相邻两个数之间无非就是四种情况: (1)对于同偶情况,不需要做转换,公约数直接为2:

HDOJ1086-You can Solve a Geometry Problem too(线段相交)

Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is ve

G - Mike and gcd problem

G - Mike and gcd problem Mike has a sequence A?=?[a1,?a2,?...,?an] of length n. He considers the sequence B?=?[b1,?b2,?...,?bn] beautiful if the gcd of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it be

hdoj-1086-You can Solve a Geometry Problem too 判断线段是否相交

You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8683 Accepted Submission(s): 4227 Problem Description Many geometry(几何)problems were designed in the ACM/ICPC.

HDU 1086 You can Solve a Geometry Problem too(判断线段相交)

题目地址:HDU 1086 就这么一道仅仅判断线段相交的题目写了2k多B的代码..是不是有点浪费...但是我觉得似乎哪里也优化不了了.... 判断线段相交就是利用的叉积.假如现在两条线段分别是L1和L2,先求L1和L2两个端点与L1的某个端点的向量的叉积,如果这两个的叉积的乘积小于0的话,说明L1在是在L2两个端点之间的,但此时并不保证一定相交.此时需要用同样的方法去判断L2是否在L1的两个端点之间,如果L2也在L1的两个端点之间的话,那就足以说明L1与L2相交.但是这题还需要判断是否端点也相交

HDU1086 You can Solve a Geometry Problem too(计算几何)

You can Solve a Geometry Problem too                                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)                                         Problem Description Many geometry(几何)problems wer