B. Alyona and a tree

Posted planche

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

 

以上是关于B. Alyona and a tree的主要内容,如果未能解决你的问题,请参考以下文章

Alyona and a tree CodeForces - 739B (线段树合并)

CF739B Alyona and a tree 树上差分

CF739B Alyona and a tree

Alyona and a tree (树上倍增+差分)

D. Alyona and a tree 公式转换 + 分块暴力

Codeforces 682C Alyona and the Tree (树上DFS+DP)