bzoj3011: [Usaco2012 Dec]Running Away From the Barn 可并堆(左偏树)
来源:互联网 发布:开淘宝店一件代发 编辑:程序博客网 时间:2024/06/10 06:27
题意:给出以1号点为根的一棵有根树,问每个点的子树中与它距离小于l的点有多少个。n<=2e5,l<=1e18
随手抽了一道,以为是个水题,刷水不成反被刷。
一开始觉得这题不难,然后样例老过不去,然后看zyf2000的代码。。还是错的,最后直接拿她的交了一下,结果是错的。。一怒之下删掉重写。。
开始想的是倍增什么的,但是好像不好动态维护,而且复杂度好大。。
然后这就只能可并堆咯,堆维护每个子树内距离不超过L的点,从下往上走,走完一个子树就合并一波,然后动态把距离超过L的删掉就可以了。
很久没打左偏树,好丑。。
需要注意的是d[i]是左偏树的dist,dis[i]则是i到根的距离,刚好与左偏树的优先级是相同的。
记录lazy[x]来传递权值。f[x]表示x所在的堆的top。
#include<cstdio>#include<algorithm>#include<cstring>#define fo(i,a,b) for(int i=a;i<=b;i++)#define fd(i,a,b) for(int i=a;i>=b;i--)using namespace std;const int N=4e5+5;typedef long long ll;int head[N],next[N],go[N],d[N];int ans[N],n;ll dis[N],lazy[N],val[N],L;int ls[N],rs[N],f[N],size[N],tot;inline void add(int x,int y,ll z){ go[++tot]=y; val[tot]=z; next[tot]=head[x]; head[x]=tot;}inline void solve(int x,ll v){ if (!x)return; dis[x]+=v; lazy[x]+=v;}inline void pushup(int x){ size[x]=size[ls[x]]+size[rs[x]]+1;}inline void pushdown(int x){ if (lazy[x])solve(ls[x],lazy[x]),solve(rs[x],lazy[x]),lazy[x]=0;}inline int merge(int x,int y){ if (!x)return y; if (!y)return x; if (dis[x]<dis[y])swap(x,y); pushdown(x); rs[x]=merge(rs[x],y); if (d[ls[x]]<d[rs[x]])swap(ls[x],rs[x]); d[x]=d[rs[x]]+1; pushup(x); return x;}inline void pop(int x){ pushdown(f[x]); f[x]=merge(ls[f[x]],rs[f[x]]);}inline void dfs(int x,int fa){ f[x]=x,d[x]=size[x]=1; for(int i=head[x];i;i=next[i]) { int v=go[i]; if (v!=fa) { dfs(v,x); solve(f[v],val[i]); while (dis[f[v]]>L)pop(v); f[x]=merge(f[x],f[v]); } } ans[x]=size[f[x]];}int main(){ scanf("%d%lld",&n,&L); fo(i,2,n) { int x; ll z; scanf("%d%lld",&x,&z); add(x,i,z); add(i,x,z); } dfs(1,0); fo(i,1,n)printf("%d\n",ans[i]);}
阅读全文
0 0
- bzoj3011: [Usaco2012 Dec]Running Away From the Barn 可并堆(左偏树)
- 【BZOJ3011】[Usaco2012 Dec]Running Away From the Barn【可并堆】
- [BZOJ3011][Usaco2012 Dec]Running Away From the Barn(可并堆)
- BZOJ3011: [Usaco2012 Dec]Running Away From the Barn 可并堆
- bzoj3011 [Usaco2012 Dec]Running Away From the Barn (可并堆)
- 【bzoj3011】[Usaco2012 Dec]Running Away From the Barn
- BZOJ 3011 Usaco2012 Dec Running Away From the Barn 可并堆
- [可并堆] BZOJ 3011 [Usaco2012 Dec]Running Away From the Barn
- bzoj 3011: [Usaco2012 Dec]Running Away From the Barn (可并堆)
- 【BZOJ 3011】[Usaco2012 Dec]Running Away From the Barn 可并堆
- 3011: [Usaco2012 Dec]Running Away From the Barn
- bzoj-3011 Running Away From the Barn
- 可并堆?左偏树?
- bzoj3012 [Usaco2012 Dec]First!
- 【bzoj3012】[Usaco2012 Dec]First!
- 左偏树(可并堆)
- HDU1512 左偏树(可并堆)
- 可并堆之左偏树
- leetcode11. Container With Most Water
- JSONObject与JSONArray的使用
- css3之天猫侧边栏 (飞动效果)
- 迁移学习-PixelDA-Unsupervised Pixel–Level Domain Adaptation with Generative Adversarial Networks
- ViewController自定义转场-基础
- bzoj3011: [Usaco2012 Dec]Running Away From the Barn 可并堆(左偏树)
- 使用rinetd作为阿里云redis访问代理
- 机器学习十大算法之随机森林算法
- Eigen教程1
- http协议学习-请求头Request Headers
- schedule 详解
- 机器学习十大算法之朴素贝叶斯
- xml文件中的红叉号问题
- Python爬虫(三)Urllib库的高级用法