hdu 6333 Harvest of Apples(组合预处理+莫队)

要你求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的,我觉得真的更加巧妙.
痛苦的一天…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值