倍增,延迟标记。

考虑一个$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. .NET类型转型的四种做法(转)

    .NET类型转型的四种做法: ◆ 强制转型:(int)变量名称 ◆ int.Parse(字符串变量名称) ◆ Convert.To类型(变量名称) ◆ TryParse 强制转型 (casting) ...

  2. linux用户管理和文件权限

    linux用户管理和文件权限 新建用户:useradd ftpuser      useradd -g gxx userxx修改密码:passwd ftpuser新增用户组:# groupadd gr ...

  3. 51Nod 1347 旋转字符串 | 规律

    Input示例 aa ab Output示例 YES NO 规律:abcabc 只需判断原始字符串 #include <bits/stdc++.h> using namespace std ...

  4. java enum用法

    基本用法 enum Day { SUNDAY, MONDAY, TUESDAY, WENDSDAY, THURSDAY, FRIDAY, SATURDAY; } 枚举是常量,所以应该用大写. 枚举是对 ...

  5. 计蒜客 Goldbach Miller_Rabin判别法(大素数判别法)

    题目链接:https://nanti.jisuanke.com/t/25985 题目: Description: Goldbach's conjecture is one of the oldest ...

  6. 完全背包问题入门 (dp)

    问题描述: 有n种重量和价值分别为Wi,Vi的物品,从这些中挑选出总重量不超过W的物品,求出挑选物品的价值总和的最大值,每种物品可以挑选任意多件. 分析: 令dp[i+1][j]表示从前i件物品中挑选 ...

  7. python初步学习-python数据类型之strings(字符串)

    数据类型-字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号(''或者"")来创建字符串 var1 = 'Hello World!' var2 = "P ...

  8. Django 1.10中文文档-第一个应用Part5-测试

    本教程上接教程Part4. 前面已经建立一个网页投票应用,现在将为它创建一些自动化测试. 自动化测试简介 什么是自动化测试 测试是检查你的代码是否正常运行的行为.测试也分为不同的级别.有些测试可能是用 ...

  9. Reactor与Proactor区别

    如网络编程中accept之后等待数据到达,并且读取数据为例: Reactor: 基于同步IO 1. 线程等待读取socket数据,将socketfd添加到事件分派器中,如添加到epoll: 2. 事件 ...

  10. 【字符串处理算法】字符串包含的算法设计及C代码实现【转】

    转自:http://blog.csdn.net/zhouzhaoxiong1227/article/details/50679587 版权声明:本文为博主原创文章,对文章内容有任何意见或建议,欢迎与作 ...