思路还是挺好玩的

首先简单粗暴的想法是dfs然后用离散化权值树状数组维护,但是这样有个问题就是这个全局的权值树状数组里并不一定都是当前点子树里的

第一反应是改树状数组,但是显然不太现实,但是可以这样想,就是现在统计子树之前把查到的答案减去,然后再查子树最后加上查到的答案,这样相当于去重了

方便起见,离散化的时候按从大到小的顺序,这样就变成了求比当前点小的点

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
const int N=100005;
int n,a[N],g[N],h[N],cnt,c[N],ans[N];
map<int,int>mp;
struct qwe
{
int ne,to;
}e[N<<1];
bool cmp(const int &a,const int &b)
{
return a>b;
}
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
h[u]=cnt;
}
void ins(int x)
{
for(int i=x;i<=n;i+=(i&(-i)))
c[i]++;
}
int ques(int x)
{
int r=0;
for(int i=x;i>=1;i-=(i&(-i)))
r+=c[i];
return r;
}
void dfs(int u)
{
ans[u]-=ques(a[u]);
for(int i=h[u];i;i=e[i].ne)
dfs(e[i].to);
ans[u]+=ques(a[u]);
ins(a[u]);
}
int main()
{
n=read();
for(int i=1;i<=n;i++)
a[i]=g[i]=read();
for(int i=2;i<=n;i++)
{
int x=read();
add(x,i);
}
sort(g+1,g+1+n,cmp);
for(int i=1;i<=n;i++)
mp[g[i]]=i;
for(int i=1;i<=n;i++)
a[i]=mp[a[i]];
dfs(1);
for(int i=1;i<=n;i++)
printf("%d\n",ans[i]);
return 0;
}

bzoj 4756: [Usaco2017 Jan]Promotion Counting【dfs+树状数组】的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. Weak Pair (dfs+树状数组)

    Weak Pair (dfs+树状数组) 题意 这个题目是要求:一颗树上,有n个节点,给出每个节点的权值.另外给出一个值k,问有多少对节点满足: \(power[u]*power[v]<=k\) ...

随机推荐

  1. Qt笔记——各种组件和其他细碎用法

    LineEdit 获取文本:ui->usrLineEdit->text() 清空内容:ui->pwdLineEdit->clear(); 定位光标:ui->usrLine ...

  2. 用java实现二分搜索<算法分析>

    实验目的:1.复习java编程:2.掌握二分搜索技术的基本原理:3.掌握使用java程序进行二分搜索的方法.实验步骤:1.由用户输入5个以上的整数:2.利用二分搜索算法完成对数组的搜索. packag ...

  3. devstck 部署OpenStack Queens allinone

    1.环境信息 1台虚拟机 8C16G CentOS 7.2   2.准备工作 #!/bin/bash set -x #配置aliyun的centos和epel mirror mkdir /etc/yu ...

  4. CodeForcesGym 100517B Bubble Sort

    Bubble Sort Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForcesGym. ...

  5. ORACLE RAC with NFS install

    第一步:建立第一台节点机1).拷贝一台虚拟机做第一个节点,增加host-only的网卡 kudzu工具识别网卡(两都host-only是可以的).然后以下步骤修改相应的配置.etc/hosts内容如下 ...

  6. [codeforces471D]MUH and Cube Walls

    [codeforces471D]MUH and Cube Walls 试题描述 Polar bears Menshykov and Uslada from the zoo of St. Petersb ...

  7. Partitioned Replacement for Cache Memory

    In a particular embodiment, a circuit device includes a translation look-aside buffer (TLB) configur ...

  8. ****HTML模板资源汇总

    站长素材: http://sc.chinaz.com/tag_moban/HTML.html wordpress模板: http://www.cssmoban.com/wpthemes/ http:/ ...

  9. JDBC驱动类型

    一下内容引用自http://wiki.jikexueyuan.com/project/jdbc/drive-types.html: 一.什么是JDBC驱动程序? JDBC驱动实现了JDBC API中定 ...

  10. 【SQL Server 学习系列】-- 随机生成日期时间的SQL脚本

    DECLARE @dt1 DATETIME,@dt2 DATETIME,@a BIGINT,@b BIGINT SET @dt1='2010-01-01'--开始日期 SET @dt2='2010-0 ...