【题意】带点权树,统计每个结点子树内点权比它大的结点数。

【算法】线段树合并

【题解】对每个点建权值线段树(动态开点),DFS中将自身和儿子线段树合并后统计。

注意三个量tot,cnt,tots,细心查错。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
int n,first[maxn],cnt,tot,tots,root[maxn],a[maxn],b[maxn],ans[maxn];
struct edge{int v,from;}e[maxn*];
void ins(int u,int v){tots++;e[tots].v=v;e[tots].from=first[u];first[u]=tots;}
struct cyc{int l,r,sum;}t[maxn*];
void insert(int l,int r,int &x,int y){
if(!x)x=++cnt;t[x].sum++;
if(l==r)return;//
int mid=(l+r)>>;
if(y<=mid)insert(l,mid,t[x].l,y);
else insert(mid+,r,t[x].r,y);
}
int merge(int x,int y){
if(!x||!y)return x^y;//
t[x].l=merge(t[x].l,t[y].l);
t[x].r=merge(t[x].r,t[y].r);//
t[x].sum=t[t[x].l].sum+t[t[x].r].sum;//
return x;
}
int ask(int left,int right,int k,int l,int r){
if(l<=left&&right<=r)return t[k].sum;
else{
int mid=(left+right)>>,sum=;
if(l<=mid)sum=ask(left,mid,t[k].l,l,r);
if(r>mid)sum+=ask(mid+,right,t[k].r,l,r);
return sum;
}
}
void dfs(int x){
insert(,tot,root[x],a[x]);
for(int i=first[x];i;i=e[i].from){
dfs(e[i].v);
root[x]=merge(root[x],root[e[i].v]);
}
if(a[x]<tot)ans[x]=ask(,tot,root[x],a[x]+,tot);
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
b[i]=a[i];
}
for(int i=;i<=n;i++){
int fa;scanf("%d",&fa);
ins(fa,i);
}
sort(b+,b+n+);tot=n;
tot=unique(b+,b+tot+)-b-;
for(int i=;i<=n;i++)a[i]=lower_bound(b+,b+tot+,a[i])-b;
dfs();
for(int i=;i<=n;i++)printf("%d\n",ans[i]);
return ;
}

【BZOJ】4756: [Usaco2017 Jan]Promotion Counting的更多相关文章

  1. BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题目大意] 给出一棵树,对于每个节点,求其子树中比父节点大的点个数 [题解] ...

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

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

  3. bzoj 4756: [Usaco2017 Jan]Promotion Counting【dfs+树状数组】

    思路还是挺好玩的 首先简单粗暴的想法是dfs然后用离散化权值树状数组维护,但是这样有个问题就是这个全局的权值树状数组里并不一定都是当前点子树里的 第一反应是改树状数组,但是显然不太现实,但是可以这样想 ...

  4. bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 线段树合并裸题.那种返回 int 的与传引用的 merge 都能过.不知别的题是不是这 ...

  5. 【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting

    调半天原来是dsu写不熟 Description The cows have once again tried to form a startup company, failing to rememb ...

  6. [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组

    4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 305  Solved: ...

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

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

  8. bzoj4756 [Usaco2017 Jan]Promotion Counting

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题解] dsu on tree,树状数组直接上 O(nlog^2n) # inclu ...

  9. 【BZOJ】1576 [Usaco2009 Jan]安全路经Travel

    [算法]最短路树+(树链剖分+线段树)||最短路树+并查集 [题解] 两种方法的思想是一样的,首先题目限制了最短路树唯一. 那么建出最短路树后,就是询问对于每个点断掉父边后重新找路径的最小值,其它路径 ...

随机推荐

  1. Lucene 常用名词解析

    索引的创建:IndexWriter: 用于创建索引Directory: 这个可以用来定义我们的索引是存放在内存中还是在硬盘上Analyzer: 分词器 有几种()这个地方需要好好解释下Document ...

  2. windows下面安装redis

    一.下载windows版本的Redis 链接:https://pan.baidu.com/s/1i6X2klv 密码:j4pi 二.安装Redis 这里下载的是Redis-x64-3.2.100版本, ...

  3. 第114天:Ajax跨域请求解决方法(二)

    一.什么是跨域 我们先回顾一下域名地址的组成: http:// www . google : 8080 / script/jquery.js   http:// (协议号)       www  (子 ...

  4. HDU4055_Number String

    题目告诉你在一个排列中,相邻两个数的大小关系.问你排列可能有多少种情况. DP. f[i][j]表示将i个数按照前面i-1个大小关系排列且最后一个数位j的排列数有多少个. 这样对于新加入的一个数i+1 ...

  5. @Retention(保留) 此注解用于运行时候(反射)时候使用 如果不使用的话 在反射时候无法获取到注解的值

    @Retention(保留) 此注解用于运行时候(反射)时候使用 如果不使用的话 在反射时候无法获取到注解的值

  6. element-ui中单独引入Message组件的问题

    import Message from './src/main.js'; export default Message; 由于Message组件并没有install 方法供Vue来操作的,是直接返回的 ...

  7. python中括号的使用

    1. 列表list是用[ ]包住的以逗号分隔的数据集合 所有对列表的解析均采用[ ],不论是元素引用或取值 [ ]表示空列表 2. 字典由键-值(key-value)对构成,一般可采用{ }表示 取字 ...

  8. 【UOJ#188】Sanrd(min_25筛)

    [UOJ#188]Sanrd(min_25筛) 题面 UOJ 题解 今天菊开讲的题目.(千古神犇陈菊开,扑通扑通跪下来) 题目要求的就是所有数的次大质因子的和. 这个部分和\(min\_25\)筛中枚 ...

  9. 【HDU5919】SequenceII(主席树)

    [HDU5919]SequenceII(主席树) 题面 Vjudge 翻译(by ppl) 给一个长度为N的数列A,有m个询问,每次问 数列[l,r]区间中所有数的第一次出现的位置的中位 数是多少 题 ...

  10. Insert Node in Sorted Linked List

    Insert a node in a sorted linked list. Have you met this question in a real interview?  Yes Example ...