[BZOJ4756]Promotion Counting
Description
问对于每个奶牛来说,它的子树中有几个能力值比它大的。
Input
接下来n行为1-n号奶牛的能力值pi
接下来n-1行为2-n号奶牛的经理(树中的父亲)
Output
Sample Input
804289384
846930887
681692778
714636916
957747794
1
1
2
3
Sample Output
0
1
0
0
#include<iostream>
#include<cstdio>
#include<algorithm>
#define M 100010
#define ls ch[node][0]
#define rs ch[node][1]
using namespace std; int n,m,num,cnt;
int head[M],a[M],b[M],ans[M],rt[M];
int v[M<<],ch[M<<][];
struct point{int to,nxt;}e[M<<]; void add(int from,int to)
{
e[++num].nxt=head[from];
e[num].to=to;
head[from]=num;
} void insert(int &node,int l,int r,int x)
{
if(!node) node=++cnt;v[node]++;
if(l==r) return;
int mid=(l+r)/;
if(x<=mid) insert(ls,l,mid,x);
else insert(rs,mid+,r,x);
} int query(int node,int l,int r,int l1,int r1)
{
if(!node) return ;
if(l1<=l&&r1>=r) return v[node];
if(l1>r||r1<l) return ;
int mid=(l+r)/;
return query(ls,l,mid,l1,r1)+query(rs,mid+,r,l1,r1);
} int merge(int x,int y)
{
if(!x||!y) return x+y;
int node=++cnt;
v[node]=v[x]+v[y];
ls=merge(ch[x][],ch[y][]);
rs=merge(ch[x][],ch[y][]);
return node;
} void dfs(int x)
{
for(int i=head[x];i;i=e[i].nxt)
{
int to=e[i].to;
dfs(to);
rt[x]=merge(rt[x],rt[to]);
}
ans[x]=query(rt[x],,m,a[x]+,m);
insert(rt[x],,m,a[x]);
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]),b[++m]=a[i];
sort(b+,b++m); m=unique(b+,b++m)-b-;
for(int i=;i<=n;i++) a[i]=lower_bound(b+,b++m,a[i])-b;
for(int i=;i<=n;i++)
{
int x;scanf("%d",&x);
add(x,i);
}
dfs();
for(int i=;i<=n;i++) printf("%d\n",ans[i]);
return ;
}
[BZOJ4756]Promotion Counting的更多相关文章
- [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组
4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 305 Solved: ...
- Luogu3605 [USACO17JAN]Promotion Counting晋升者计数
Luogu3605 [USACO17JAN]Promotion Counting晋升者计数 给一棵 \(n\) 个点的树,点 \(i\) 有一个权值 \(a_i\) .对于每个 \(i\) ,求 \( ...
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]
题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...
- 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数
P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...
- 【题解】晋升者计数 Promotion Counting [USACO 17 JAN] [P3605]
[题解]晋升者计数 Promotion Counting [USACO 17 JAN] [P3605] 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训.!牛是可怕的管理者! [题目描 ...
- BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)
题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...
- BZOJ4756:[USACO]Promotion Counting(线段树合并)
Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对于每个奶牛来说,它的子树中有几个能力值比它大的. Input n,表示有几只奶牛 n<=10 ...
- bzoj4756 [Usaco2017 Jan]Promotion Counting
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题解] dsu on tree,树状数组直接上 O(nlog^2n) # inclu ...
随机推荐
- Python全栈day14-15-16-17(函数)
一,数学定义的函数 函数的定义:给定一个数集A,对A施加对应法则f,记作f(A),得到另一数集B,也就是B=f(A).那么这个关系式就叫函数关系式,简称函数.函数概念含有三个要素:定义域A.值域C和对 ...
- 在R语言中封装类读写sql操作工具类
1.mysql_helper.R # 使用RMySQL操作数据库 # 载入DBI和RMySQL包 library(DBI) library(RMySQL) mysql_con <- functi ...
- [iOS微博项目 - 4.4] - 会员标识
github: https://github.com/hellovoidworld/HVWWeibo A.会员标识 1.需求 给vip会员打上会员标识 不同等级的vip会员使用不同的标识 使用橙色作为 ...
- JavaBean的任务就是: “Write once, run anywhere, reuse everywhere” Enterprise JavaBeans
javaBean_百度百科 https://baike.baidu.com/item/javaBean/529577?fr=aladdin 区别EJB JavaBean 和 Server Bean(通 ...
- Django 框架之 URL
URL配置就像Django所支撑网站的目录.它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表. # 示例: urlpatterns = [ path(route, view, kwa ...
- NFS服务基础
NFS服务部署: (一)NFS环境准备: 操作系统 角色 IP地址 CentOS Linux release 7.3.1611 (Core) NFS服务端 192.168.152.138 CentOS ...
- How many---hdu2609(最小表示)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2609 给你n个01串,然后求这n个串中有几个不同的: 例如:1100 ,1001 ,0011 ,011 ...
- Json与字符串互相转换
jQuery插件支持的转换方式: $.parseJSON( jsonstr ); //jQuery.parseJSON(jsonstr),可以将json字符串转换成json对象 浏览器 ...
- goland激活
http://blog.csdn.net/benben_2015/article/details/78725467 http://blog.csdn.net/john_f_lau/article/de ...
- php about session store db or cache
PHP关于Session的配置: 在php.ini中配置为:session.name = PHPSESSID 在请求开始的时候,会话名称会被重置并存储到session.name配置项. 所以要想在不改 ...