树状数组

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std; const int maxn = + ;
vector<int>Tree[ + ];
int n;
int val[ + ];
int c[maxn];
int ans[ + ]; 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 = ;
    while (x != )
    {
        ret += c[x];
        x -= lowbit(x);
    }
    return ret;
} void init()
{
    for (int i = ; i <= n; i++) Tree[i].clear();
    memset(c, , sizeof c);
} void DFS(int now)
{
    int A, B;
    A = Get( + ) - Get(val[now]);
    for (int i = ; i < Tree[now].size(); i++)
        DFS(Tree[now][i]);
    B = Get( + ) - Get(val[now]);
    ans[now] = B - A;
    Update(val[now], );
} int main()
{
    while (~scanf("%d", &n))
    {
        init();
        for (int i = ; i < n; i++)
        {
            int k;
            scanf("%d", &k);
            Tree[k].push_back(i);
        }
        for (int i = ; i < n; i++)
        {
            scanf("%d", &val[i]);
            val[i]++;
        }
        DFS();
        for (int i = ; i < n; i++)
        {
            if (i < n - ) printf("%d ", ans[i]);
            else printf("%d\n", ans[i]);
        }
    }
    return ;
}

ZOJ 2866 Overstaffed Company的更多相关文章

  1. ZOJ 2112 Dynamic Rankings(主席树の动态kth)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112 The Company Dynamic Rankings ...

  2. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  3. 高级数据结构(树状数组套主席树):ZOJ 2112 Dynamic Rankings

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  4. ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

    题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...

  5. ZOJ 3794 Greedy Driver

    两次SPFA 第一关找:从1没有出发点到另一个点的多少是留给油箱 把边反过来再找一遍:重每一个点到终点最少须要多少油 Greedy Driver Time Limit: 2 Seconds       ...

  6. ZOJ 2676 Network Wars[01分数规划]

    ZOJ Problem Set - 2676 Network Wars Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special J ...

  7. ZOJ 2112 Dynamic Rankings (动态第 K 大)(树状数组套主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  8. ZOJ 2676 Network Wars(最优比例最小割)

    Network Wars Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge Network of Bytelan ...

  9. ZOJ 2112 Dynamic Rankings(带修改的区间第K大,分块+二分搜索+二分答案)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

随机推荐

  1. erlang四大behaviour之四-supervisor

    http://www.cnblogs.com/puputu/articles/1689621.html 1. 监督规则 一个监督者负责启动.停止.监控他的子进程.监督者的一个基本概念就是当必要的时候重 ...

  2. python--zeros函数和ones函数

    使用numpy.zeros,numpy.ones,numpy.eye等方法可以构造特定的矩阵 例如: 代码如下: >>>from numpy import * >>> ...

  3. bootstrap开始咯

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  4. 如何使用ajax(jquery)

    以下是我第一次工作时写的ajax: $.ajax({ url: "/spinweb/store/storeHome.do", dataType: 'json', data: { & ...

  5. 感知哈希算法的java实现

    一.原理讲解      实现这种功能的关键技术叫做"感知哈希算法"(Perceptual Hash Algorithm), 意思是为图片生成一个指纹(字符串格式), 两张图片的指纹 ...

  6. keybd_event 对应表

    Option Explicit Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bSc ...

  7. DLL调试方法

    1.已经做好的dll不能设置:你可以用AfxMessageBox把信息打印出来.2.哪个地方调用的函数 把DLL重新编译一次 在把DLL放到工程里 从新添加一下 然后在你工程调用DLL内容的地方设置断 ...

  8. how to use tar?

    In UNIX, tar is the most useful tool to compress files (just like zip in Windows.) To compress, inpu ...

  9. ECShop - 数据库操作类

    ECShop v2.7.2没有使用一些开源的数据库操作类,比如adodb或者PEAR,而是封装了自己的实现.这样做的好处是实现非常轻量,只有一个文件,27Kb,大大减小了分发包的文件大小.另外,当网站 ...

  10. Linux添加用户user到用户组group

    添加用户:useradd niot 添加到组:usermod -a -G root niot 改密码:passwd niot 切换:su - niot 查看用户组:groups 将一个用户添加到用户组 ...