bzoj3011 [Usaco2012 Dec]Running Away From the Barn 左偏树
2024-08-28 19:23:14
题目传送门
https://lydsy.com/JudgeOnline/problem.php?id=3011
题解
复习一下左偏树板子。
看完题目就知道是左偏树了。
结果这个板子还调了好久。
大概已经不会写左偏树了。
时间复杂度 \(O(n\log n)\)。
#include<bits/stdc++.h>
#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back
template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b, 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b, 1 : 0;}
typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii;
template<typename I> inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
}
const int N = 200000 + 7;
#define lc c[0]
#define rc c[1]
int n; ll l;
int ans[N], rt[N];
ll dis[N];
struct Edge { int to, ne; ll w; } g[N << 1]; int head[N], tot;
inline void addedge(int x, int y, ll z) { g[++tot].to = y, g[tot].w = z, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y, ll z) { addedge(x, y, z), addedge(y, x, z); }
struct Node { int c[2], dis, s; ll v; } t[N];
inline int merge(int o, int p) {
if (!o || !p) return o ^ p;
if (t[o].v < t[p].v) std::swap(o, p);
t[o].rc = merge(t[o].rc, p);
if (t[t[o].lc].dis < t[t[o].rc].dis) std::swap(t[o].lc, t[o].rc);
t[o].dis = t[t[o].rc].dis + 1, t[o].s = t[t[o].lc].s + t[t[o].rc].s + 1;
return o;
}
inline void pop(int &o) { o = merge(t[o].lc, t[o].rc); }
inline void dfs(int x, int fa = 0) {
rt[x] = x, t[x].v = dis[x], t[x].s = 1;
for fec(i, x, y) if (y != fa) dis[y] = dis[x] + g[i].w, dfs(y, x), rt[x] = merge(rt[x], rt[y]);
while (t[rt[x]].v > dis[x] + l) pop(rt[x]);
ans[x] = t[rt[x]].s;
}
inline void work() {
for (int i = 1; i <= n; ++i) if (!dis[i]) dfs(i);
for (int i = 1; i <= n; ++i) printf("%d\n", ans[i]);
}
inline void init() {
read(n), read(l);
int x; ll y;
for (int i = 2; i <= n; ++i) read(x), read(y), adde(x, i, y);
}
int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}
最新文章
- IT软件技术人员的职位路线(从程序员到技术总监) - 部门管理经验谈
- gradle项目中如何支持java与scala混合使用?
- IIC总线
- SVN分支和映射总结和数据库初步使用
- Aoite 系列(03) - 一起来 Redis 吧!
- hibernate的多对多例子讲解(加图片)
- ASP.NET Web API 数据提供系统相关类型及其关系
- SqlServer 事务和异常处理示例
- randomize_va_space
- c++ 从标注输入流读取行
- 【BZOJ】【1834】【ZJOI2010】Network 网络扩容
- 字符编解码的故事–ASCII,ANSI,Unicode,Utf-8区别(转)
- UC浏览器插件开发
- JavaScript知识点总结
- BZOJ_1828_[Usaco2010 Mar]balloc 农场分配_线段树
- python中的集合
- ES6 Template Strings(转)
- MongoDB作为Windows服务来安装 错误1053:服务没有及时响应启动或控制请求
- 用jstl标签判断一个字符串是否包含了另一个字符串
- python迭代、列表生成式