zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    HTML5学习笔记简明版(1):HTML5介绍与语法
    用margin还是用padding(1)——W3School CSS外边距合并
    Minimum Depth of Binary Tree
    118. Pascal's Triangle
    Convert Sorted Array to Binary Search Tree
    112. Path Sum
    Balanced Binary Tree
    centos 7下nginx搭建流媒体服务器【动态添加模块】
    Java内存泄漏
    Quartz的job中注入的services接口为空的解决办法
  • 原文地址:https://www.cnblogs.com/zufezzt/p/4779473.html
Copyright © 2011-2022 走看看