树状数组

#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. C# 将List中的数据导入csv文件中

    //http://www.cnblogs.com/mingmingruyuedlut/archive/2013/01/20/2849906.html C# 将List中的数据导入csv文件中   将数 ...

  2. CentOS 6下的VPN搭建

    PPTP 全称为 Point to Point Tunneling Protocol — 点到点隧道协议,是VPN协议中的一种.虚拟专用网(VPN)被定义为通过一个公用网络(通常是因特网)建立一个临时 ...

  3. a标签中使用img后的高度多了几个像素

    a元素下有一个匿名文本,这个文本外有一个匿名行级盒子,它有的默认vertical-align是baseline的,而且往往因为上文line-height的影响,使它有个line-height,从而使其 ...

  4. yii2图片验证码

    控制器LoginController.php <?php namespace backend\controllers; use Yii; use yii\debug\models\search\ ...

  5. android应用开发小技巧

    1,实现应用可以安装到SD卡里面去:在AndroidManifest.xml文件的manifest里面添加下面一行: 1 android:installLocation="preferExt ...

  6. mysql 中 SQL_CALC_FOUND_ROWS 功能

    mysql 数据库不符合sql标准的地方不少,比如TIMESTAMP列的处理,字符串比较默认大小写不敏感什么的.有时候这些问题会让你很郁闷,尤其是对从其它数据库转过来的人来说.但有些功能倒也蛮有趣. ...

  7. 《C++ Primer》之指向函数的指针

    函数指针是指指向函数而非指向对象的指针.像其他指针一样,函数指针也指向某个特定的类型.函数类型由其返回类型以及形参表确定,而与函数名无关: // pf points to function retur ...

  8. VMware 8安装苹果操作系统Mac OS X 10.7 Lion正式版

    今天介绍下VM8下安装Mac OS X 10.7  1.工具篇       下载Vmware Workstation 8.0正式版http://115.com/file/bhyk1l2u#       ...

  9. PostMessage 向Windows窗口发送Alt组合键

    PostMessage 向Windows窗口发送Alt组合键 关于向Windows窗口发送Alt组合键的问题,这个真是经典问题啊,在网上找了一下,问的人N多,方法差不多, 但就是没有很好解决问题. 之 ...

  10. SpringSecurity自定义过滤器

    applicationContext-security.xml: <beans:beans xmlns="http://www.springframework.org/schema/s ...