Codeforces Round #381 (Div. 2) D dfs序+树状数组
2 seconds
256 megabytes
standard input
standard output
Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex i she wrote ai. Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges).
Let's define dist(v, u) as the sum of the integers written on the edges of the simple path from v to u.
The vertex v controls the vertex u (v ≠ u) if and only if u is in the subtree of v and dist(v, u) ≤ au.
Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex v what is the number of vertices u such that vcontrols u.
The first line contains single integer n (1 ≤ n ≤ 2·105).
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the integers written in the vertices.
The next (n - 1) lines contain two integers each. The i-th of these lines contains integers pi and wi (1 ≤ pi ≤ n, 1 ≤ wi ≤ 109) — the parent of the (i + 1)-th vertex in the tree and the number written on the edge between pi and (i + 1).
It is guaranteed that the given graph is a tree.
Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.
5
2 5 1 4 6
1 7
1 1
3 5
3 6
1 0 1 0 0
5
9 7 8 6 5
1 1
2 1
3 1
4 1
4 3 2 1 0
In the example test case the vertex 1 controls the vertex 3, the vertex 3 controls the vertex 5 (note that is doesn't mean the vertex 1controls the vertex 5).
题意:给你一棵树 有点权和边权 对于每个结点 若从当前点i到其子树中点j的边权之和小于等于j点权则表示i可以控制j点 计算每个结点能控制的点的个数
题解:div(i,j)<=a[j]
d[j]-d[i]<=a[j] d[j]代表j点到root的边权和
d[j]-a[j]<=d[i]
预处理出每个点 M[j].w=d[j]-a[j]
转化为i个子树中M[j].w<=d[i] 的点的个数
利用dfs序 将树转换为区间
利用树状数组计算每次查询 树状数组中存的是点的位置
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <map>
#define ll __int64
#define mod 1000000007
#define dazhi 2147483647
using namespace std;
ll n;
ll a[];
ll d[];
ll v[];
ll nedge=;
ll pre[];
ll in[];
ll out[];
ll tree[];
ll re[];
struct node
{
ll to,pre;
ll we;
}N[]; struct xx
{
ll w,pos;
}M[];
bool cmp1(struct xx aa,struct xx bb)
{
return aa.w<bb.w;
}
struct yy
{
ll l,r;
ll pos;
ll we;
}S[];
bool cmp2 (struct yy aa,struct yy bb)
{
return aa.we<bb.we;
}
void add1(ll from,ll to,ll w)
{
nedge++;
N[nedge].we=w;
N[nedge].to=to;
N[nedge].pre=pre[from];
pre[from]=nedge;
}
ll dfn=;
ll jishu=;
void getdfs(ll root,ll sum)
{
in[root]=++dfn;
d[root]=sum;
M[jishu].w=d[root]-a[root];
M[jishu].pos=dfn;
jishu++;
for(ll i=pre[root];i;i=N[i].pre)
{
sum+=N[i].we;
getdfs(N[i].to,sum);
sum-=N[i].we;
}
out[root]=dfn;
}
ll lowbit(ll xx)
{
return xx&(-xx);
}
void add2 (ll x,ll y)
{
for(ll i=x;i<=n;i+=lowbit(i))
tree[i]+=y;
}
ll getsum (ll x)
{
ll ans=;
for(ll i=x;i>=;i-=lowbit(i))
ans+=tree[i];
return ans;
}
int main()
{
memset(pre,,sizeof(pre));
scanf("%I64d",&n);
for(ll i=;i<=n;i++)
scanf("%I64d",&a[i]);
ll exm1,exm2;
for(ll i=;i<=n-;i++)
{
scanf("%I64d %I64d",&exm1,&exm2);
add1(exm1,i+,exm2);
}
getdfs(,);
for(ll i=;i<=n;i++)
{
S[i].l=in[i]+;
S[i].r=out[i];
S[i].pos=i;
S[i].we=d[i];
}
sort(M,M+jishu,cmp1);
sort(S+,S++n,cmp2);
ll start=;
for(ll i=;i<=n;i++)
{
while(start<jishu&&M[start].w<=S[i].we)
{
add2(M[start].pos,);
start++;
}
re[S[i].pos]=getsum(S[i].r)-getsum(S[i].l-);
}
for(ll i=;i<=n;i++)
printf("%I64d ",re[i]);
printf("\n");
return ;
}
Codeforces Round #381 (Div. 2) D dfs序+树状数组的更多相关文章
- Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数
E. Infinite Inversions ...
- 【Codeforces Round #433 (Div. 1) C】Boredom(树状数组)
[链接]h在这里写链接 [题意] 给你一个n*n的矩阵. 其中每一列都有一个点. 任意两个点构成了矩形的两个对角点 ->即任意两个点确定了一个矩形. ->总共能确定n*(n-1)/2个矩形 ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- HDU 3887:Counting Offspring(DFS序+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )
一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...
- 【bzoj3881】[Coci2015]Divljak AC自动机+树链的并+DFS序+树状数组
题目描述 Alice有n个字符串S_1,S_2...S_n,Bob有一个字符串集合T,一开始集合是空的. 接下来会发生q个操作,操作有两种形式: “1 P”,Bob往自己的集合里添加了一个字符串P. ...
- [BZOJ1103][POI2007]大都市meg dfs序+树状数组
Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...
随机推荐
- vue-router爬坑记
简介 因为我们用Vue开发的页面是单页面应用,就相当于只有一个主的index.html,这时候我们就不能使用a标签来进行页面的切换了,所以这时候我们今天的主角Vue-Router就闪亮的登场了 Vue ...
- 376. Binary Tree Path Sum【LintCode java】
Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...
- 使用清华镜像在python中pip 安装
Anaconda的安装步骤不在本文的讨论中,我们主要是学习一下如何配置conda的镜像,以及一些问题的解决过程 配置镜像 在conda安装好之后,默认的镜像是官方的,由于官网的镜像在境外,我们使用国内 ...
- mac上golang编译出现clang错误
错误现象 几周前,突然发现我的go 项目编译开始报一种以前从来没有出现过的错误: # runtime/cgo clang: warning: argument unused during compil ...
- 使用Response.Write实现在页面的生命周期中前后台的交互
Response.Write()方法非常的常见,也很普通,就是向http output中输出一string.其输出的内容位于页面的最顶端,常用来实现显示一些页面消息框等逻辑. 一般来说,在页面的整个生 ...
- POJ 1696 Space Ant(凸包变形)
Description The most exciting space discovery occurred at the end of the 20th century. In 1999, scie ...
- 软件工程课堂作业(一)——随机产生四则运算题目(C++)
一.设计思想: 1.首先主函数只用来调用随机产生并输出运算题目函数,随机产生并输出这一部分功能用一个randout函数实现: 2.随机产生运算数这一功能,两个运算数可以用随机函数生成,并将它们控制在1 ...
- servlet转发和重定向
1 转发 RequestDispatcher getRequestDispatcher(java.lang.String path) 1)地址栏不会改变 2)转发只能转发到当前web应用内的资源 3) ...
- vue服务端渲染axios预取数据
首先是要参考vue服务端渲染教程:https://ssr.vuejs.org/zh/data.html. 本文主要代码均参考教程得来.基本原理如下,拷贝的原文教程. 为了解决这个问题,获取的数据需要位 ...
- IntelliJ IDEA Maven引入