题目链接

luogu

思路

可以说是线段树合并的练手题目吧

也没啥说的,就是dfs,然后合并、、、

看代码吧

错误

和写主席树错的差不多

都是变量写错、、、、

代码

#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
using namespace std;
const int maxn=1e5+7;
inline int read() {
int x=0,f=1;char s=getchar();
for(;s>'9'||s<'0';s=getchar()) if(s=='-') f=-1;
for(;s>='0'&&s<='9';s=getchar()) x=x*10+s-'0';
return x*f;
}
int n,tmp,cnt,a[maxn],b[maxn],rt[maxn],ans[maxn],ch[maxn*20][2],siz[maxn*20];
vector<int> t[maxn];
void build(int l,int r,int k,int &now) {
now=++cnt;
siz[now]=1;
if(l==r)return;
int mid=(l+r)>>1;
if(k<=mid) build(l,mid,k,ch[now][0]);
else build(mid+1,r,k,ch[now][1]);
}
int merge(int x,int y) {
if(!x||!y) return x+y;
siz[x]+=siz[y];
ch[x][0]=merge(ch[x][0],ch[y][0]);
ch[x][1]=merge(ch[x][1],ch[y][1]);
return x;
}
int query(int l,int r,int k,int now) {
if(r<=k||!now) return 0;
if(l>k) return siz[now];
int mid=(l+r)>>1,ans=0;
ans+=query(l,mid,k,ch[now][0]);
ans+=query(mid+1,r,k,ch[now][1]);
return ans;
}
void dfs(int u) {
for(vector<int>::iterator it=t[u].begin();it!=t[u].end();++it)
dfs(*it),rt[u]=merge(rt[u],rt[*it]);
ans[u]=query(1,n,a[u],rt[u]);
}
int main() {
n=read();
FOR(i,1,n) a[i]=b[i]=read();
FOR(i,2,n) tmp=read(),t[tmp].push_back(i);
sort(b+1,b+1+n);
tmp=unique(b+1,b+1+n)-b-1;
FOR(i,1,n) a[i]=lower_bound(b+1,b+1+tmp,a[i])-b,build(1,n,a[i],rt[i]);
dfs(1);
FOR(i,1,n) cout<<ans[i]<<"\n";
return 0;
}

luogu P3605 [USACO17JAN]Promotion Counting晋升者计数的更多相关文章

  1. 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数

    题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...

  2. 树状数组 P3605 [USACO17JAN]Promotion Counting晋升者计数

    P3605 [USACO17JAN]Promotion Counting晋升者计数 题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 ...

  3. 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]

    题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...

  4. 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数

    题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...

  5. P3605 [USACO17JAN]Promotion Counting晋升者计数

    思路 线段树合并的板子.. 和子节点合并之后在值域线段树上查询即可 代码 #include <cstdio> #include <algorithm> #include < ...

  6. Luogu3605 [USACO17JAN]Promotion Counting晋升者计数

    Luogu3605 [USACO17JAN]Promotion Counting晋升者计数 给一棵 \(n\) 个点的树,点 \(i\) 有一个权值 \(a_i\) .对于每个 \(i\) ,求 \( ...

  7. 题解 P3605 【[USACO17JAN]Promotion Counting晋升者计数】

    这道题开10倍左右一直MLE+RE,然后尝试着开了20倍就A了...窒息 对于这道题目,我们考虑使用线段树合并来做. 所谓线段树合并,就是把结构相同的线段树上的节点的信息合在一起,合并的方式比较类似左 ...

  8. [USACO17JAN]Promotion Counting晋升者计数

    题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 1 \cdots N(1 \leq N \leq 100, 000)1⋯N(1≤N ...

  9. BZOJ4756 [USACO17JAN]Promotion Counting晋升者计数

    Description The cows have once again tried to form a startup company, failing to remember from past ...

随机推荐

  1. c++ string split

    #include <iterator> #include <regex> std::vector<std::string> s_split(const std::s ...

  2. Mysql常规优化

    一.SQL语句优化 (1)使用limit对查询结果的记录进行限定(2)避免select *,将需要查找的字段列出来(3)使用连接(join)来代替子查询(4)拆分大的delete或insert语句 二 ...

  3. 【2017-03-21】HTML表单及标记

    一.表单 表单在网页中主要负责数据采集功能 表单格式 <form action="服务器路径" method=get(用的比较少)/post(最常用)></for ...

  4. QString 与 string转换

    [1]QString 转换为string QString qString("好好学习天天向上"); std::string stdString = qString.toStdStr ...

  5. 基于Kubernetess集群部署完整示例——Guestbook

    目录贴:Kubernetes学习系列 本文依赖环境:Centos7部署Kubernetes集群.基于Kubernetes集群部署skyDNS服务 该示例中,我们将创建一个redis-master.两个 ...

  6. 【Alpha版本】冲刺阶段——Day2

    [Alpha版本]冲刺阶段--Day2 阅读目录 今日进展 问题困难 明日任务 今日贡献量 站立式会议 TODOlist [今日进展] 密码算法方面: 存储密码的步骤 使用CSPRNG生成一个长度足够 ...

  7. yum配置文件中baseurl和mirrorlist的区别

    找到yum.repo.d文件夹下的文件,随便打开一个,找到mirrorlist的url,比如: http://mirrorlist.centos.org/?release=6&arch=$ba ...

  8. 求N!的二进制表示中最低位1的位置。(编程之美)

    要求的是N!的二进制表示中最低位1的位置.给定一个整数N,求N!二进制表示的最低位1在第几位?例如:给定N = 3,N!= 6,那么N!的二进制表示(1 010)的最低位1在第二位. 为了得到更好的解 ...

  9. 网上搜到的权限系统demo

    网上搜到的权限系统demo http://www.sojson.com/shiro

  10. no matching editors or conversion strategy found

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionM ...