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