可并堆

复习一下可并堆

维护一个大跟堆,每次把节点儿子打上边权标记,然后合并,可并堆上维护一个size,每次把大于l的弹出,size就是答案

程序中那个d写l和r速度差不多,我写l是表示右儿子到u的最长距离

#include<bits/stdc++.h>
using namespace std;
const int N = ;
struct edge {
int to;
long long w;
edge(int to = , long long w = ) : to(to), w(w) {}
};
int n;
long long L;
long long dis[N], tag[N];
int root[N], l[N], r[N], d[N], ans[N], size[N];
vector<edge> G[N];
void pushdown(int x)
{
if(tag[x] == ) return;
tag[l[x]] += tag[x];
dis[l[x]] += tag[x];
tag[r[x]] += tag[x];
dis[r[x]] += tag[x];
tag[x] = ;
}
int merge(int u, int v)
{
if(!u) return v;
if(!v) return u;
pushdown(u);
pushdown(v);
if(dis[u] < dis[v]) swap(u, v);
r[u] = merge(r[u], v);
if(d[r[u]] > d[l[u]]) swap(l[u], r[u]);
d[u] = d[l[u]] + ;
size[u] = size[l[u]] + size[r[u]] + ;
return u;
}
void dfs(int u)
{
root[u] = u;
size[u] = ;
for(int i = ; i < G[u].size(); ++i)
{
edge e = G[u][i];
dfs(e.to);
tag[root[e.to]] += e.w;
dis[root[e.to]] += e.w;
root[u] = merge(root[u], root[e.to]);
}
while(dis[root[u]] > L && root[u]) root[u] = merge(l[root[u]], r[root[u]]);
ans[u] = size[root[u]];
}
int main()
{
scanf("%d%lld", &n, &L);
for(int i = ; i <= n; ++i)
{
int u;
long long len;
scanf("%d%lld", &u, &len);
G[u].push_back(edge(i, len));
}
dfs();
for(int i = ; i <= n; ++i) printf("%d\n", ans[i]);
return ;
}

bzoj3011的更多相关文章

  1. 【BZOJ3011】[Usaco2012 Dec]Running Away From the Barn 可并堆

    [BZOJ3011][Usaco2012 Dec]Running Away From the Barn Description It's milking time at Farmer John's f ...

  2. [BZOJ3011][Usaco2012 Dec]Running Away From the Barn

    题意 给出一棵以1为根节点树,求每个节点的子树中到该节点距离<=l的节点的个数 题解 方法1:倍增+差分数组 首先可以很容易的转化问题,考虑每个节点对哪些节点有贡献 即每次对于一个节点,找到其第 ...

  3. bzoj3011 可并堆

    我们可以遍历得出每个节点到根节点的距离h,然后用可并堆进行维护.树形dp 我用的是pairing heap #include<cstdio> #include<algorithm&g ...

  4. bzoj3011 [Usaco2012 Dec]Running Away From the Barn 左偏树

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3011 题解 复习一下左偏树板子. 看完题目就知道是左偏树了. 结果这个板子还调了好久. 大概已 ...

  5. Usaco2012-2013 金组 题解 (暂缺Hill walk以及Figue eight)

    https://files.cnblogs.com/files/Winniechen/usaco2012-2013.pdf 做的不是很好,还请见谅! 如果有什么疑问,可以QQ上找我. QQ号:1967 ...

随机推荐

  1. elk大纲

    一.ELK功能概览 1.检索 2.数据可视化--实时监控(实时刷新) nginx 访问量 ip地区分布图(大数据) 3.zabbix 微信联动报警 4.大数据日志分析平台(基于hadoop) 二.ka ...

  2. pringboot开启找回Run Dashboard

    代码中加入 <option name="configurationTypes"> <set> <option value="SpringBo ...

  3. FileWriter实现从一个文件中读取内容并写到另一个文件中

    FileWriter和FileOutputStream都是向文件写内容,区别是前台一次写一个字符,后者一次写一个字节 package com.janson.day20180827; import ja ...

  4. Python supprocess模块

    当我们需要调用系统的命令的时候,最先考虑的os模块.用os.system()和os.popen()来进行操作.但是这两个命令过于简单,不能完成一些复杂的操作,如给运行的命令提供输入或者读取命令的输出, ...

  5. SocketServer 网络服务框架

    SocketServer简化了网络服务器的编写.它有4个类:TCPServer,UDPServer,UnixStreamServer,UnixDatagramServer.这4个类是同步进行处理的,另 ...

  6. TestNG常用注解

    原文链接:https://www.yiibai.com/testng/basic-annotations.html    以下是TestNG支持的注释列表: 注解 描述 @BeforeSuite 在该 ...

  7. unigui的ini文件读写【6】

    procedure THeaderFooterForm.writerParas; var IniFile : TIniFile; begin try IniFile:=TIniFile.Create( ...

  8. R - Milking Time DP

    Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that sh ...

  9. H - Tickets

    Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this is really a ...

  10. ECMAScript 6 入门学习笔记(二)——变量的解构赋值

    一.数组的解构赋值 let [foo, [[bar], baz]] = [1, [[2], 3]]; ①可多不可少,等号的右边是数组 let [x, y] = [1, 2, 3]; //可以 let ...