KD-Tree模板

.
题:Bzoj2850
参考资料
好像其实挺好写的(就是慢了很多很多),复杂度是 O ( k n 1 − 1 k ) O(kn^{1-\frac{1}{k}}) O(kn1k1)
只能算是一个“分块级别”的暴力,离线段树这种还差很多

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 50010
#define LL long long
using namespace std;
struct P{ int x,y,v; } w[N];
struct tree{
	int U,D,L,R,l,r;
	int x,y,v; LL S;
} s[N];
inline bool cx(P a,P b){ return a.x<b.x; }
inline bool cy(P a,P b){ return a.y<b.y; }
inline void gmx(int& a,int b){ a<b?a=b:0; }
inline void gmn(int& a,int b){ a>b?a=b:0; }
inline void merge(int x){
	s[x].L=s[x].R=s[x].x;
	s[x].U=s[x].D=s[x].y;
	int y; s[x].S=s[x].v;
	if(y=s[x].l){
		s[x].S+=s[y].S;
		gmx(s[x].R,s[y].R);
		gmx(s[x].D,s[y].D);
		gmn(s[x].L,s[y].L);
		gmn(s[x].U,s[y].U);
	}
	if(y=s[x].r){
		s[x].S+=s[y].S;
		gmx(s[x].R,s[y].R);
		gmx(s[x].D,s[y].D);
		gmn(s[x].L,s[y].L);
		gmn(s[x].U,s[y].U);
	}
}
inline int build(int l,int r,int o){
	if(l>r) return 0;
	int m=l+r>>1;
	nth_element(w+l,w+m,w+r+1,o?cx:cy);
	s[m]=(tree){0,0,0,0,build(l,m-1,!o),build(m+1,r,!o),w[m].x,w[m].y,w[m].v,0};
	merge(m); return m;
}
int n,m,rt,A,B,C; //querys
inline int c(int x,int y){ return A*x+B*y<C; }
inline int Q(tree x){ return c(x.L,x.U)+c(x.R,x.U)+c(x.L,x.D)+c(x.R,x.D); }
inline LL query(tree& x,int q){
	if(q==4) return x.S;
	LL A=c(x.x,x.y)?x.v:0;
	int y=x.l,t;
	if(y&&(t=Q(s[y]))) A+=query(s[y],t);
	y=x.r;
	if(y&&(t=Q(s[y]))) A+=query(s[y],t);
	return A;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;++i) scanf("%d%d%d",&w[i].x,&w[i].y,&w[i].v);
	rt=build(1,n,0);
	for(;m--;){
		scanf("%d%d%d",&A,&B,&C);
		printf("%lld\n",query(s[rt],Q(s[rt])));
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值