倍增,延迟标记。

考虑一个$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. centos6.8使用脚本一键搭建apache+svn服务

    服务器环境: 脚本如下: #!/bin/bash yum install wget -y mv /etc/yum.repos.d/*.repo /tmp wget -O /etc/yum.repos. ...

  2. Flume入门——Selector、Chanel等

    1.selector (http://blog.csdn.net/looklook5/article/details/40430965) (http://blog.csdn.net/xiao_jun_ ...

  3. CSS哪些样式属性可以继承

    不可继承的:display.margin.border.padding.background.height.min-height.max- height.width.min-width.max-wid ...

  4. CSS浮动为什么不会遮盖同级元素

    1.问题描述 在W3CSchool学习web前端时,看完CSS定位-浮动这一节后,感觉没有什么问题.但是在CSS高级-分类这一节的中进行实践时,遇到了如下问题.测试地址:浮动的简单应用. 完整的htm ...

  5. D题 hdu 1412 {A} + {B}

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1412 {A} + {B} Time Limit: 10000/5000 MS (Java/Others ...

  6. Arduino 舵机sg90电位器实现转动方向控制

    /* Sweep*/ #include <Servo.h> int potpin = 0;//电位器接到A0 int val; //存储电位器读取的数值 Servo myservo//定义 ...

  7. Java多线程学习(六)Lock锁的使用

    系列文章传送门: Java多线程学习(二)synchronized关键字(1) Java多线程学习(二)synchronized关键字(2) Java多线程学习(三)volatile关键字 Java多 ...

  8. 哈希表(一):解决hash冲突的几种方法

    (一)线性探测法 线性探测法是最简单的处理冲突的方法. (1)插入元素:插入元素时,如果发生冲突,算法将从该槽位向后遍历哈希表,直到找到表中的下一个空槽,并将该值放入到空槽当中. (2)查找元素:查找 ...

  9. python科学计算整理

    网站: http://bokeh.pydata.org/gallery.html

  10. Educational Codeforces Round 23 补题小结

    昨晚听说有教做人场,去补了下玩. 大概我的水平能做个5/6的样子? (不会二进制Trie啊,我真菜) A. 傻逼题.大概可以看成向量加法,判断下就好了. #include<iostream> ...