[BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组
4756: [Usaco2017 Jan]Promotion Counting
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 305 Solved: 217
[Submit][Status][Discuss]
Description
问对于每个奶牛来说,它的子树中有几个能力值比它大的。
Input
接下来n行为1-n号奶牛的能力值pi
接下来n-1行为2-n号奶牛的经理(树中的父亲)
Output
Sample Input
804289384
846930887
681692778
714636916
957747794
1
1
2
3
Sample Output
0
1
0
0
HINT
Source
先离散化。
dfs遍历,用树状数组维护,比当前元素小的元素个数,答案显然为子树size-遍历完子树后比当前元素小的元素个数+进入子树时比当前元素小的元素个数。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int read() {
int x=,f=;char ch=getchar();
while(!isdigit(ch)) {ch=getchar();}
while(isdigit(ch)) {x=x*+ch-'';ch=getchar();}
return x;
}
int n;
int p[];
int a[];
int sum[];
int lowbit(int x){return x&(-x);}
void update(int x,int val) {for(int i=x;i<=n;i+=lowbit(i)) sum[i]+=val;}
int query(int x) {
int ans=;
for(int i=x;i>;i-=lowbit(i)) ans+=sum[i];
return ans;
}
struct data {
int to,next;
}e[];
int head[],cnt,size[],ans[];
void add(int u,int v) {e[cnt].next=head[u];e[cnt].to=v;head[u]=cnt++;}
void dfs(int now) {
size[now]=;
int tmp=query(a[now]);
update(a[now],);
for(int i=head[now];i>=;i=e[i].next) {
int to=e[i].to;
dfs(to);
size[now]+=size[to];
}
tmp=query(a[now])-tmp;
ans[now]=size[now]-tmp;
}
int main() {
memset(head,-,sizeof(head));
n=read();
for(int i=;i<=n;i++) p[i]=a[i]=read();
sort(p+,p+n+);
for(int i=;i<=n;i++) a[i]=lower_bound(p+,p+n+,a[i])-p;
for(int i=;i<=n;i++) {
int v=i,u=read();
add(u,v);
}
dfs();
for(int i=;i<=n;i++) printf("%d\n",ans[i]);
}
[BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组的更多相关文章
- BZOJ_4756_[Usaco2017 Jan]Promotion Counting_树状数组
BZOJ_4756_[Usaco2017 Jan]Promotion Counting_树状数组 Description n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根. 问对 ...
- 【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting
调半天原来是dsu写不熟 Description The cows have once again tried to form a startup company, failing to rememb ...
- [BZOJ4756] [Usaco2017 Jan]Promotion Counting(线段树合并)
传送门 此题很有意思,有多种解法 1.用天天爱跑步的方法,进入子树的时候ans-query,出去子树的时候ans+query,query可以用树状数组或线段树来搞 2.按dfs序建立主席树 3.线段树 ...
- bzoj4756 [Usaco2017 Jan]Promotion Counting
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4756 [题解] dsu on tree,树状数组直接上 O(nlog^2n) # inclu ...
- BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)
题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...
- 【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 || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- bzoj 4756: [Usaco2017 Jan]Promotion Counting【dfs+树状数组】
思路还是挺好玩的 首先简单粗暴的想法是dfs然后用离散化权值树状数组维护,但是这样有个问题就是这个全局的权值树状数组里并不一定都是当前点子树里的 第一反应是改树状数组,但是显然不太现实,但是可以这样想 ...
- HDU 4358 Boring counting 树状数组+思路
研究了整整一天orz……直接上官方题解神思路 #include <cstdio> #include <cstring> #include <cstdlib> #in ...
随机推荐
- Pascal小游戏 打飞机
一个经典的打飞机游戏(1)Pascal代码 十分经典,有一种街机的感觉 奇葩青年的又一控制台神作. uses crt; type list=record ty,ax:integer; ...
- Nuget的使用笔记-(使用nuget发布dll到www.nuget.org)
Nuget是神马东东? 来自nuget.org官方的介绍 ----------------------------------------------------------------------- ...
- python基础实践(四)
# -*- coding:utf-8 -*-# Author:sweeping-monkwhy = "为什么要组织列表?"print(why)Chicken_soup = &quo ...
- http协议--留
1.http消息结构 *http客户端,即web浏览器,链接到服务器,向服务器发送一个http请求的目的 *http服务器,即web服务,接受请求,并向客户端发送http响应数据 http统一资源标识 ...
- rest_framework_jwt
安装配置 安装 pip install djangorestframework-jwt 配置 REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ...
- 孤荷凌寒自学python第二十四天python类中隐藏的私有方法探秘
孤荷凌寒自学python第二十四天python类中隐藏的私有方法探秘 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天发现了python的类中隐藏着一些特殊的私有方法. 这些私有方法不管我 ...
- leetcode 208. 实现 Trie (前缀树)
实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert(" ...
- Linux大小端模式转换函数
转自 http://www.cnblogs.com/kungfupanda/archive/2013/04/24/3040785.html 不同机器内部对变量的字节存储顺序不同,有的采用大端模式(bi ...
- Servlet 中 RequestDispacher 请求与分发
RequestDispacher 请求与分发使用HttpServletRequest的getRequestDispatcher()方法取得 Login.java页面 package control; ...
- ocrosoft Contest1316 - 信奥编程之路~~~~~第三关 问题 I: 寻找大富翁
http://acm.ocrosoft.com/problem.php?cid=1316&pid=8 题目描述 浙江杭州某镇共有n个人,请找出该镇上的前m个大富翁. 输入 输入包含多组测试用例 ...