- 钱限题.题面可以看这里.
- 显然 (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
",ans);
}
return 0;
}