bzoj 4756: [Usaco2017 Jan]Promotion Counting【dfs+树状数组】
思路还是挺好玩的
首先简单粗暴的想法是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+树状数组】的更多相关文章
- 【bzoj4756】[Usaco2017 Jan]Promotion Counting 离散化+树状数组
原文地址:http://www.cnblogs.com/GXZlegend/p/6832263.html 题目描述 The cows have once again tried to form a s ...
- bzoj 4756 [Usaco2017 Jan]Promotion Counting——线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 线段树合并裸题.那种返回 int 的与传引用的 merge 都能过.不知别的题是不是这 ...
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- BZOJ 4756 [Usaco2017 Jan]Promotion Counting(线段树合并)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题目大意] 给出一棵树,对于每个节点,求其子树中比父节点大的点个数 [题解] ...
- BZOJ[Usaco2017 Jan]Promotion Counting——线段树合并
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
- 【BZOJ】4756: [Usaco2017 Jan]Promotion Counting
[题意]带点权树,统计每个结点子树内点权比它大的结点数. [算法]线段树合并 [题解]对每个点建权值线段树(动态开点),DFS中将自身和儿子线段树合并后统计. 注意三个量tot,cnt,tots,细心 ...
- BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)
题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...
- [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组
4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 305 Solved: ...
- Weak Pair (dfs+树状数组)
Weak Pair (dfs+树状数组) 题意 这个题目是要求:一颗树上,有n个节点,给出每个节点的权值.另外给出一个值k,问有多少对节点满足: \(power[u]*power[v]<=k\) ...
随机推荐
- IntelliJ IDEA 13.1.1版本偶然的错误
总之很悲催也很浪费时间,这款软件很喜欢,不想卸载 图片中的style.css使得style.css一直是文本形式 将style.css删除就恢复正常了,这个错误弄了半天才搞定,心累.
- Python接口测试之报告(十五)
在本文章中,主要使用jenkins和编写的自动化测试代码,来生成漂亮的测试报告,关于什么是CI这些 我就不详细的介绍了,这里我们主要是实战为主. 首先搭建java的环境,这个这里不做介绍.搭建好jav ...
- set/multiset用法详解
集合 使用set或multiset之前,必须加入头文件<set> Set.multiset都是集合类,差别在与set中不允许有重复元素,multiset中允许有重复元素. sets和mul ...
- [luoguP1043] 数字游戏(DP)
传送门 搞个前缀和随便DP一下 代码 #include <cstdio> #include <cstring> #include <iostream> #defin ...
- hdu - 1394 Minimum Inversion Number(线段树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1394 很基础的线段树. 先查询在更新,如果后面的数比前面的数小肯定会查询到前面已经更新过的值,这时候返回的sum ...
- Ubuntu 16.04安装RapidSVN
使用RabbitVCS有一些不完美,比如没有把文件增加到版本库的功能,导致无法提交等问题,现在再次安装RapidSVN来弥补这些缺点. 安装: sudo apt-get install rapidsv ...
- Mybatis中insert中返回主键ID的方法
<insertid=“doSomething"parameterType="map"useGeneratedKeys="true"keyProp ...
- Chains (链 )
Indy 中的工作队列系统,调度器,和链引擎都被叫做链. 当使用链的时候,一个基于链的 IOHandler 存储工作项目到有关的工作队列中.在一个工作单元被完成以前,执行这个工作单元的纤程是无法做其它 ...
- centos7更新、更新、每天更新、每天自动更新
每一天我们的系统时时刻刻都被凶狠之徒盯着,保持软件在最新的状态是其中一项我们必须做,也很容易做到的工作. 首先我们立即手动更新所有预先安装的软件: yum -y update 跟着设定系统定时自动更新 ...
- linux网络结构体
一 链路层: (1)局域网(以太网ethernet): *struct eth_header:以太网头部. (ethernet/eth.c) *struct net_device:每一个网络设备都用这 ...