ZOJ 2866 Overstaffed Company
树状数组
#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的更多相关文章
- ZOJ 2112 Dynamic Rankings(主席树の动态kth)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112 The Company Dynamic Rankings ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 高级数据结构(树状数组套主席树):ZOJ 2112 Dynamic Rankings
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- ZOJ 3794 Greedy Driver
两次SPFA 第一关找:从1没有出发点到另一个点的多少是留给油箱 把边反过来再找一遍:重每一个点到终点最少须要多少油 Greedy Driver Time Limit: 2 Seconds ...
- ZOJ 2676 Network Wars[01分数规划]
ZOJ Problem Set - 2676 Network Wars Time Limit: 5 Seconds Memory Limit: 32768 KB Special J ...
- ZOJ 2112 Dynamic Rankings (动态第 K 大)(树状数组套主席树)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- ZOJ 2676 Network Wars(最优比例最小割)
Network Wars Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge Network of Bytelan ...
- ZOJ 2112 Dynamic Rankings(带修改的区间第K大,分块+二分搜索+二分答案)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
随机推荐
- python--sum函数--sum(axis=1)
平时用的sum应该是默认的axis=0 就是普通的相加,当加入axis=1以后就是将一个矩阵的每一行向量相加. 例如: >>>import numpy as np >>& ...
- 使用timer定时器,防止事件重入
首先简单介绍一下timer,这里所说的timer是指的System.Timers.timer,顾名思义,就是可以在指定的间隔是引发事件.官方介绍在这里,摘抄如下: 1 2 Timer 组件是基于服务器 ...
- CFNetwork SSLHandshake failed (-9824) ios 9
设置 NSAppTransportSecurity
- sql 将表B中不存在表A的数据 插入到表A中
insert into tableA select * from tableB b where not exists(select 1 from tableA a where a.id = b.id) ...
- android开发进阶学习博客资源
Android开发者博客推荐 Android入门级 - 罗宪明 http://blog.csdn.net/wdaming1986 Android入门级 - 魏祝林 http://blog.csdn.n ...
- HDU 5813 Elegant Construction
构造.从a[i]最小的开始放置,例如放置了a[p],那么还未放置的,还需要建边的那个点 需求量-1,然后把边连起来. #pragma comment(linker, "/STACK:1024 ...
- ios简单更改系统TabBar的高度
- (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; CGRect frame = self.tabBar.frame; fra ...
- 股票K线图-JfreeChart版
http://blog.csdn.net/ami121/article/details/3953272 股票K线图-JfreeChart版 标签: jfreechartpropertiesapplet ...
- Entity Framework教程
随着Code First一起出现的DbContext和DbSet类绝对可以称得上EF的功能核心,其取代了之前的ObjectContext和ObjectSet类,提供了与数据库通信,管理内存中实体的重要 ...
- hdu 2503 a/b + c/d
Problem Description 给你2个分数,求他们的和,并要求和为最简形式. Input 输入首先包含一个正整数T(T<=1000),表示有T组测试数据,然后是T行数据,每行包含四 ...