• ZOJ 2866 Overstaffed Company


    树状数组

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    #include<vector>
    using namespace std;
    
    const int maxn = 1000000 + 10;
    vector<int>Tree[50000 + 10];
    int n;
    int val[50000 + 10];
    int c[maxn];
    int ans[50000 + 10];
    
    int lowbit(int x)
    {
        return x & (-x);
    }
    
    void Update(int x, int add)
    {
        while (x < maxn)
        {
            c[x] += add;
            x += lowbit(x);
        }
    }
    
    int Get(int x)
    {
        int ret = 0;
        while (x != 0)
        {
            ret += c[x];
            x -= lowbit(x);
        }
        return ret;
    }
    
    void init()
    {
        for (int i = 0; i <= n; i++) Tree[i].clear();
        memset(c, 0, sizeof c);
    }
    
    void DFS(int now)
    {
        int A, B;
        A = Get(1000000 + 1) - Get(val[now]);
        for (int i = 0; i < Tree[now].size(); i++)
            DFS(Tree[now][i]);
        B = Get(1000000 + 1) - Get(val[now]);
        ans[now] = B - A;
        Update(val[now], 1);
    }
    
    int main()
    {
        while (~scanf("%d", &n))
        {
            init();
            for (int i = 1; i < n; i++)
            {
                int k;
                scanf("%d", &k);
                Tree[k].push_back(i);
            }
            for (int i = 0; i < n; i++)
            {
                scanf("%d", &val[i]);
                val[i]++;
            }
            DFS(0);
            for (int i = 0; i < n; i++)
            {
                if (i < n - 1) printf("%d ", ans[i]);
                else printf("%d
    ", ans[i]);
            }
        }
        return 0;
    }
  • 相关阅读:
    前世今生:Hive、Shark、spark SQL
    spark streaming 6: BlockGenerator、RateLimiter
    spark streaming 5: InputDStream
    spark streaming 4: DStreamGraph JobScheduler
    常见css水平自适应布局
    js动态加载以及确定加载完成的代码
    如何判断css是否加载完成
    翻书特效
    jquery 事件冒泡的介绍以及如何阻止事件冒泡
    phonegap之android原生日历调用
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4779473.html
Copyright © 2020-2023  润新知