倍增,延迟标记。

考虑一个$u$给他的哪几个祖先$v$贡献了$1$。越往上$dis(v,u)$越大,找到最远的一个还满足条件的$v$,$v$到$u$的父亲这条链上的答案都$+1$。延迟标记一下,然后从叶子节点往上走一遍求前缀和即可。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c))
{
x = x * + c - '';
c = getchar();
}
} int n;
long long a[];
int to[][];
long long len[][];
struct Edge
{
int v;
long long w;
}e[];
vector<int>G[]; int ans[]; int get(int x)
{
long long L=;
int res=x,y=x;
while()
{
int flag=;
for(int j=;j>=;j--)
{
if(len[res][j]==-) continue;
if(len[res][j]+L>a[y]) continue;
flag=; L=len[res][j]+L; res=to[res][j]; break;
}
if(flag==) break;
}
return res;
} void dfs(int x,int fa,int idx,int dep)
{
to[x][]=fa;
if(idx!=-) len[x][]=e[idx].w;
else len[x][]=-; for(int j=;j<=;j++)
{
if((<<j)>dep)
{
to[x][j]=-; len[x][j]=-;
continue;
} to[x][j]=to[to[x][j-]][j-];
len[x][j]=len[x][j-]+len[to[x][j-]][j-];
} int y=get(x);
if(y!=x)
{
if(to[x][]!=-) ans[to[x][]]++;
if(to[y][]!=-) ans[to[y][]]--;
} for(int i=;i<G[x].size();i++)
{
int id=G[x][i];
dfs(e[id].v,x,id,dep+);
}
} void F(int x)
{
for(int i=;i<G[x].size();i++)
{
int id=G[x][i];
F(e[id].v);
ans[x]+=ans[e[id].v];
}
} int main()
{
cin>>n;
for(int i=;i<=n;i++) cin>>a[i];
for(int i=;i<=n-;i++)
{
int fa; cin>>fa;
e[i].v=i+; cin>>e[i].w;
G[fa].push_back(i);
} dfs(,-,-,); F(); for(int i=;i<=n;i++) cout<<ans[i]<<" ";
cout<<endl; return ;
}

CodeForces 740D Alyona and a tree的更多相关文章

  1. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  2. CodeForces 682C Alyona and the Tree (树+dfs)

    Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...

  3. XJOI 3363 树4/ Codeforces 739B Alyona and a tree(树上差分+路径倍增)

    D. Alyona and a tree time limit per test  2 seconds memory limit per test  256 megabytes input  stan ...

  4. Codeforces 739B Alyona and a tree(树上路径倍增及差分)

    题目链接 Alyona and a tree 比较考验我思维的一道好题. 首先,做一遍DFS预处理出$t[i][j]$和$d[i][j]$.$t[i][j]$表示从第$i$个节点到离他第$2^{j}$ ...

  5. XJOI3363 树3/Codeforces 682C Alyona and the Tree(dfs)

    Alyona decided to go on a diet and went to the forest to get some apples. There she unexpectedly fou ...

  6. codeforces 682C Alyona and the Tree(DFS)

    题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...

  7. Codeforces 682C Alyona and the Tree (树上DFS+DP)

    题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...

  8. Codeforces 682C Alyona and the Tree

    题目链接:http://codeforces.com/problemset/problem/682/C 分析:存图,用dfs跑一遍,详细见注释 1 #include<iostream> 2 ...

  9. Codeforces 682C Alyona and the Tree(树形DP)

    题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...

随机推荐

  1. matlab向量的排序(自写函数)

    function a_ed = arraysort(a) %冒泡排序法 for i =1:length(a)-1 %进行多少次比较 for j=1+i:length(a) %每次求出最大的数,放在最后 ...

  2. 耗子学Python了(1)___Python环境配置

    一:写在开始的开始 最近我们团对的老大问我想看什么书,我让老大买了一本<Python 3面向对象编程>,所以在看的时候边敲边实践,决定在自己电脑配置下相应的环境,然后开始Python的学习 ...

  3. google protobuf序列化原理解析 (PHP示例)

    一.简介 Protocol Buffers是谷歌定义的一种跨语言.跨平台.可扩展的数据传输及存储的协议,因为将字段协议分别放在传输两端,传输数据中只包含数据本身,不需要包含字段说明,所以传输数据量小, ...

  4. 如果你也想写个完整的 Vue 组件项目

    1.一个完整的组件项目需要什么? 必要的: 组件构建方式 ( webpack / rollup 之类 ),并提供至少一个主流的输出格式 (ESModule) Demo 及 Demo 源码 文档,可以是 ...

  5. .net WebAPI返回xml、json格式

    WebAPI返回xml.json格式简单示例 using System.Net.Http.Formatting; public class TestController : ApiController ...

  6. js_读【javascript面向对象编程指南】笔记

    写在前面: 工欲善其事,必先利其器.编程的器,是前人总结的经验,常言道站在巨人的肩膀上开发,往往比自己另辟蹊径容易的多.经验藏于书,故有书中自有颜如玉,书中自有黄金屋,我也一度认为读书要花费很多时间, ...

  7. Java垃圾收集算法

    算法名称 过程 优缺点 1. 标记-清除算法 (Mark-Sweep) 分为两个阶段: 1.首先标记出所有需要回收的对象: 2.在标记完成后统一回收所有被标记的对象. 缺点: 1.效率问题:标记和清除 ...

  8. [Leetcode Week14]Maximum Binary Tree

    Maximum Binary Tree 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/maximum-binary-tree/description/ ...

  9. linux中的tasklet机制【转】

    转自:http://blog.csdn.net/yasin_lee/article/details/12999099 转自: http://www.kerneltravel.net/?p=143 中断 ...

  10. JS中类型检测方式

    在js中的类型检测目前我所知道的是三种方式,分别有它们的应用场景: 1.typeof:主要用于检测基本类型. typeof undefined;//=> undefined typeof 'a' ...