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 ...
随机推荐
- python3 随机数
random库 random.random()返回n,则 0 <= n < 1的小数. random.uniform(a,b) 返回n ,则 a <= n <= b的浮点 ...
- ural 1698. Square Country 5(记忆化搜索)
1698. Square Country 5 Time limit: 2.0 secondMemory limit: 64 MB The first arithmetical operation ta ...
- ios的虚拟键盘与fixed移动端的bug
//$('#search')表单input;$('.search_out')浮动元素 var u = navigator.userAgent, app = navigator.appVersion;v ...
- The Linux Storage Stack Diagram 内核 4.0 版的 I/O 栈
- px和em,rem的区别
任意浏览器的默认字体高都是16px. px: 像素(Pixel) , 计算机屏幕上的一个点.固定大小:不方便维护: em:相对于当前对象内 (父元素) 文本的字体尺寸.如当前对行内文本的字体尺寸未被人 ...
- Google 怎么搜索
著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者:崔凯 链接:http://www.zhihu.com/question/20161362/answer/14180620 ...
- getScrollX()理解
- Flask -- 请求、上传文件、Cookies
请求对象 from flask import request request.method #值为form表单提交的method 'POST'. 'GET'等 #如果值为'POST'或'PUT',则可 ...
- JavaScript中以构造函数的方式调用函数
转自:http://www.cnblogs.com/Saints/p/6012188.html 构造器函数(Constructor functions)的定义和任何其它函数一样,我们可以使用函数声明. ...
- java线程数据交换Exchanger
两个线程都等到交换函数才能完成交换数据操作,代码如下: package threadLock; import java.util.Random; import java.util.concurrent ...