D. Alyona and a tree
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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 thatv controls u.

Input

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.

Output

Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.

Examples
input
5
2 5 1 4 6
1 7
1 1
3 5
3 6
output
1 0 1 0 0
input
5
9 7 8 6 5
1 1
2 1
3 1
4 1
output
4 3 2 1 0
Note

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).

题意:n个点的一棵树,每个点有一个权值,满足一个条件,点v的子树中u的个数,条件是u->v的最短路径权值和<=a[u];

思路:dfs序写法:

dis[i]为根->i的最短距离,条件->    dis[u]-dis[v]<=a[u];

    将条件转化一下:    dis[v]>=dis[u]-a[u];

    可以预处理出b[u]=dis[u]-a[u],从而转化为子树问题,子树中b[u]小于等于dis[v]的个数;

    树上二分写法:

    将条件转化一下:    dis[v]>=dis[u]-a[u];

    由于每个点的权值都是非负数,所以dis是单调递增的,二分dis,找到该点,前缀和处理;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x) cout<<"bug"<<x<<endl;
const int N=2e5+,M=1e6+,inf=;
const ll INF=1e18+,mod=;
struct is
{
int v,next;
ll w;
}edge[N<<];
int head[N],edg;
int in[N],out[N],tot;
ll dis[N],a[N];
void init()
{
memset(head,-,sizeof(head));
edg=;
tot=;
}
void add(int u,int v,ll w)
{
edg++;
edge[edg].v=v;
edge[edg].w=w;
edge[edg].next=head[u];
head[u]=edg;
}
void dfs(int u,int fa,ll val)
{
tot++;
in[u]=tot;
dis[u]=val;
for(int i=head[u];i!=-;i=edge[i].next)
{
int v=edge[i].v;
ll w=edge[i].w;
if(v==fa)continue;
dfs(v,u,val+w);
}
out[u]=tot;
}
struct treearray
{
int tree[N];
void init()
{
memset(tree,,sizeof(tree));
}
int lowbit(int x)
{
return x&-x;
}
void update(int x,int c)
{
while(x<N)
{
tree[x]+=c;
x+=lowbit(x);
}
}
int query(int x)
{
int sum=;
while(x)
{
sum+=tree[x];
x-=lowbit(x);
}
return sum;
}
};
struct p
{
ll x;
int pos;
bool operator <(const p &a)const
{
return x<a.x;
}
}b[N];
struct q
{
int l,r,pos;
ll x;
bool operator <(const q &a)const
{
return x<a.x;
}
}q[N];
int ans[N];
int main()
{
init();
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
for(int i=;i<=n;i++)
{
int v;ll w;
scanf("%d%lld",&v,&w);
add(v,i,w);
add(i,v,w);
}
dfs(,-,0LL);
for(int i=;i<=n;i++)
b[in[i]].x=dis[i]-a[i],b[i].pos=i;
//for(int i=1;i<=n;i++)
//cout<<in[i]<<" "<<out[i]<<endl;
//cout<<"yyyy"<<endl;
sort(b+,b++n);
for(int i=;i<=n;i++)
{
q[i].l=in[i];
q[i].r=out[i];
q[i].pos=i;
q[i].x=dis[i];
}
sort(q+,q++n);
//for(int i=1;i<=n;i++)
//cout<<b[i].x<<" "<<b[i].pos<<endl;
//cout<<"zzzzz"<<endl;
int st=;
treearray tree;
tree.init();
for(int i=;i<=n;i++)
{
//cout<<q[i].l<<" "<<q[i].r<<" "<<q[i].x<<" "<<q[i].pos<<endl;
while(st<=n&&q[i].x>=b[st].x)
{
tree.update(b[st].pos,);
st++;
}
ans[q[i].pos]=tree.query(q[i].r)-tree.query(q[i].l);
}
for(int i=;i<=n;i++)
printf("%d ",ans[i]);
return ;
}

Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组的更多相关文章

  1. Codeforces Round #381 (Div. 1) B. Alyona and a tree dfs序 二分 前缀和

    B. Alyona and a tree 题目连接: http://codeforces.com/contest/739/problem/B Description Alyona has a tree ...

  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 ...

  3. 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 ...

  4. Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)

    http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...

  5. Codeforces Round #381 (Div. 2) D. Alyona and a tree 树上二分+前缀和思想

    题目链接: http://codeforces.com/contest/740/problem/D D. Alyona and a tree time limit per test2 secondsm ...

  6. Codeforces Round #381 (Div. 2)D. Alyona and a tree(树+二分+dfs)

    D. Alyona and a tree Problem Description: Alyona has a tree with n vertices. The root of the tree is ...

  7. Codeforces Round #358 (Div. 2) C. Alyona and the Tree dfs

    C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  8. Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组

    E. George and Cards   George is a cat, so he loves playing very much. Vitaly put n cards in a row in ...

  9. Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)

    题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出  ...

随机推荐

  1. 【BZOJ1713】[Usaco2007 China]The Bovine Accordion and Banjo Orchestra 音乐会 斜率优化

    [BZOJ1713][Usaco2007 China]The Bovine Accordion and Banjo Orchestra 音乐会 Description Input 第1行输入N,之后N ...

  2. 【Android】TextView动态设置android:drawableLeft|Right|Top|Bottom,SetColor

    Android中有时需动态设置控件四周的drawble图片,这个时候就需要调用 setCompoundDrawables(left, top, right, bottom),四个参数类型都是drawa ...

  3. redis运维常用的server端命令

    TIME 查看时间戳与微秒数 DBSIZE 查看当前库中的key数量 BGREWRITEAOF 后台进程重写AOF BGSAVE 后台保存rdb快照 SAVE 保存rdb快照 LASTSAVE 上次保 ...

  4. Oracle正在执行和执行过的SQL语句

    1.正在执行的SQL select a.username, a.sid,b.SQL_TEXT, b.SQL_FULLTEXT from v$session a, v$sqlarea b where a ...

  5. 系统性能优化- Session丢失

    最近在做项目的过程中,客户经常反馈在操作的时候进场会突然跳转到登录页面. 先描述下系统情况: 操作系统:WindowsServer 2008 .NET版本:.NET 4.0/ASP.NET 4.0/A ...

  6. appfog 添加数据库支持

    1.PhpMyAdmin与app 在同一应用 1.cd进入应用所在的文件夹,输入 git clone git://github.com/appfog/af-php-myadmin.git 2.进入本地 ...

  7. 【Loadrunner】性能测试报告实战

    一.一份好的性能测试报告需要遵循什么规则? 好的报告只需要遵循3点即可:清晰的结构.简要的语言以及数据的对比. 二.如何用Loadrunner自动到处HTML以及word版的报告? 1.导出html格 ...

  8. (1.5)DML增强功能-try catch及事务控制

    一.事务控制与Try Catch结合 当 SET XACT_ABORT 为 ON 时,如果执行 Transact-SQL 语句产生运行时错误,则整个事务将终止并回滚. 当 SET XACT_ABORT ...

  9. Redis持久化磁盘IO方式及其带来的问题

    有Redis线上运维经验的人会发现Redis在物理内存使用比较多,但还没有超过实际物理内存总容量时就会发生不稳定甚至崩溃的问题 一.对Redis持久化的探讨与理解 redis是一个支持持久化的内存数据 ...

  10. HDU5139:Formula(找规律+离线处理)

    http://acm.hdu.edu.cn/showproblem.php?pid=5139 Problem Description f(n)=(∏i=1nin−i+1)%1000000007You ...