luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题目链接
思路
可以说是线段树合并的练手题目吧
也没啥说的,就是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晋升者计数的更多相关文章
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- 树状数组 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晋升者计数
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
- P3605 [USACO17JAN]Promotion Counting晋升者计数
思路 线段树合并的板子.. 和子节点合并之后在值域线段树上查询即可 代码 #include <cstdio> #include <algorithm> #include < ...
- Luogu3605 [USACO17JAN]Promotion Counting晋升者计数
Luogu3605 [USACO17JAN]Promotion Counting晋升者计数 给一棵 \(n\) 个点的树,点 \(i\) 有一个权值 \(a_i\) .对于每个 \(i\) ,求 \( ...
- 题解 P3605 【[USACO17JAN]Promotion Counting晋升者计数】
这道题开10倍左右一直MLE+RE,然后尝试着开了20倍就A了...窒息 对于这道题目,我们考虑使用线段树合并来做. 所谓线段树合并,就是把结构相同的线段树上的节点的信息合在一起,合并的方式比较类似左 ...
- [USACO17JAN]Promotion Counting晋升者计数
题目描述 奶牛们又一次试图创建一家创业公司,还是没有从过去的经验中吸取教训--牛是可怕的管理者! 为了方便,把奶牛从 1 \cdots N(1 \leq N \leq 100, 000)1⋯N(1≤N ...
- BZOJ4756 [USACO17JAN]Promotion Counting晋升者计数
Description The cows have once again tried to form a startup company, failing to remember from past ...
随机推荐
- Rpgmakermv(14)Archeia_Steamworks
作用: 接入steam成就 -----------------------------------------------------------------------------------原文: ...
- 32网络通信之Poll模型
多路复用并发模型 -- poll #include<poll.h> int poll(struct pollfd *fds, unsigned int nfds, int timeo ...
- spring 的核心类JdbcTemplate 方法
2018-11-29 10:28:02
- Java集合-----List详解
List中的元素是有序排列的而且可重复 1.LinkedList LinkedList是非线程安全的,底层是基于双向链表实现的 LinkedList常用方法: toArray() ...
- 判断是移动端还是PC端
// 判断是移动端还是PC端 $http_user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USE ...
- Kattis之旅——Chinese Remainder
Input The first line of input consists of an integers T where 1≤T≤1000, the number of test cases. Th ...
- linux git 报错提示 fatal: 'origin' does not appear to be a git repository 解决办法
输入: git pull origin master git报错提示 fatal: 'origin' does not appear to be a git repository fatal: Cou ...
- PHP 中文工具类,支持汉字转拼音、拼音分词、简繁互转
ChineseUtil 下载地址:https://github.com/Yurunsoft/ChineseUtil 另外一个中文转拼音工具:https://github.com/overtrue/pi ...
- 写给大忙人的CentOS 7下最新版(6.2.4)ELK+Filebeat+Log4j日志集成环境搭建完整指南
现在的公司由于绝大部分项目都采用分布式架构,很早就采用ELK了,只不过最近因为额外的工作需要,仔细的研究了分布式系统中,怎么样的日志规范和架构才是合理和能够有效提高问题排查效率的.经过仔细的分析和研究 ...
- k8s (kubernetes) 代码分析
简易入门 结构图: debug 先了解 etcd API源码分析 API server 是中心. https://www.jianshu.com/p/88c6ed78b668 $ git ls-fil ...