bzoj3011
可并堆
复习一下可并堆
维护一个大跟堆,每次把节点儿子打上边权标记,然后合并,可并堆上维护一个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的更多相关文章
- 【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 ...
- [BZOJ3011][Usaco2012 Dec]Running Away From the Barn
题意 给出一棵以1为根节点树,求每个节点的子树中到该节点距离<=l的节点的个数 题解 方法1:倍增+差分数组 首先可以很容易的转化问题,考虑每个节点对哪些节点有贡献 即每次对于一个节点,找到其第 ...
- bzoj3011 可并堆
我们可以遍历得出每个节点到根节点的距离h,然后用可并堆进行维护.树形dp 我用的是pairing heap #include<cstdio> #include<algorithm&g ...
- bzoj3011 [Usaco2012 Dec]Running Away From the Barn 左偏树
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3011 题解 复习一下左偏树板子. 看完题目就知道是左偏树了. 结果这个板子还调了好久. 大概已 ...
- Usaco2012-2013 金组 题解 (暂缺Hill walk以及Figue eight)
https://files.cnblogs.com/files/Winniechen/usaco2012-2013.pdf 做的不是很好,还请见谅! 如果有什么疑问,可以QQ上找我. QQ号:1967 ...
随机推荐
- vue组件---动态组件之多标签页面
首先看下效果图 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> & ...
- oracle 清理跟踪文件.trc .trm
trc,trm文件介绍:trc:系统的跟踪文件(trace),当系统启动时或运行过程中出现错误时,系统会自动记录跟踪文件到指定的目录,以便于检查,这些文件需定期维护删除.trm:伴随着.trc文件产生 ...
- python3接口测试某个模块的很多接口有的用post有的用get
没啥好说的,啊哈哈 大神提示可以判断下用post还是get,但是加到哪里合适呢?仔细看认真看 耶耶耶
- kvm virsh命令详解
[root@ok home]# virsh list Id Name State ---------------------------------------------------- 1 13sv ...
- How to read and write multiple files in Python?
Goal: I want to write a program for this: In a folder I have =n= number of files; first read one fil ...
- PAT 1131 Subway Map
In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...
- PAT 1126 Eulerian Path
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similar ...
- 负载均衡之Ocelot+Consul(WebAPI注册服务)
上一篇 负载均衡之Ocelot+Consul(文件配置注册服务),介绍了如何通过json文件注册服务,本篇将学习如何通过web api 注册服务. 在展开学习过程之前,且先总结一下 consul服 ...
- Leetcode 76.最小覆盖子串
最小覆盖子串 给定一个字符串 S 和一个字符串 T,请在 S 中找出包含 T 所有字母的最小子串. 示例: 输入: S = "ADOBECODEBANC", T = "A ...
- SQL to MongoDB Mapping Chart
http://docs.mongodb.org/manual/reference/sql-comparison/ In addition to the charts that follow, you ...