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 ...
随机推荐
- Xcode8中添加SnapKit框架报错,编译失败
既然SnapKit的作者说SnapKit已经支持Swift3.0了,那么我们就先来适配SnapKit,首先用Xcode8新建一个空项目,利用Cocoapods导入SnapKit. Podfile 打 ...
- ***1133. Fibonacci Sequence(斐波那契数列,二分,数论)
1133. Fibonacci Sequence Time limit: 1.0 secondMemory limit: 64 MB is an infinite sequence of intege ...
- javascript注释规范
注释在代码编写过程中的重要性,写代码超过半年的就能深深的体会到.没有注释的代码都不是好代码.为了别人学习,同时为了自己以后对代码进行'升级',看看js/javascript代码注释规范与示例.来自:h ...
- Mozilla Firefox的各级版本链接
Mozilla Firefox的各级版本链接 及语言 https://ftp.mozilla.org/pub/firefox/releases/
- over 分析函数之 lag() lead()
/*语法*/ lag(exp_str,offset,defval) over() 取前 Lead(exp_str,offset,defval) over() 取后 --exp_str要取的列 -- ...
- 在 Linux 环境下报错 java.lang.reflect.InvocationTargetException
今天开发了一个 excel 导出数据的功能,放到 linux 服务器上后发现报错. 捕获到 java.lang.reflect.InvocationTargetException 异常,这个异常不太常 ...
- 使用monit搭建一个监控系统
上周用monit搭建或者说定制了一个监控系统,来监控服务器发生事情.当然了主要是监控异常,因为我们的产品属于服务器类型,很多进程都daemon,要不停的运行.我们搭建监控目的不过是出现问题能够及时的知 ...
- delete、truncate与drop的区别
转自:SQL truncate .delete与drop区别 相同点: 1.truncate和不带where子句的delete.以及drop都会删除表内的数据. 2.drop.truncate都是DD ...
- hdu_5793_A Boring Question(打表找规律)
题目链接:hdu_5793_A Boring Question 题意: 自己看吧,说不清楚了. 题解: 打表找规律 #include<cstdio> typedef long long l ...
- Soj题目分类
-----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...