洛谷P3066 [USACO12DEC] 逃跑的Barn [左偏树]
逃跑的Barn
题目描述
It's milking time at Farmer John's farm, but the cows have all run away! Farmer John needs to round them all up, and needs your help in the search.
FJ's farm is a series of N (1 <= N <= 200,000) pastures numbered 1...N connected by N - 1 bidirectional paths. The barn is located at pasture 1, and it is possible to reach any pasture from the barn.
FJ's cows were in their pastures this morning, but who knows where they ran to by now. FJ does know that the cows only run away from the barn, and they are too lazy to run a distance of more than L. For every pasture, FJ wants to know how many different pastures cows starting in that pasture could have ended up in.
Note: 64-bit integers (int64 in Pascal, long long in C/C++ and long in Java) are needed to store the distance values.
给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个。
输入输出格式
输入格式:
* Line 1: 2 integers, N and L (1 <= N <= 200,000, 1 <= L <= 10^18)
* Lines 2..N: The ith line contains two integers p_i and l_i. p_i (1 <= p_i < i) is the first pasture on the shortest path between pasture i and the barn, and l_i (1 <= l_i <= 10^12) is the length of that path.
输出格式:
* Lines 1..N: One number per line, the number on line i is the number pastures that can be reached from pasture i by taking roads that lead strictly farther away from the barn (pasture 1) whose total length does not exceed L.
输入输出样例
4 5
1 4
2 3
1 5
3
2
1
1
说明
Cows from pasture 1 can hide at pastures 1, 2, and 4.
Cows from pasture 2 can hide at pastures 2 and 3.
Pasture 3 and 4 are as far from the barn as possible, and the cows can hide there.
分析:
一句话:左偏树。
做多了这类题目的话,这题也就不难想。从根节点向下$DFS$,然后递归回溯,到达某个结点时就把它和它的子树构成的左偏树合并,这里左偏树维护的权值是该点到根节点的距离,判断的时候用类似于前缀和的思想就行了。
Code:
//It is made by HolseLee on 28th Aug 2018
//Luogu.org P3066
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std; typedef long long ll;
const int N=2e5+;
int n,ans[N],nxt[N],head[N],to[N],cnte,rt[N];
ll m,v[N];
struct Leftist{
int ch[N][],dis[N],siz[N];
ll val[N]; int merge(int x,int y)
{
if( !x || !y )return x+y;
if( val[x]<val[y] || (val[x]==val[y] && x<y) )
swap(x,y);
int &ul=ch[x][], &ur=ch[x][];
ur=merge(ur,y);
if( dis[ur]>dis[ul] ) swap(ur,ul);
dis[x]=dis[ur]+;
siz[x]=siz[ur]+siz[ul]+;
return x;
} inline int delet(int x)
{
return merge(ch[x][],ch[x][]);
}
}T; template<typename re>
inline void read(re &x)
{
x=; char ch=getchar(); bool flag=false;
while( ch<''||ch>'' ) {
if( ch=='-' )flag=true;
ch=getchar();
}
while( ch>=''&&ch<='' ) {
x=(x<<)+(x<<)+(ch^);
ch=getchar();
}
x=flag?-x:x;
} void dfs(int u,ll now)
{
rt[u]=u;
T.val[u]=now, T.siz[u]=, T.dis[u]=;
T.ch[u][]=T.ch[u][]=;
for(int i=head[u]; i; i=nxt[i]) {
dfs(to[i],now+v[i]);
rt[u]=T.merge(rt[u],rt[to[i]]);
}
while( rt[u] && (T.val[rt[u]]-now)>m ) rt[u]=T.delet(rt[u]);
ans[u]=T.siz[rt[u]];
} int main()
{
read(n); read(m);
int x; ll y;
for(int i=; i<=n; ++i) {
read(x); read(y);
to[++cnte]=i;v[cnte]=y;
nxt[cnte]=head[x];head[x]=cnte;
}
dfs(,);
for(int i=;i<=n;++i)printf("%d\n",ans[i]);
return ;
}
洛谷P3066 [USACO12DEC] 逃跑的Barn [左偏树]的更多相关文章
- P3066 [USACO12DEC] 逃跑的Barn 左偏树
P3066 逃跑的Barn 左偏树 题面 题意:给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个. 注意到答案的两个性质: 一个点的所有答案一定包含在其所有儿子的答案中 如 ...
- 洛谷P3066 [USACO12DEC]逃跑的Barn (线段树合并)
题目描述It's milking time at Farmer John's farm, but the cows have all run away! Farmer John needs to ro ...
- 洛谷.3273.[SCOI2011]棘手的操作(左偏树)
题目链接 还是80分,不是很懂. /* 七个操作(用左偏树)(t2表示第二棵子树): 1.合并:直接合并(需要将一个t2中原有的根节点删掉) 2.单点加:把这个点从它的堆里删了,加了再插入回去(有负数 ...
- 洛谷P3273 [SCOI2011] 棘手的操作 [左偏树]
题目传送门 棘手的操作 题目描述 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边,连接第x个节点和第y个节点 A1 ...
- 洛谷P4331 [BOI2004] Sequence 数字序列 [左偏树]
题目传送门 数字序列 题目描述 给定一个整数序列 a1,a2,⋅⋅⋅,an ,求出一个递增序列 b1<b2<⋅⋅⋅<bn ,使得序列 ai 和 bi 的各项之差的绝对 ...
- 模板 可并堆【洛谷P3377】 【模板】左偏树(可并堆)
P3377 [模板]左偏树(可并堆) 如题,一开始有N个小根堆,每个堆包含且仅包含一个数.接下来需要支持两种操作: 操作1: 1 x y 将第x个数和第y个数所在的小根堆合并(若第x或第y个数已经被删 ...
- 洛谷$P4331\ [BOI2004]\ Sequence$ 数字序列 左偏树
正解:左偏树 解题报告: 传送门$QwQ$ 开始看到的时候$jio$得长得很像之前做的一个$dp$,,, 但是$dp$那题是说不严格这里是严格? 不难想到我们可以让$a_{i},b_{i}$同时减去$ ...
- 洛谷P3066 [USACO12DEC]逃跑的BarnRunning Away From…
题面链接 一句话题意:给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于等于l的点有多少个. 我:似乎并不好做啊...看了题解后大雾... sol:考虑树上差分,对于一个点,在他那个位置++, ...
- 洛谷P3261 [JLOI2015]城池攻占(左偏树)
传送门 每一个城市代表的点开一个小根堆,把每一个骑士合并到它开始攻占的城池所代表的点上 然后开始dfs,每一次把子树里那些还活着的骑士合并上来 然后再考虑当前点的堆,一直pop直到骑士全死光或者剩下的 ...
随机推荐
- [DeeplearningAI笔记]序列模型3.7-3.8注意力模型
5.3序列模型与注意力机制 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.7注意力模型直观理解Attention model intuition 长序列问题 The problem of ...
- php 傻瓜式代码计算两个时间间隔
$stamp = (strtotime($_POST['start'])-strtotime($_POST['end'])); $s = $stamp%60; //秒 $m_stamp= ($stam ...
- 为VSCODE添加右键菜单
参考:https://blog.csdn.net/GreekMrzzJ/article/details/82194913 1.创建一个名为vscode.reg的空文本文件,填入下列内容 Windows ...
- FTP、SFTP文件下载内容校验
描述: 从FTP.SFTP下载的文件做MD5码校验,文件名和MD5码值存放在表格里,表格位置在FTP.SFTP服务器上. os模块只能遍历本地目录/文件,需要先连接FTP.SFTP服务器,将表格下载到 ...
- Mysql通过show processlist排查数据库执行慢
RDS for MySQL使用的是InnoDB引擎.不同于MyISAM引擎只提供表锁,InnoDB提供不同级别的锁.但是在我们日常的操作过程中经常由于对数据库不当的SQL操作导致出现长时间的锁,造成其 ...
- JavaScript定义类的几种方式
提起面向对象我们就能想到类,对象,封装,继承,多态.在<javaScript高级程序设计>(人民邮电出版社,曹力.张欣译.英文名字是:Professional JavaScript for ...
- Array和String测试与java.String.split
java.string.split() 存在于java.lang包中,返回值是一个数组. 作用是按指定字符或者正则去切割某个字符串,结果以字符串数组形式返回. 例 String [] toSort = ...
- WordPress友情链接插件的安装
插件:link manager 这样就安装成功! 在外观小工具里 把右边即可 这样在前台页面上就可看见添加的友情链接了!!!
- pycharts实现可视化
https://blog.csdn.net/u012535605/article/details/80677791http://pyecharts.org/#/zh-cn/prepare (中文官网 ...
- webgote的例子(6)SQL注入(盲注)
SQL Injection - Blind (WS/SOAP) 本期演示的是盲注的手法.有些网站在与数据库交互的地方进行了很好的修饰,将报错的语句进行修改,即使你找到了注入点也无法下手拿数据,这个时候 ...