# 洛谷P4475 巧克力王国

## 输入输出样例

3 3
1 2 5
3 1 4
2 2 1
2 1 6
1 3 5
1 3 7

5
0
4

## 题解Here!

#include<iostream>
#include<algorithm>
#include<cstdio>
#define MAXN 50010
#define MAX (1LL<<60)
using namespace std;
int n,m,root;
bool sort_flag=false;
struct Point{
long long x,y,z;
friend bool operator <(const Point &p,const Point &q){
if(sort_flag)return p.y<q.y;
return p.x<q.x;
}
}point[MAXN],now;
struct Tree{
Point point;
long long minx,miny,maxx,maxy,val,sum;
int lson,rson;
}a[MAXN];
inline int read(){
int date=0,w=1;char c=0;
while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
return date*w;
}
void pushup(int rt){
int lson=a[rt].lson,rson=a[rt].rson;
a[rt].sum=a[lson].sum+a[rson].sum+a[rt].val;
a[rt].maxx=max(a[rt].maxx,max(a[lson].maxx,a[rson].maxx));
a[rt].maxy=max(a[rt].maxy,max(a[lson].maxy,a[rson].maxy));
a[rt].minx=min(a[rt].minx,min(a[lson].minx,a[rson].minx));
a[rt].miny=min(a[rt].miny,min(a[lson].miny,a[rson].miny));
}
void buildtree(int l,int r,int &rt,int flag){
int mid=l+r>>1;
rt=mid;
sort_flag=flag;
nth_element(point+l,point+mid,point+r+1);
a[rt].point=point[mid];
a[rt].val=point[mid].z;
a[rt].maxx=a[rt].minx=point[mid].x;
a[rt].maxy=a[rt].miny=point[mid].y;
if(l<mid)buildtree(l,mid-1,a[rt].lson,flag^1);
if(mid<r)buildtree(mid+1,r,a[rt].rson,flag^1);
pushup(rt);
}
inline bool check(int rt){
return ((a[rt].point.x*now.x+a[rt].point.y*now.y)<now.z);
}
inline long long max_dis(int rt){
long long x,y;
x=max(a[rt].minx*now.x,a[rt].maxx*now.x);
y=max(a[rt].miny*now.y,a[rt].maxy*now.y);
return x+y;
}
inline long long min_dis(int rt){
long long x,y;
x=min(a[rt].minx*now.x,a[rt].maxx*now.x);
y=min(a[rt].miny*now.y,a[rt].maxy*now.y);
return x+y;
}
long long query(int rt){
long long dis=max_dis(rt);
if(dis<now.z)return a[rt].sum;
dis=min_dis(rt);
if(dis>=now.z)return 0;
long long ans=0;
if(check(rt))ans+=a[rt].val;
if(a[rt].lson)ans+=query(a[rt].lson);
if(a[rt].rson)ans+=query(a[rt].rson);
return ans;
}
void work(){
while(m--){
now.x=read();now.y=read();now.z=read();
printf("%lld\n",query(root));
}
}
void init(){
n=read();m=read();
a[0].maxx=a[0].maxy=-MAX;
a[0].minx=a[0].miny=MAX;
for(int i=1;i<=n;i++){point[i].x=read();point[i].y=read();point[i].z=read();}
buildtree(1,n,root,0);
}
int main(){
init();
work();
return 0;
}


posted @ 2019-04-05 09:08  符拉迪沃斯托克  阅读(178)  评论(0编辑  收藏  举报
Live2D