B. Alyona and a tree

二分+dfs序+前缀和+瞎几把yy

 

#include "a.h"

int n;
aLL a;
struct edge
{
  int to, nx, w;
} e[N << 1];
arr fi;
int ce = 1;
aLL sum, ans;
aLL vec, up, down, res;
ll tot = 1;
ll rs = 0;

inline void add(int u, int v, int w)
{
  e[++ce] = edge{v, fi[u], w};
  fi[u] = ce;
}

void dfs(int x)
{
  if (fi[x] == -1)
  {
    rs = res[x];
    rs += up[x], rs -= down[x];
    ans[x] = rs;
    return;
  }
  for (int i = fi[x]; i != -1; i = e[i].nx)
  {
    int v = e[i].to, w = e[i].w;
    sum[++tot] = sum[tot - 1] + w;
    int p = lower_bound(sum + 1, sum + 1 + tot, sum[tot] - a[v]) - sum;
    if (tot - 1 >= p)
      up[vec[tot - 1]]++, down[vec[p - 1]]++;
    vec[tot] = v;
    dfs(v);
    tot--;
    res[x] += rs;
    rs = 0;
  }
  rs = res[x];
  rs += up[x], rs -= down[x];
  ans[x] = rs;
}

int main()
{
  // file("test");
  while (~scanf("%d", &n))
  {
    me(fi, -1);
    For(i, 1, n) sdf(a[i]);
    For(i, 2, n)
    {
      int x, y;
      sdf(x), sdf(y);
      add(x, i, y);
    }
    vec[1] = 1;
    sum[1] = 0;
    dfs(1);
    For(i, 1, n)
        printf("%d ", ans[i]);
  }
  return 0;
}

 

转载于:https://www.cnblogs.com/planche/p/9503352.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值