【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=4756

【题目大意】

  给出一棵树,对于每个节点,求其子树中比父节点大的点个数

【题解】

  我们考虑每个权值建立一棵线段树,边dfs边将子节点合并为一颗线段树,
  那么只要查询当前点的树上后缀和即可。

【代码】

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int N=100010,M=N*20;
int n,a[N],ans[N],root[N],disc[N];
vector<int> v[N];
namespace Segment_Tree{
int tot;
struct node{int l,r,a,b,sum;}T[M];
void up(int x){T[x].sum=T[T[x].l].sum+T[T[x].r].sum;}
int build(int l,int r,int p){
int x=++tot;
T[x].a=l; T[x].b=r; T[x].sum=0;
if(l==r){T[x].sum=1;return x;}
int mid=(l+r)>>1;
if(p<=mid){T[x].l=build(l,mid,p);}
else{T[x].r=build(mid+1,r,p);}
return up(x),x;
}
int ask(int x,int l,int r){
if(!x)return 0;
if(l<=T[x].a&&T[x].b<=r)return T[x].sum;
int mid=(T[x].a+T[x].b)>>1,res=0;
if(l<=mid)res+=ask(T[x].l,l,r);
if(r>mid)res+=ask(T[x].r,l,r);
return res;
}
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);
return up(x),x;
}
void dfs(int x,int fx){
int res=0;
for(int i=0;i<v[x].size();i++){
int y=v[x][i];
if(y==fx)continue;
dfs(y,x);
res+=ask(root[y],a[x]+1,n);
root[x]=merge(root[x],root[y]);
}ans[x]=res;
}
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),disc[i]=a[i];
sort(disc+1,disc+n+1);
int m=unique(disc+1,disc+n+1)-disc-1;
for(int i=1;i<=n;i++)a[i]=lower_bound(disc+1,disc+m+1,a[i])-disc;
for(int i=2;i<=n;i++){
int x; scanf("%d",&x);
v[x].push_back(i); v[i].push_back(x);
}
for(int i=1;i<=n;i++)root[i]=Segment_Tree::build(1,n,a[i]);
Segment_Tree::dfs(1,1);
for(int i=1;i<=n;i++)printf("%d\n",ans[i]);
return 0;
}

BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)的更多相关文章

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

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

  2. BZOJ[Usaco2017 Jan]Promotion Counting——线段树合并

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

  3. BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)

    题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...

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

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

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

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

  6. 【BZOJ】4756: [Usaco2017 Jan]Promotion Counting

    [题意]带点权树,统计每个结点子树内点权比它大的结点数. [算法]线段树合并 [题解]对每个点建权值线段树(动态开点),DFS中将自身和儿子线段树合并后统计. 注意三个量tot,cnt,tots,细心 ...

  7. BZOJ4756:[USACO]Promotion Counting(线段树合并)

    Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对于每个奶牛来说,它的子树中有几个能力值比它大的. Input n,表示有几只奶牛 n<=10 ...

  8. bzoj 4756 Promotion Counting —— 线段树合并

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 合并子树的权值线段树: merge 返回 int 或者是 void 都可以. 代码如下 ...

  9. 【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组

    原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html 题目描述 The cows have once again tried to form a s ...

随机推荐

  1. 虽然UIImageView是UIScollView的子视图,但UIImageView左上角是contentOfSet的原点

      虽然UIImageView是UIScollView的子视图,但UIImageView左上角是contentOfSet的原点   https://www.evernote.com/shard/s22 ...

  2. 阿里云服务器部署笔记二(python3、Flask、uWSGI、Nginx)

    从git上把项目拉到服务器,项目可以在服务器上运行后,就只需要配置uwsgi和nginx了.它们的逻辑关系是:外部请求->nginx->uwsgi->项目实例. 一.配置uwsgi ...

  3. 对vue中 默认的 config/index.js:配置的详细理解 -【以及webpack配置的理解】-config配置的目的都是为了服务webpack的配置,给不同的编译条件提供配置

    当我们需要和后台分离部署的时候,必须配置config/index.js: 用vue-cli 自动构建的目录里面  (环境变量及其基本变量的配置) var path = require('path') ...

  4. vue.js将一个对象的所有属性作为prop进行传递

    1.方法一:使用不带参数的v-bind写法 <div id="app"> <child v-bind="todo"></child ...

  5. [干货,阅后进BAT不是梦]面试心得与总结---BAT、网易、蘑菇街

    本文转载自:公众号:JANiubility 前言 之前实习的时候就想着写一篇面经,后来忙就给忘了,现在找完工作了,也是该静下心总结一下走过的路程了,我全盘托出,奉上这篇诚意之作,希望能给未来找工作的人 ...

  6. Perl6 Bailador框架(2):路径设置

    use v6; use Bailador; =begin pod get表示是get发送 post表示是post发送 get/post 后面的 '/name' 表示是路径 => sub {} 是 ...

  7. parse_str

    之前没有遇到过parse_str,其意思就是“把查询字符串解析到变量中”也就是$str会被解析为变量. <?php $data = "a=1&b=2";parse_s ...

  8. udpserver.pl 和 udpclient.pl

    udpserver.pl #!use/bin/perl -w use Socket; #导入Socket库 ,INADDR_ANY);#压入sockaddr_in模式,利用了全局当地压缩地点INADD ...

  9. C# 读写XML文件示例

    using System; using System.Collections.Generic; using System.Text; using System.Configuration; using ...

  10. Python的数值和字符串

    Python数据类型 1.数值 --类型:  1/整型 2/长整型   3/浮点型 -- 0.0, 12.0, -18.8, 3e+7等 4/复数型 -- complex  In []: 0x34al ...