bzoj 2850 巧克力王国

本文介绍使用KD树解决bzoj2850巧克力王国问题,通过将问题转化为二维坐标上的点,利用KD树进行高效查询。详细解释了KD树的构建与查询过程,包括如何判断节点管辖范围与查询范围的关系,以及实现细节。

bzoj 2850 巧克力王国

  • 钱限题.题面可以看这里.
  • 显然 \(x\) \(y\) 可以看成坐标平面上的两维,蛋糕可以在坐标平面上表示为 \((x,y)\) ,权值为 \(h\) .用 \(kd-tree\) 维护这些点.
  • 查询时,类似于线段树,若当前节点管辖范围完全在查询范围内,直接返回当前节点记录的总和;若完全在查询范围外,返回 \(0\) ;否则进入两颗子树,递归处理.
  • 如何判断当前节点管辖范围与查询范围的关系?注意到查询范围是一个限制 \(ax+by<c\) ,即一个半平面.而用记录的 \(x,y\) 最大最小值组合成 \(4\) 个点,管辖范围是被包含在这个矩形中的.
  • 由于矩形是一个凸包,根据半平面的性质,若这个矩形的 \(4\) 个端点全都在范围内或范围外,则整个矩形也都在范围内或范围外,则管辖范围也全都在范围内或范围外.
  • 实现时注意一个小细节, \(kd-tree\) 节点的左右子树是不包含自身这个节点的,与线段树不同.所以进入递归前要判一下自身这个节点是否在范围中而统计贡献.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pii pair<int,int>
inline ll read()
{
    ll x=0;
    bool pos=1;
    char ch=getchar();
    for(;!isdigit(ch);ch=getchar())
        if(ch=='-')
            pos=0;
    for(;isdigit(ch);ch=getchar())
        x=x*10+ch-'0';
    return pos?x:-x;
}
const int MAXN=5e4+10;
int n,m,Dimen,rt;
struct node{
    ll sum,h;
    int v[2];
    int mi[2],ma[2];
    int ls,rs;
    bool operator < (const node &rhs) const
        {
            return v[Dimen]<rhs.v[Dimen];
        }
    node()
        {
            h=0;
            sum=0;
        }
}Tree[MAXN],A[MAXN];
#define root Tree[o]
#define lson Tree[root.ls]
#define rson Tree[root.rs]
#define inf 0x7fffffff
void init()
{
    for(int i=0;i<2;++i)
        {
            Tree[0].mi[i]=inf;
            Tree[0].ma[i]=-inf;
        }
    Tree[0].sum=0;
}
inline void pushup(int o)
{
    for(int i=0;i<2;++i)
        {
            root.mi[i]=min(root.mi[i],min(lson.mi[i],rson.mi[i]));
            root.ma[i]=max(root.ma[i],max(lson.ma[i],rson.ma[i]));
        }
    root.sum=lson.sum+rson.sum+root.h;
}
int BuildTree(int l,int r,int dimen)
{
    Dimen=dimen;
    int mid=(l+r)>>1;
    int o=mid;
    nth_element(A+l,A+mid,A+r+1);
    root.h+=A[mid].h;//位置可能重复? 
    for(int i=0;i<2;++i)
        {
            root.v[i]=A[mid].v[i];
            root.mi[i]=root.ma[i]=root.v[i];
        }
    root.ls=root.rs=0;
    if(l<=mid-1)
        root.ls=BuildTree(l,mid-1,dimen^1);
    if(mid+1<=r)
        root.rs=BuildTree(mid+1,r,dimen^1);
    pushup(o);
    return o;
}
ll ans,a,b,c;
ll calc(int x,int y)
{
    return 1LL*a*x+1LL*b*y;
}
bool totally_out(int o)
{
    if(calc(root.mi[0],root.mi[1])<c)
        return false;
    if(calc(root.ma[0],root.ma[1])<c)
        return false;
    if(calc(root.mi[0],root.ma[1])<c)
        return false;
    if(calc(root.ma[0],root.mi[1])<c)
        return false;
    return true;
}
bool totally_in(int o)
{
    if(calc(root.mi[0],root.mi[1])>=c)
        return false;
    if(calc(root.ma[0],root.ma[1])>=c)
        return false;
    if(calc(root.mi[0],root.ma[1])>=c)
        return false;
    if(calc(root.ma[0],root.mi[1])>=c)
        return false;
    return true;
}
void query(int o)
{
    if(!o)
        return;
    if(totally_in(o))
        {
            ans+=root.sum;
            return;
        }
    if(totally_out(o))
        return;
    if(1LL*a*root.v[0]+1LL*b*root.v[1]<c)
        ans+=root.h;
    query(root.ls);
    query(root.rs);
}
int main()
{
    n=read(),m=read();
    for(int i=1;i<=n;++i)
        {
            A[i].v[0]=read();
            A[i].v[1]=read();
            A[i].h=read();
        }
    init();
    rt=BuildTree(1,n,0);
    while(m--)
        {
            a=read(),b=read(),c=read();
            ans=0;
            query(rt);
            printf("%lld\n",ans);
        }
    return 0;
}

转载于:https://www.cnblogs.com/jklover/p/10407900.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值