BZOJ4756:[USACO]Promotion Counting(线段树合并)
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
Solution
Code
#include<iostream>
#include<cstdio>
#define N (100009)
#define INF (1000000000)
using namespace std; struct Sgt{int ls,rs,val;}Segt[N<<];
struct Edge{int to,next;}edge[N<<];
int n,x,sgt_num,a[N],ans[N],Root[N];
int head[N],num_edge; void add(int u,int v)
{
edge[++num_edge].to=v;
edge[num_edge].next=head[u];
head[u]=num_edge;
} void Update(int &now,int l,int r,int x)
{
if (!now) now=++sgt_num;
Segt[now].val++;
if (l==r) return;
int mid=(l+r)>>;
if (x<=mid) Update(Segt[now].ls,l,mid,x);
else Update(Segt[now].rs,mid+,r,x);
} int Query(int now,int l,int r,int l1,int r1)
{
if (l>r1 || r<l1) return ;
if (l1<=l && r<=r1) return Segt[now].val;
int mid=(l+r)>>,ls=Segt[now].ls,rs=Segt[now].rs;
return Query(ls,l,mid,l1,r1)+Query(rs,mid+,r,l1,r1);
} void Merge(int &x,int y)
{
if (!x || !y) {x|=y; return;}
Segt[x].val+=Segt[y].val;
Merge(Segt[x].ls,Segt[y].ls);
Merge(Segt[x].rs,Segt[y].rs);
} void DFS(int x,int fa)
{
for (int i=head[x]; i; i=edge[i].next)
if (edge[i].to!=fa)
{
DFS(edge[i].to,x);
Merge(Root[x],Root[edge[i].to]);
}
ans[x]=Query(Root[x],,INF,a[x]+,INF);
} int main()
{
scanf("%d",&n);
for (int i=; i<=n; ++i)
{
scanf("%d",&a[i]);
Update(Root[i],,INF,a[i]);
}
for (int i=; i<=n; ++i)
{
scanf("%d",&x);
add(i,x); add(x,i);
}
DFS(,);
for (int i=; i<=n; ++i)
printf("%d\n",ans[i]);
}
BZOJ4756:[USACO]Promotion Counting(线段树合并)的更多相关文章
- BZOJ4756: [Usaco2017 Jan]Promotion Counting(线段树合并)
题意 题目链接 Sol 线段树合并板子题 #include<bits/stdc++.h> using namespace std; const int MAXN = 400000, SS ...
- 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——线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 线段树合并裸题.那种返回 int 的与传引用的 merge 都能过.不知别的题是不是这 ...
- bzoj 4756 Promotion Counting —— 线段树合并
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4756 合并子树的权值线段树: merge 返回 int 或者是 void 都可以. 代码如下 ...
- 【dsu || 线段树合并】bzoj4756: [Usaco2017 Jan]Promotion Counting
调半天原来是dsu写不熟 Description The cows have once again tried to form a startup company, failing to rememb ...
- 线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数
题面:P3605 [USACO17JAN]Promotion Counting晋升者计数 题解:这是一道万能题,树状数组 || 主席树 || 线段树合并 || 莫队套分块 || 线段树 都可以写..记 ...
- 洛谷P3605 [USACO17JAN] Promotion Counting 晋升者计数 [线段树合并]
题目传送门 Promotion Counting 题目描述 The cows have once again tried to form a startup company, failing to r ...
- 2018.08.27 [Usaco2017 Jan]Promotion Counting(线段树合并)
描述 The cows have once again tried to form a startup company, failing to remember from past experienc ...
- [模板]BZOJ4756线段树合并
题面 Solution: 板子不解释 #include <iostream> #include <algorithm> #include <cstdio> #inc ...
随机推荐
- 简单的自定义Session
有关Session.Cookie机制建议参考文章:CookieSession机制详解,写的很详细,不再赘述 本篇文章通过一个简单的案例揭秘Session机制以及和Cookie的区别和联系: 服务器端代 ...
- [PHP] Yaf框架的简单安装使用
PHP开发组鸟哥惠新宸开发的php扩展框架 安装 windows下载扩展:https://pecl.php.net/package/yaf/2.2.9/windows 根据自己的电脑系统和php的版本 ...
- 深复制与浅复制&&strong,copy修饰符总结
又是一个老生常谈的话题,可是貌似这个问题,好多ios开发工程师并不能理解透彻,所以简单记录分析一下深复制与浅复制的原理以及strong,copy修饰符的原理和使用. 一.深复制与浅复制 ...
- [源码] 定义String s="abcd", 求长度
一般会答: s.length() 看源码是如何实现的: /** * Returns the length of this string. * The length is equal to the nu ...
- hdu 1401
Solitaire Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- 百度Ueditor编辑器
百度的Ueditor编辑器出于安全性考虑,用户在html模式下粘贴进去的html文档会自动被去除样式和转义.虽然安全的,但是非常不方便. 1. ueditor.config.js 做出如下修改即可: ...
- js小练习
1.题目:某班的成绩出来了,现在老师要把班级的成绩打印出来. 效果如下:xxx年xx月x日 星期x--班级总分为:81 格式要求:1.显示打印的日期.格式为类似“xxxx年xx月xx日 星期x”的当 ...
- C#动态创建lambda表达式
/// <summary> /// 创建lambda表达式:p=>true /// </summary> /// <typeparam name="T&q ...
- Code Signal_练习题_Array Replace
Given an array of integers, replace all the occurrences of elemToReplace with substitutionElem. Exam ...
- iOS中表视图单元格事件用nib和storyboard的两种写法总结
从ios6开始,苹果公司推出了storyborad技术取代了nib的写法,这样代码量确实少写了很多,也比较简洁.但是,从学习的角度来说,阿堂认为 用nib的写法,虽然多了些代码,但是对于掌握知识和原理 ...