题目链接

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. PDO数据访问抽象层(下)

    PDO两大功能 一.事务功能 PDO的事务功能主要控制好几条sql语句同时成功或者同时失败(当其中一条SQL语句有错误时,同时好几条一起失败),失败时可以回滚操作 1.造对象 <?php $ds ...

  2. [3]传奇3服务器源码分析一 DBServer

    留存 服务端下载地址: 点击这里

  3. SnmpTools配置

    上网搜索了很多文档,但是snmptools一直没有配置好,原因就是64机器,网上的说法大多直接复制过来的,或者就没有考虑64位机器.经过仔细搜索和测试,一下是详细的配置过程: Index 安装 如果是 ...

  4. EasyUI表格DataGrid前端分页和后端分页的总结

    Demo简介 Demo使用Java.Servlet为后台代码(数据库已添加数据),前端使用EasyUI框架,后台直接返回JSON数据给页面 1.配置Web.xml文件 <?xml version ...

  5. cocos 搭建安卓环境

    http://blog.csdn.net/yiye3376/article/details/42219889

  6. 刨根究底字符编码之—UTF-16编码方式

    在网上已经转悠好几天了, 这篇文章让我知道了UTF-16的前世今生, 感谢作者https://cloud.tencent.com/developer/article/1384687 1. UTF-16 ...

  7. 设计模式之Prototype(原型)(转)

    定义: 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. Prototype模式允许一个对象再创建另外一个可定制的对象,根本无需知道任何如何创建的细节,工作原理是:通过将一个原型对象传 ...

  8. Linux基础命令---pgrep

    pgrep pgrep指令可以按名字或者其他属性搜索指定的进程,显示出进程的id到标准输出. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedo ...

  9. strace跟踪多进程与内核的交互

    1.ptrace的说明 ptrace原型: #include <sys/ptrace.h> long ptrace(enum __ptrace_request request, pid_t ...

  10. Python词云分析

    import jieba from matplotlib import pyplot as plt from wordcloud import WordCloud from PIL import Im ...