【BZOJ2850】巧克力王国 KDtree
【BZOJ2850】巧克力王国
Description
Input
Output
Sample Input
1 2 5
3 1 4
2 2 1
2 1 6
1 3 5
1 3 7
Sample Output
0
4
HINT
1 <= n, m <= 50000,1 <= 10^9,-10^9 <= a, b, x, y <= 10^9。
题解:看起来ax+by<=c挺难搞,但其实只要改一改KDtree的估价函数就好了,具体见代码
注意:别忘了a,b,x,y可以是负数,负负能得正!
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define rep for(int i=0;i<=1;i++)
using namespace std;
typedef long long ll;
struct kd
{
int ls,rs,sm[2],sn[2],v[2];
ll sum,s;
kd (int a,int b,int c){ls=rs=0,sm[0]=sn[0]=v[0]=a,sm[1]=sn[1]=v[1]=b,s=sum=c;}
kd (){}
};
kd t[100010];
int n,m,D,root;
ll A,B,C;
bool cmp(kd a,kd b)
{
return (a.v[D]==b.v[D])?(a.v[D^1]<b.v[D^1]):(a.v[D]<b.v[D]);
}
void pushup(int x,int y)
{
rep t[x].sm[i]=max(t[x].sm[i],t[y].sm[i]),t[x].sn[i]=min(t[x].sn[i],t[y].sn[i]);
t[x].sum+=t[y].sum;
}
int build(int l,int r,int d)
{
if(l>r) return 0;
int mid=l+r>>1;
D=d;
nth_element(t+l,t+mid,t+r+1,cmp);
t[mid].ls=build(l,mid-1,d^1),t[mid].rs=build(mid+1,r,d^1);
if(t[mid].ls) pushup(mid,t[mid].ls);
if(t[mid].rs) pushup(mid,t[mid].rs);
return mid;
}
int check(int x)
{
int ret=0;
ret+=(A*t[x].sn[0]+B*t[x].sn[1]<C);
ret+=(A*t[x].sm[0]+B*t[x].sn[1]<C);
ret+=(A*t[x].sm[0]+B*t[x].sm[1]<C);
ret+=(A*t[x].sn[0]+B*t[x].sm[1]<C);
return ret;
}
ll query(int x)
{
if(!x||!check(x)) return 0;
if(check(x)==4) return t[x].sum;
ll ret=0;
if(A*t[x].v[0]+B*t[x].v[1]<C) ret+=t[x].s;
ret+=query(t[x].ls)+query(t[x].rs);
return ret;
}
int main()
{
scanf("%d%d",&n,&m);
int i,a,b,c;
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&a,&b,&c);
t[i]=kd(a,b,c);
}
root=build(1,n,0);
for(i=1;i<=m;i++)
{
scanf("%lld%lld%lld",&A,&B,&C);
printf("%lld\n",query(root));
}
return 0;
}
【BZOJ2850】巧克力王国 KDtree的更多相关文章
- 【BZOJ2850】巧克力王国 [KD-tree]
巧克力王国 Time Limit: 60 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 巧克力王国里的巧克力都是由牛奶和 ...
- [bzoj2850]巧克力王国_KD-Tree
巧克力王国 bzoj-2850 题目大意:给出n块巧克力,每块巧克力都有自己的两个参数x和y和本身的价值val,询问:m个人,每个人有两个系数和一个限度a,b,和c.求所有ax+by<=c的巧克 ...
- bzoj2850巧克力王国
巧克力王国 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 861 Solved: 325[Submit][Status][Discuss] Desc ...
- Bzoj2850 巧克力王国
Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 505 Solved: 204 Description 巧克力王国里的巧克力都是由牛奶和可可做成的.但 ...
- BZOJ 2850: 巧克力王国 KDtree + 估价函数
Code: #include<bits/stdc++.h> #define maxn 100000 #define inf 1000000008 #define mid ((l+r)> ...
- 【kd-tree】bzoj2850 巧克力王国
分四种情况讨论:a,b>=0 a,b<0 a>=0,b<0 a<0,b>=0 然后每次检验是否进入一个矩形框 或者 是否直接利用这个矩形框的答案 仅仅利用两个对角的 ...
- bzoj 2850 巧克力王国——KDtree
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2850 改一下估价即可.判断子树能否整个取或者是否整个不能取,时间好像就能行了? 因为有负数, ...
- KD-tree 专题「Hide and Seek · 巧克力王国」
Lockey的瞎理解 抄了一遍板子又水了俩题,感觉对KD-tree 稍稍理解了一点儿,唠叨一下(二维的KD-tree),如有错误请指出(Lockey 洗脸恭听) 普通平衡树维护的是一维的序列,但对于二 ...
- 洛谷 P4475 巧克力王国 解题报告
P4475 巧克力王国 题目描述 巧克力王国里的巧克力都是由牛奶和可可做成的.但是并不是每一块巧克力都受王国人民的欢迎,因为大家都不喜欢过于甜的巧克力. 对于每一块巧克力,我们设 \(x\) 和 \( ...
随机推荐
- jQuery带小图标的Tab切换焦点图
在线演示 本地下载
- c刷新缓冲区
int c; while((c = getchar()) != '\n' && c != EOF);
- MySQL数据库表分区功能详解
1.什么是表分区? mysql数据库中的数据是以文件的形势存在磁盘上的,默认放在/mysql/data下面(可以通过my.cnf中的datadir来查看),一张表主要对应着三个文件,一个是frm存放表 ...
- python——动态类型简介
动态类型简介 类型属于对象,而不是变量 每一个对象都有两个标准的头部信息:类型标志符和引用计数器. 对象的垃圾收集,就是通过引用计数器实现的.可以在脚本中任意使用对象而不需要考虑释放内存空间. 循环饮 ...
- Go语言学习之运算符(The way to go)
生命不止,继续go go go 今天介绍go中的运算符. 运算符大概分为: Arithmetic Operators Relational Operators Logical Operators Bi ...
- HTTP Status 500 - java.lang.NoClassDefFoundError: JspTagException
HTTP Status 500 - java.lang.NoClassDefFoundError: JspTagException cause java.lang.NoClassDefFoundEr ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- GeometryServer
http://blog.csdn.net/limina/article/details/8364515
- sed、awk学习篇
[yongsan@yz6245 ~]$ awk 'BEGIN {FS=":"}{shells[$NF]++;}END{for(i in shells)print i ": ...
- urllib.urlretrieve远程下载
下面我们再来看看 urllib 模块提供的 urlretrieve() 函数.urlretrieve() 方法直接将远程数据下载到本地. >>> help(urllib.urlret ...