• codeforces 381 D Alyona and a tree(倍增)(前缀数组)


    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 that v 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 1 controls the vertex 5).

    【分析】题意就不说了,说下怎么做。很显然暴力肯定超时。对于每一个节点,用倍增的思想,fa[i][x]、cost[i][x]数组分别记录x节点往上第2的i次方个祖先编号和与那个祖先的距离。如果节点x对节点f有一个贡献,那么x对处于x和f之间的节点也应该有一个贡献,所以用前缀数组的思想,ans[x]++,ans[f]--,然后只需要在dfs的时候一个一个累加就行了。

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #define inf 0x3f3f3f3f
    #define met(a,b) memset(a,b,sizeof a)
    #define pb push_back
    typedef long long ll;
    using namespace std;
    const int N = 2e5+50;
    int n,m,k,u;
    int fa[20][N];
    ll cost[20][N];
    ll a[N],ans[N];
    struct man{
        int son;
        ll co;
    };
    vector<man>edg[N];
    void dfs(int x){
        for(int i=1;fa[i-1][fa[i-1][x]];i++){
          fa[i][x]=fa[i-1][fa[i-1][x]];
          cost[i][x]=cost[i-1][x]+cost[i-1][fa[i-1][x]];
        }
        for(int i=0;i<edg[x].size();i++){
            man e=edg[x][i];
            int v=e.son;ll c=e.co;
            fa[0][v]=x;cost[0][v]=c;
            dfs(v);
        }
    }
    int Find(int x){
        ll c=a[x];
        for(int i=19;i>=0;i--){
            if(cost[i][x]<=c&&fa[i][x]){
                c-=cost[i][x];
                x=fa[i][x];
            }
        }
        return x;
    }
    void sum_dfs(int x){
        for(int i=0;i<edg[x].size();i++){
            man e=edg[x][i];
            int v=e.son;ll c=e.co;
            sum_dfs(v);
            ans[x]+=ans[v];
        }
    }
    int main (){
        ll c;
        met(ans,0);
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
        for(int i=2;i<=n;i++){
            scanf("%d%lld",&u,&c);
            man s;s.co=c;s.son=i;
            edg[u].push_back(s);
        }
        dfs(1);
        for(int i=1;i<=n;i++){
            int f=Find(i);
            ans[fa[0][f]]--;
            ans[fa[0][i]]++;
        }
        sum_dfs(1);
        for(int i=1;i<=n;i++)printf("%lld ",ans[i]);printf("
    ");
        return 0;
    }
  • 相关阅读:
    BZOJ 3150 [Ctsc2013]猴子 ——期望DP 高斯消元
    BZOJ 4569 [Scoi2016]萌萌哒 ——ST表 并查集
    BZOJ 4590 [Shoi2015]自动刷题机 ——二分答案
    BZOJ 3462 DZY Loves Math II ——动态规划 组合数
    BZOJ 4827 [Shoi2017]分手是祝愿 ——期望DP
    BZOJ 4827 [Hnoi2017]礼物 ——FFT
    BZOJ 4826 [Hnoi2017]影魔 ——扫描线 单调栈
    ZOJ 3874 Permutation Graph ——分治 NTT
    UVA 12633 Super Rooks on Chessboard ——FFT
    HDU 2065 "红色病毒"问题 ——快速幂 生成函数
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6117471.html
Copyright © 2020-2023  润新知