洛谷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直到骑士全死光或者剩下的 ...
随机推荐
- SpringCloud学习(2)——Rest微服务案例
创建父工程: microservicecloud 创建公共模块api:microservicecloudapi SQL脚本: 此学习路线总共创建3个库, 分别为clouddb01, clouddb0 ...
- Python print "hello world" SyntaxError: invalid syntax
刚安装Python,在IDLE中输入print “Hello World”,谁知却发生错误: >>> print "Hello World"SyntaxError ...
- IIC总线学习
IIC总线 IIC协议简要说明: 1.2条双向串行线,一条数据线称为SDA,一条时钟线SCL,双向半双工 2.传输的设备之间只是简单的主从关系,主机可以作为主机发送也可以作为主机接收,任何时候只能由一 ...
- JAVA 企业培训
- 4163 hzwer与逆序对 (codevs + 权值线段树 + 求逆序对)
题目链接:http://codevs.cn/problem/4163/ 题目:
- 前端QRCode.js生成二维码(解决长字符串模块和报错问题)
QRCode 用法 1.使用npm安装到你的项目中 npm install qrcode2 --save 使用commonjs或者es6模块方式导入 var QRCode = require('qrc ...
- python基础之常用的高阶函数
前言 高阶函数指的是能接收函数作为参数的函数或类:python中有一些内置的高阶函数,在某些场合使用可以提高代码的效率. map() map函数可以把一个迭代对象转换成另一个可迭代对象,不过在pyth ...
- ip_rcv && ip_rcv_finish
(1) 在inet_init中注册了类型为ETH_P_IP协议的数据包的回调ip_rcv (2) 当二层数据包接收完毕,会调用netif_receive_skb根据协议进行向上层分发 (3) 类型为E ...
- linux的curl用法【转】
每分钟访问云签到任务执行页面.顺便记录了下curl的用法.以下内容摘自阮一峰博客. 一.查看网页源码 直接在curl命令后加上网址,就可以看到网页源码.我们以网址www.sina.com为例(选择该网 ...
- URAL题解三
URAL题解三 URAL 1045 题目描述:有\(n\)个机场,\(n-1\)条航线,任意两个机场有且只有一种方案联通.现有两个恐怖分子从\(m\)号机场出发,第一个人在机场安装炸弹,乘坐飞机,引爆 ...