CDQ分治

时间:2020-01-22 21:21:44   收藏:0   阅读:82

\(code :\)

void cdq(int l,int r)
{
    if(l==r) return;
    int mid=(l+r)>>1;
    cdq(l,mid),cdq(mid+1,r);
    sort(q+l,q+mid+1,cmp2);
    sort(q+mid+1,q+r+1,cmp2);
    int pos=l;
    for(int i=mid+1;i<=r;++i)
    {
        while(q[i].b>=q[pos].b&&pos<=mid)
        {
            update(q[pos].c,q[pos].val);
            pos++;
        }
        q[i].ans+=query(q[i].c);
    }
    for(int i=l;i<pos;++i) update(q[i].c,-q[i].val);
}

原文:https://www.cnblogs.com/lhm-/p/12229595.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!