https://www.zybuluo.com/ysner/note/1282069

题面

给一颗带点权的树,求每个点的子树中比该点权值大的点的个数。

  • \(n\leq10^5\)

解析

首先有个很无脑的方法。

用一个权值树状数组维护所有点权(离散化后的)。

每到一个点,询问比该点大的数的个数,然后把这个点权加入树状数组。

dfs回溯以后,再次询问,用这次答案减去上次答案。

这样可以在每个点上直接询问答案。

复杂度\(O(nlog^2n)\)。常数巨小。

然而我做这道题是想入门线段树合并。

其实思想正好相反。

我们不删点权,只把信息从下面合并上来。

对每个点,把它儿子的值域线段树与自己合并。

具体来说是从根节点出发,统计根结点的值,然后合并左儿子右儿子。

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#define ll long long
#define re register
#define il inline
#define fp(i,a,b) for(re int i=a;i<=b;i++)
#define fq(i,a,b) for(re int i=a;i>=b;i--)
using namespace std;
const int N=5e5+100,M=1e7;
struct Edge{int to,nxt;}e[N<<1];
int a[N],w[N],n,h[N],cnt,tot,ans[N],rt[M],s[M],ls[M],rs[M],tim;
il void add(re int u,re int v){e[++cnt]=(Edge){v,h[u]};h[u]=cnt;}
il ll gi()
{
re ll x=0,t=1;
re char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') t=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-48,ch=getchar();
return x*t;
}
il void Modify(re int &x,re int l,re int r,re int W)
{
if(!x) x=++tim;
++s[x];
if(l==r) return;
re int mid=l+r>>1;
if(W<=mid) return Modify(ls[x],l,mid,W);
return Modify(rs[x],mid+1,r,W);
}
il int Query(re int x,re int l,re int r,re int W)
{
if(!x) return 0;
if(l>=W) return s[x];
re int mid=l+r>>1;
if(W<=mid) return Query(ls[x],l,mid,W)+Query(rs[x],mid+1,r,W);
return Query(rs[x],mid+1,r,W);
}
il int Merge(re int u,re int v)
{
if(!u) return v;if(!v) return u;
re int p=++tim;
s[p]=s[u]+s[v];
ls[p]=Merge(ls[u],ls[v]);
rs[p]=Merge(rs[u],rs[v]);
return p;
}
il void dfs(re int u)
{
for(re int i=h[u];i+1;i=e[i].nxt)
{
re int v=e[i].to;
dfs(v);
rt[u]=Merge(rt[u],rt[v]);
}
ans[u]=Query(rt[u],1,tot,w[u]+1);
Modify(rt[u],1,tot,w[u]);
}
int main()
{
memset(h,-1,sizeof(h));
n=gi();
fp(i,1,n) a[i]=w[i]=gi();
fp(v,2,n)
{
re int u=gi();
add(u,v);
}
sort(a+1,a+1+n);
tot=unique(a+1,a+1+n)-a-1;
fp(i,1,n) w[i]=lower_bound(a+1,a+1+tot,w[i])-a;
dfs(1);
fp(i,1,n) printf("%d\n",ans[i]);
return 0;
}

luogu3605晋升者计数的更多相关文章

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

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

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

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

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

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

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

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

  5. [USACO17JAN] 晋升者计数 dfs序+树状数组

    [USACO17JAN] 晋升者计数 dfs序+树状数组 题面 洛谷P3605 题意:一棵有点权的树,找出树中所有\((u,v)\)的对数,其中\(u,v\)满足\(val(u)\le val(v)\ ...

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

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

  7. 【USACO17JAN】Promotion Counting晋升者计数 线段树+离散化

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

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

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

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

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

随机推荐

  1. IDEA基本使用及配置(2)

    IDEA配置:File >> Setiings进入配置界面 1.主题配置:默认两种主题,黑色.白色,可以自己在网上下载,然后File >> Import Setiings导入, ...

  2. Xshell连接Centos7.5和yum

    目 录 第1章 Centos7 IP地址的配置    1 1.1 第一种配置ip方法(nmtui)    1 1.2 第二种 修改网卡配置文件    5 1.2.1 使用cat查看配置文件    5 ...

  3. UVA 253 Cube painting(枚举 模拟)

    题意: 按如图的顺序给定2个骰子的颜色(只有r.b.g三种颜色) 问2个骰子是否一模一样 如 可表示为“rbgggr” 和 “rggbgr”, 第二个就是绕着Z轴顺时针旋转90度与第一个相同的骰子. ...

  4. JQuery常用的案例

    1.给导航栏添加鼠标移上去的时候变换背景颜色的方法. $(function () { $(".nav li").mouseover(function () { $(this).cs ...

  5. servlet页面没有跳转

    Boolean b = userService.selectByParams(user);if (b) { req.getSession().setAttribute("loginname& ...

  6. CodeForcesGym 100524J Jingles of a String

    Jingles of a String Time Limit: 2000ms Memory Limit: 524288KB This problem will be judged on CodeFor ...

  7. xtu read problem training 4 B - Multiplication Puzzle

    Multiplication Puzzle Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. O ...

  8. 【NOIP2017练习】函数变换(DP,dfs)

    题意: 思路: 极限步数大概不会超过30 ; ..max,..]of longint; eul:..max]of longint; cas,v,n,k,i,ans,j:longint; functio ...

  9. CSU - 1115 最短的名字(字典树模板题)

    Description 在一个奇怪的村子中,很多人的名字都很长,比如aaaaa, bbb and abababab. 名字这么长,叫全名显然起来很不方便.所以村民之间一般只叫名字的前缀.比如叫'aaa ...

  10. Ubuntu 16.04安装Atom(加强版文本工具)

    安装: sudo add-apt-repository ppa:webupd8team/atom sudo apt-get update sudo apt-get install atom 或者直接上 ...