提交时间:2023-08-16 15:34:01
运行 ID: 98817
#include<bits/stdc++.h> #define ll long long using namespace std; const int mod=1e9+7; const int N=3e5+10; ll Q,n,k,s,jc[N],inv[N],ans[N]; inline ll kpow(ll x,ll n){ if(n==0)return 1; if(n==1)return x; if(n&1)return kpow(x*x%mod,n>>1)*x%mod; return kpow(x*x%mod,n>>1); } ll work(){ jc[0]=1; for(ll i=1;i<=N;i++)jc[i]=jc[i-1]*i%mod; for(ll i=0;i<=N;i++)inv[i]=kpow(jc[i],mod-2); } inline void write(ll x){ if(x>9)write(x/10); putchar(x%10+'0'); } inline void read(ll &x){ x=0;bool f=0;char c=getchar(); while(c>'9'||c<'0'){if(c=='-')f=1;c=getchar();} while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar(); if(f)x=-x; } inline ll C(ll n,ll m){ return jc[n]*inv[n-m]%mod*inv[m]%mod; } struct node{ ll l,r,id; inline bool operator<(const node &x)const{ if(l/s==x.l/s)return r<x.r; return l<x.l; } inline bool operator>(const node &x)const{ if(l/s==x.l/s)return r>x.r; return l>x.l; } }q[N]; int main(){ // freopen("ssh.in","r",stdin); // freopen("ssh.out","w",stdout); work(); read(Q);s=sqrt(100000); for(ll i=1;i<=Q;i++){ read(q[i].l);read(q[i].r); q[i].id=i; } sort(q+1,q+Q+1); ll l=1,r=0,cnt=1; for(ll i=1;i<=Q;i++){ while(r>q[i].r)cnt=(cnt-C(l,r--)+mod)%mod; while(l<q[i].l)cnt=(cnt*2-C(l++,r)+mod)%mod; while(r<q[i].r)cnt=(cnt+C(l,++r))%mod; while(l>q[i].l)cnt=(cnt+C(--l,r))*inv[2]%mod; ans[q[i].id]=(ll)(cnt+mod)%mod; } for(ll i=1;i<=Q;i++){ write(ans[i]);putchar('\n'); } return 0; } /* 10 1 1 3 2 5 2 8 3 12 0 642 246 2222 999 2525 21 50000 25000 100000 100000 */