【BZOJ 2850】巧克力王国

时间:2023-03-10 06:33:18
【BZOJ 2850】巧克力王国

  复习了下KDtree,贴一下新板子233。

 #include "bits/stdc++.h"

 using namespace std;

 inline int read(){
int s=,k=;char ch=getchar ();
while (ch<''|ch>'') ch=='-'?k=-:,ch=getchar();
while (ch>&ch<='') s=s*+(ch^),ch=getchar();
return s*k;
} const int N=5e4+; typedef long long ll; int Kd; ll A,B,C,ans; #define sum(x) (x?x->sum:0) struct Point{
int d[],mx[],mn[],val;
Point *lc,*rc;
ll sum;
int& operator [](int k){
return d[k];
}
friend bool operator <(Point a,Point b){
return a[Kd]<b[Kd];
}
inline void update(){
for (int i=;i<;++i) {
mx[i]=mn[i]=d[i];
if(lc) mx[i]=max(mx[i],lc->mx[i]),mn[i]=min(mn[i],lc->mn[i]);
if(rc) mx[i]=max(mx[i],rc->mx[i]),mn[i]=min(mn[i],rc->mn[i]);
}
sum=val+sum(lc)+sum(rc);
}
}cho[N]; #define check(x,y) (A*x+B*y<C) inline int calc(Point x){
int ret=;
ret=check(x.mn[],x.mn[])+check(x.mx[],x.mx[])
+check(x.mn[],x.mx[])+check(x.mx[],x.mn[]);
return ret;
} struct KdTree{
inline Point* build(int l,int r,int now){
Kd=now;int mid=l+r>>;
nth_element(cho+l,cho+mid,cho+r+);
Point *t=&cho[mid];
if(l^mid) t->lc=build(l,mid-,now^);
if(r^mid) t->rc=build(mid+,r,now^);
t->update();
return t;
}
inline void query(Point *t){
if (check(t->d[],t->d[])) ans+=t->val;
int tl=,tr=;
if(t->lc) tl=calc(*(t->lc));
if(t->rc) tr=calc(*(t->rc));
if(tl&) ans+=t->lc->sum;
else if(tl) query(t->lc);
if(tr&) ans+=t->rc->sum;
else if(tr) query(t->rc);
} }kd; int n,m; int main(){
n=read(),m=read();
register int i,j;
for (i=;i<=n;++i)
cho[i][]=read(),cho[i][]=read(),
cho[i].val=read();
Point *root=kd.build(,n,);
while (m--){
A=read(),B=read(),C=read();
ans=;
kd.query(root);
printf("%lld\n",ans);
}
}