BZOJ 2809: [Apio2012]dispatching [主席树 DFS序]
题意:查询树上根节点值*子树中权值和$\le m$的最大数量 最大值是多少
求$DFS$序,然后变成区间中和$\le m$最多有几个元素,建主席树,然后权值线段树上二分就行了
$WA$:又把边表开小了.....
好吧我$zz$了有根树加无向边干什么....
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lc(x) t[x].l
#define rc(x) t[x].r
typedef long long ll;
const int N=1e5+;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,m,mp[N];
struct Ninjia{
int w,li,id;
bool operator <(const Ninjia &r)const{return w<r.w;}
}a[N];
inline bool cmpId(Ninjia a,Ninjia b){return a.id<b.id;}
struct Edge{
int v,ne;
}e[N<<];
int h[N],cnt;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
}
int L[N],R[N],dfc,pos[N];
void dfs(int u,int fa){
L[u]=++dfc; pos[dfc]=u;
for(int i=h[u];i;i=e[i].ne)
if(e[i].v!=fa) dfs(e[i].v,u);
R[u]=dfc;
} struct Node{
int l,r,size;
ll sum;
}t[N*];
int sz,root[N];
void fIns(int &x,int l,int r,int p){
t[++sz]=t[x];x=sz;
t[x].size++;
t[x].sum+=(ll)mp[p];
if(l==r) return;
int mid=(l+r)>>;
if(p<=mid) fIns(lc(x),l,mid,p);
else fIns(rc(x),mid+,r,p);
}
int fQue(int x,int y,int l,int r,ll m){
//printf("fQue %d %d %d %d %d %d %lld %lld\n",x,y,l,r,t[x].size,t[y].size,t[x].sum,t[y].sum);
if(l==r){
ll lsum=t[y].sum-t[x].sum;
return lsum<=m ? t[y].size-t[x].size : ;
}else{
int mid=(l+r)>>;
ll lsum=t[lc(y)].sum-t[lc(x)].sum;//printf("lsum %lld %lld\n",lsum,m);
if(m<=lsum) return fQue(lc(x),lc(y),l,mid,m);
else return t[lc(y)].size-t[lc(x)].size+fQue(rc(x),rc(y),mid+,r,m-lsum);
}
}
int rt=,u;
int main(){
freopen("in","r",stdin);
n=read();m=read();
for(int i=;i<=n;i++){
u=read();if(u==) rt=i;
ins(u,i),a[i].w=read(),a[i].li=read(),a[i].id=i;
}
sort(a+,a++n);
for(int i=;i<=n;i++) mp[i]=a[i].w,a[i].w=i;
sort(a+,a++n,cmpId);
//for(int i=1;i<=n;i++) printf("a %d %d %d\n",a[i].id,a[i].w,mp[a[i].w]);
dfs(rt,);
//for(int i=1;i<=n;i++) printf("LR %d %d %d\n",i,L[i],R[i]);
//for(int i=1;i<=n;i++) printf("%d ",pos[i]);puts("");
for(int i=;i<=n;i++) root[i]=root[i-],fIns(root[i],,n,a[pos[i]].w);
ll ans=;
for(int i=;i<=n;i++) ans=max(ans,(ll)a[i].li*fQue(root[L[i]-],root[R[i]],,n,m));
printf("%lld",ans);
}
BZOJ 2809: [Apio2012]dispatching [主席树 DFS序]的更多相关文章
- BZOJ - 2809 dispatching 主席树+dfs序
在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级.为保密,同时增强忍者们的 ...
- bzoj 3772 精神污染 主席树+dfs序
精神污染 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 637 Solved: 177[Submit][Status][Discuss] Descri ...
- [Apio2012]dispatching 主席树做法
bzoj 2809: [Apio2012]dispatching http://www.lydsy.com/JudgeOnline/problem.php?id=2809 Description 在一 ...
- 51 nod 1681 公共祖先 (主席树+dfs序)
1681 公共祖先 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 有一个庞大的家族,共n人.已知这n个人的祖辈关系正好形成树形结构(即父亲向儿子连边). 在另 ...
- 【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序
[BZOJ1803]Spoj1487 Query on a tree III Description You are given a node-labeled rooted tree with n n ...
- BZOJ 2809: [Apio2012]dispatching( 平衡树 + 启发式合并 )
枚举树上的每个结点做管理者, 贪心地取其子树中薪水较低的, 算出这个结点为管理者的满意度, 更新答案. 用平衡树+启发式合并, 时间复杂度为O(N log²N) ------------------- ...
- bzoj 2809: [Apio2012]dispatching -- 可并堆
2809: [Apio2012]dispatching Time Limit: 10 Sec Memory Limit: 128 MB Description 在一个忍者的帮派里,一些忍者们被选中派 ...
- bzoj 2809: [Apio2012]dispatching【dfs序+主席树】
可并堆就可以,但是想复健一下主席树. 考虑枚举管理者,然后选忍者的时候在子树中贪心的从小到大选.做成dfs序就是选区间内和小于等于k的最多点.可以用主席树,查询的时候在主席树上二分即可 这里注意,为了 ...
- 【SPOJ】10628. Count on a tree(lca+主席树+dfs序)
http://www.spoj.com/problems/COT/ (速度很快,排到了rank6) 这题让我明白了人生T_T 我知道我为什么那么sb了. 调试一早上都在想人生. 唉. 太弱. 太弱. ...
随机推荐
- A. Duff and Meat
A. Duff and Meat time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Eclipse安装JD-Eclipse反编译插件成功看源码
Eclipse安装JD-Eclipse反编译插件 转载 2017年12月24日 15:19:27 http://heavengate.blog.163.com/blog/static/202381 ...
- 从零开始学习前端开发 — 14、CSS3变形基础
一.css3变形: transform:rotate(旋转)|scale(缩放)|skew(倾斜)|translate(位移); 注:当多种变形方式综合在一起时,用空格隔开 1.旋转 a) rotat ...
- thinkphp5自动完成
- tp5命名空间
- 再叙Java反射
Java中的反射 本文为反射的基础知识部分. 能够分析类能力的程序被称为反射(reflective). 反射机制允许程序在运行时取得任何一个已知名称的class的内部信息,容许程序在运行时加载.探知. ...
- Spring MVC 用post方式提交表单到Controller乱码问题,而get方式提交没有乱码问题
在web.xml中添加一个filter,即可解决post提交到Spring MVC乱码问题 <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码--> <filter> ...
- mysql-SQL优化总结
1.查询首先考虑在where和order by设计的列上建立索引,尽量避免全表扫描. 2.尽量避免在where子句中对字段进行null值判断,否则将导致引擎放弃使用索引而进行全表扫描. select ...
- 深入浅出讲解:php的socket通信[转]
对TCP/IP.UDP.Socket编程这些词你不会很陌生吧?随着网络技术的发展,这些词充斥着我们的耳朵.那么我想问: 1. 什么是TCP/IP.UDP?2. Sock ...
- sql 查询 ORA-12170 TNS 连接超时特殊原因
一般对于ORA-12170这个问题 1 看数据ip 是否能ping通 2 数据库服务是否启动 3 数据库服务所在服务器防火墙 然而当你能进去数据,而报这个这错时: 1 看代码中数据库连接是写错 2 如 ...