[KD-TREE] BZOJ 2850 巧克力王国

时间:2022-01-23 20:48:37

题意:一个半平面内点权和

应该都能看懂


#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;

inline char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
	return *p1++;
}

inline void read(int &x)
{
	char c=nc(),b=1;
	for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

inline void read(ll &x)
{
	char c=nc(),b=1;
	for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

const int N=50005;

int D;

struct node{
	int d[2],mx[2],mn[2],l,r;
	ll val,sum;
	node(int x=0,int y=0,int v=0) { mx[0]=mn[0]=d[0]=x; mx[1]=mn[1]=d[1]=y; l=r=0; val=sum=v; } 
	int& operator[](int x) { return d[x]; }
	friend bool operator == (const node &A, const node &B) { return A.d[0]==B.d[0] && A.d[1]==B.d[1]; }
	friend bool operator < (const node &A,const node &B) { return A.d[D]<B.d[D]; }
}tmp[N];

ll A,B,C;

inline bool Check(int x,int y){
	return A*x+B*y<C;
}

inline int calc(node x){
	return Check(x.mn[0],x.mn[1])+Check(x.mn[0],x.mx[1])+Check(x.mx[0],x.mn[1])+Check(x.mx[0],x.mx[1]);
}

struct KDT{
	node T[N],now;
	int root; ll ans;
	void update(int x){
		int l=T[x].l,r=T[x].r;
		for(int i=0;i<2;i++)
		{
			T[x].mn[i]=T[x].mx[i]=T[x][i];
			if(l) T[x].mn[i]=min(T[x].mn[i],T[l].mn[i]),T[x].mx[i]=max(T[x].mx[i],T[l].mx[i]);
			if(r) T[x].mn[i]=min(T[x].mn[i],T[r].mn[i]),T[x].mx[i]=max(T[x].mx[i],T[r].mx[i]);
		}
		T[x].sum=T[x].val+T[l].sum+T[r].sum;
	}
	int Reb(int l,int r,int D){
		if (l>r) return 0;
		int mid=(l+r)>>1;
		::D=D; nth_element(tmp+l,tmp+mid,tmp+r+1);
		T[mid]=tmp[mid];
		T[mid].l=Reb(l,mid-1,D^1);
		T[mid].r=Reb(mid+1,r,D^1);
		update(mid); return mid;
	}
	void Query(int x){
		if (!x) return;
		if(Check(T[x][0],T[x][1])) ans+=T[x].val;
		int sl=0,sr=0,l=T[x].l,r=T[x].r;
		if (l) sl=calc(T[l]);
		if (r) sr=calc(T[r]);
		if (sl==4) ans+=T[l].sum; else if (sl) Query(l);
		if (sr==4) ans+=T[r].sum; else if (sr) Query(r);
	}
}KD;

int n;

int main()
{
	int Q,x,y,v;
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	read(n); read(Q);
	for (int i=1;i<=n;i++)
		read(x),read(y),read(v),tmp[i]=node(x,y,v);
	KD.root=KD.Reb(1,n,0);
	while (Q--)
	{
		read(A); read(B); read(C);
		KD.ans=0; KD.Query(KD.root);
		printf("%lld\n",KD.ans);
	}
	return 0;
}