要你求C(0,n)+…+C(m,n)
1e5组询问,n,m<=1e5
容易得到S(n,m)和S(n-1,m),S(n,m-1)之间关系,可以O(1)处理.使用莫队
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#define debug(x) //std::cerr << #x << " = " << (x) << std::endl
using namespace std;
typedef long long LL;
const int MAXN = 1e5 + 17;
const int MOD = 1e9 + 7;
LL jc[MAXN],inv[MAXN];
LL qm(LL a,LL b)
{
LL res = 1;
while(b)
{
if(b&1) res = res*a%MOD;
a=a*a%MOD;
b>>=1;
}
return res;
}
LL cinv(LL a)
{
return qm(a,MOD-2);
}
LL comb(LL a,LL b)
{
return ((jc[a]*inv[b])%MOD*inv[(a-b)])%MOD;
}
LL blk;
struct dap
{
int x,y,id;
dap(int a,int b,int c):x(a),y(b),id(c){}
bool operator<(const dap & b) const
{
if((x/blk)==(b.x/blk))
return y<b.y;
return x<b.x;
}
};
int main()
{
#ifdef noob
freopen("Input.txt", "r", stdin);
freopen("Output.txt", "w", stdout);
#endif
int n;
cin>>n;
blk = 333;
for(int i=jc[0]=1;i<=100000;++i)
{
jc[i] = jc[i-1]*i%MOD;
inv[i] = cinv(jc[i]);
}
inv[0] = inv[1];
vector<dap > all;
for (int i = 0; i < n; ++i)
{
int u,m;
scanf("%d%d",&u,&m);
all.push_back(dap(u,m,i));
}
sort(all.begin(), all.end());
vector<int > ans(100000+17,0);
int l = 0,r = 0;
LL now = 1;
for (int i = 0; i < n; ++i)
{
int x = all[i].x,y = all[i].y;
while(x<l)
{
now = ((now+comb(l-1, r))%MOD*cinv(2LL))%MOD;
l--;
}
while(x>l)
{
now = (((2LL*now)%MOD-comb(l,r))%MOD+MOD)%MOD;
l++;
}
while(y<r)
{
now = ((now-comb(l,r))%MOD+MOD)%MOD;
r--;
}
while(y>r)
{
now = (now+comb(l, r+1))%MOD;
r++;
}
ans[all[i].id] = now;
}
for (int i = 0; i < n; ++i)
printf("%lld\n",ans[i] );
return 0;
}
哇真是菜的要死,第一次学到莫队,莫队就是当区间答案可以o(1)转移时的方法,复杂度为n根号n级别的,l转移最多n根号n,r也最多n根号n.
这题也可以分块做,即放置一些预先算好的点,每个新询问,就去找他最近的那个,这样预处理和查询都是n根号n的,我觉得真的更加巧妙.
痛苦的一天…