CodeForces 740D Alyona and a tree
倍增,延迟标记。
考虑一个$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的更多相关文章
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- CodeForces 682C Alyona and the Tree (树+dfs)
Alyona and the Tree 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/C Description Alyona ...
- 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 ...
- Codeforces 739B Alyona and a tree(树上路径倍增及差分)
题目链接 Alyona and a tree 比较考验我思维的一道好题. 首先,做一遍DFS预处理出$t[i][j]$和$d[i][j]$.$t[i][j]$表示从第$i$个节点到离他第$2^{j}$ ...
- 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 ...
- codeforces 682C Alyona and the Tree(DFS)
题目链接:http://codeforces.com/problemset/problem/682/C 题意:如果点v在点u的子树上且dist(u,v)>a[v]则u和其整个子树都将被删去,求被 ...
- Codeforces 682C Alyona and the Tree (树上DFS+DP)
题目链接:http://codeforces.com/problemset/problem/682/C 题目大意:取树上任意一个点v,若点v的子树中有一个点u使得dist(v,u)>a[u]那么 ...
- Codeforces 682C Alyona and the Tree
题目链接:http://codeforces.com/problemset/problem/682/C 分析:存图,用dfs跑一遍,详细见注释 1 #include<iostream> 2 ...
- Codeforces 682C Alyona and the Tree(树形DP)
题目大概说给一棵点有权.边也有权的树.一个结点v不高兴当且仅当存在一个其子树上的结点u,使得v到u路径上的边权和大于u的权值.现在要不断地删除叶子结点使得所有结点都高兴,问最少删几个叶子结点. 一开始 ...
随机推荐
- ZooKeeper翻译(一)
欢迎来到Apache ZooKeeper的世界 Apache Zookeeper是一个为了开发和维护一个开源的服务的一个尝试,这个服务使高可用的分布式协作成为可能. ZooKeeper是什么? Zoo ...
- bzoj 2038 莫队入门
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 题意:多次询问区间内取出两个相同颜色的种类数 思路:由于不是在线更新,那么可以进行离线查询,而 ...
- windows 安装elk日志系统
1.前往https://www.elastic.co官网下载对应的elasticsearch .kibana和logstash他们的版本号一致. 2.elasticsearch 解压后前往bin文件下 ...
- java在不同系统有不同的换行符
//从当前系统中获取换行符,默认是"\n" String lineSeparator = System.getProperty("line.separator" ...
- 【vijos】P1066 弱弱的战壕
[算法]线段树 [题解]将所有坐标按x(第一)和y(第二)从小到大排序,再按顺序插入线段树,即在线段树中将y坐标位置+1,这样就能保证每个坐标能包含的点一定先被处理了,每次询问查询1...a[i].y ...
- Spring的使用优点
spring事物配置,声明式事务管理和基于@Transactional注解的使用 spring支持编程式事务管理和声明式事务管理两种方式. 编程式事务管理使用TransactionTemplate或者 ...
- [Unity]模拟雨水的折射效果
用GrabPass做的小玩具. 并不是真的计算了折射,只是简单地扰动了uv,对于雨水来说效果已经足够好了. Shader代码: Shader "Unlit/Rain" { Prop ...
- linux安装(Ubuntu)——(二)
centos的安装参考: http://www.runoob.com/linux/linux-install.html Linux 安装(Ubuntu) 虚拟机:虚拟机(Virtual Machine ...
- Ubuntu终端里面显示路径名称太长,怎么设置变短【转】
转自:http://blog.csdn.net/id19870510/article/details/8276914 $: sudo vi ~/.bashrc 这个文件记录了用户终端配置 找到 if ...
- ue4.3正式版源码链接
ue4.3正式版源码链接 http://tieba.baidu.com/p/3170253742