传送门

主席树经典题。

首先把树搞出来,然后搞出来DFS序。然后离散化点权,在DFS序上建立主席树。

对于每个点对应的区间,查找对应的区间最大的点数即可。

//BZOJ2809
//by Cydiater
//2016.12.6
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <ctime>
#include <cmath>
#include <iomanip>
#include <bitset>
#include <set>
using namespace std;
#define ll long long
#define up(i,j,n)		for(int i=j;i<=n;i++)
#define down(i,j,n)		for(int i=j;i>=n;i--)
#define cmax(a,b)		a=max(a,b)
#define cmin(a,b)		a=min(a,b)
#define Auto(i,node)		for(int i=LINK[node];i;i=e[i].next)
#define FILE "dispatching"
const int MAXN=1e5+5;
const ll oo=1LL<<55;
inline int read(){
	char ch=getchar();int x=0,f=1;
	while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int N,root[MAXN],cnt=0,siz[MAXN],dfn[MAXN],dfs_clock=0;
ll M,va[MAXN],vb[MAXN],fsort[MAXN],rnum,val[MAXN],ans=0;
struct Graph{
	int LINK[MAXN],len;
	struct edge{
		int y,next;
	}e[MAXN<<1];
	inline void insert(int x,int y){e[++len].next=LINK[x];LINK[x]=len;e[len].y=y;}
	inline void Insert(int x,int y){insert(x,y);insert(y,x);}
	void make_graph(){
		up(i,1,N){
			int fa=read();va[i]=read();vb[i]=read();
			fsort[i]=va[i];
			if(!fa)continue;
			Insert(i,fa);
		}
	}
	void dfs(int node,int father){
		siz[node]=1;dfn[++dfs_clock]=node;
		Auto(i,node)if(e[i].y!=father){
			dfs(e[i].y,node);
			siz[node]+=siz[e[i].y];
		}
	}
}G;
struct Chair_man_Tree{
	ll cost,sum;
	int son[2];
}t[MAXN<<5];
namespace solution{
	void init(){
		N=read();M=read();
		G.make_graph();
		G.dfs(1,0);
	}
	int NewNode(ll cost,int sum,int son0,int son1){
		t[++cnt].cost=cost;t[cnt].sum=sum;
		t[cnt].son[0]=son0;t[cnt].son[1]=son1;
		return cnt;
	}
	void insert(int leftt,int rightt,int &Root,int last,int pos){
		Root=NewNode(t[last].cost+fsort[pos],t[last].sum+1,t[last].son[0],t[last].son[1]);
		int mid=(leftt+rightt)>>1;
		if(leftt==rightt)	return;
		if(pos<=mid)		insert(leftt,mid,t[Root].son[0],t[last].son[0],pos);
		else			insert(mid+1,rightt,t[Root].son[1],t[last].son[1],pos);
	}
	ll Get(int S,int T,int leftt,int rightt,ll LIM){
		if(leftt==rightt)		return min(LIM/fsort[leftt],t[T].sum-t[S].sum);
		int mid=(leftt+rightt)>>1;
		ll cost=t[t[T].son[0]].cost-t[t[S].son[0]].cost;
		if(cost<=LIM)	return t[t[T].son[0]].sum-t[t[S].son[0]].sum+Get(t[S].son[1],t[T].son[1],mid+1,rightt,LIM-cost);
		else		return Get(t[S].son[0],t[T].son[0],leftt,mid,LIM);
	}
	void slove(){
		sort(fsort+1,fsort+N+1);
		rnum=unique(fsort+1,fsort+N+1)-(fsort+1);
		up(i,1,N)val[i]=lower_bound(fsort+1,fsort+rnum+1,va[dfn[i]])-fsort;
		up(i,1,N)insert(1,rnum,root[i],root[i-1],val[i]);
		up(i,1,N){
			int L=i,R=i+siz[dfn[i]]-1;
			cmax(ans,vb[dfn[i]]*Get(root[L-1],root[R],1,rnum,M));
		}
	}
	void output(){
		cout<<ans<<endl;
	}
}
int main(){
	//freopen("input.in","r",stdin);
	//freopen(FILE".in","r",stdin);
	//freopen(FILE".out","w",stdout);
	using namespace solution;
	init();
	slove();
	output();
	return 0;
}

BZOJ2809: [Apio2012]dispatching的更多相关文章

  1. bzoj2809 [Apio2012]dispatching(左偏树)

    [Apio2012]dispatching Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 M ...

  2. BZOJ2809 [Apio2012]dispatching 可并堆

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2809 题意概括 n个点组成一棵树,每个点都有一个领导力和费用,可以让一个点当领导,然后在这个点的子 ...

  3. BZOJ2809——[Apio2012]dispatching

    1.题目大意:给一棵树和M值,每个点有两个权值C和L,选x个点,这x个点的C值的和不能超过M,且这x个点如果都在某个子树内 定义满意度为x*这个子树的根的L值 2.分析:这是一道可并堆的题目,我们考虑 ...

  4. [BZOJ2809][Apio2012]dispatching(左偏树)

    首先对于一个节点以及它的子树,它的最优方案显然是子树下选最小的几个 用左偏树维护出每棵子树最优方案的节点,记录答案 然后它的这棵树可以向上转移给父节点,将所有子节点的左偏树合并再维护就是父节点的最优方 ...

  5. 【DFS序】【莫队算法】【权值分块】bzoj2809 [Apio2012]dispatching

    题意:在树中找到一个点i,并且找到这个点子树中的一些点组成一个集合,使得集合中的所有点的c之和不超过M,且Li*集合中元素个数和最大 首先,我们将树处理出dfs序,将子树询问转化成区间询问. 然后我们 ...

  6. bzoj2809 [Apio2012]dispatching——左偏树(可并堆)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2809 思路有点暴力和贪心,就是 dfs 枚举每个点作为管理者: 当然它的子树中派遣出去的忍者 ...

  7. [BZOJ2809][Apio2012]dispatching 贪心+可并堆

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2809 我们考虑以每一个节点作为管理者所得的最优答案,一定是优先选择所要薪水少的忍者.那么首 ...

  8. 【BZOJ2809】[Apio2012]dispatching 可并堆

    [BZOJ2809][Apio2012]dispatching Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 M ...

  9. 【bzoj2809】[Apio2012]dispatching 左偏树

    2016-05-31  15:56:57 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2809 直观的思想是当领导力确定时,尽量选择薪水少的- ...

随机推荐

  1. VS2012 Unit Test —— 我对IdleTest库动的大手术以及对Xml相关操作进行测试的方式

    [1]我的IdleTest源码地址:http://idletest.codeplex.com/ [2]IdleTest改动说明:2013年10月份在保持原有功能的情况下对其动了较大的手术,首先将基本的 ...

  2. 获取当前应用的系统路径工具类和java的System.getProperty()方法介绍

    java的System.getProperty()方法可以获取的值,如下: 对于Java程序,无论是未打包的还是打包的JAR或WAR文件,有时候都需要获取它运行所在目录信息,如何做到这一点呢? /** ...

  3. swfupload 相关配置

    部署在IIS上出现404: 修改 C:\Windows\System32\inetsrv\config\applicationHost.config 文件 连续查找requestFiltering,往 ...

  4. win10关机指示灯亮解决办法

    新上市的win10,大家都迫不及待的装上了,但是却遇到了很多问题. 对于win10关机指示灯亮的解决办法有下面几种. 指示灯 如果是只有电源的指示灯亮,拔掉电源即可. 如果是信号灯和其他的指示灯都亮原 ...

  5. I2C子系统之驱动SSD1306 OLED

    理解I2C设备驱动框架,主要围绕四个结构体去分析就容易了. struct i2c_algorithm:提供I2C协议的实现的操作,如:master_xfer实现数据收发的最基本方法. struct i ...

  6. xshell不能输入中文,显示为??

    不知不觉间发现自己使用xshell的时候不能输入中文了,输入的中文会变成"??",开始以为是编码问题,看了一下编码设置后发现没有任何问题,而且显示中文没有出现乱码问题,只是在输入的 ...

  7. [python]python try异常处理机制

    #python的try语句有两种风格 #一:种是处理异常(try/except/else) #二:种是无论是否发生异常都将执行最后的代码(try/finally) try/except/else风格 ...

  8. 排序算法----基数排序(RadixSort(L,max))单链表版本

    转载http://blog.csdn.net/Shayabean_/article/details/44885917博客 先说说基数排序的思想: 基数排序是非比较型的排序算法,其原理是将整数按位数切割 ...

  9. HashMap源码分析

    最近一直特别忙,好不容易闲下来了.准备把HashMap的知识总结一下,很久以前看过HashMap源码.一直想把集合类的知识都总结一下,加深自己的基础.我觉的java的集合类特别重要,能够深刻理解和应用 ...

  10. Transient的作用

    1:transient的作用及其使用方法 当一个对象实现类Serilizable接口,那么这个类就可以被序列化,java的这种序列化的模式为开发者提供了很多的便利. 然而在实际开发中,我们常常遇到这样 ...