当前位置: 首页 > news >正文

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

题目描述

The cows have once again tried to form a startup company, failing to remember from past experience t
hat cows make terrible managers!The cows, conveniently numbered 1…N1…N (1≤N≤100,000), organize t
he company as a tree, with cow 1 as the president (the root of the tree). Each cow except the presid
ent has a single manager (its "parent" in the tree). Each cow ii has a distinct proficiency rating, 
p(i), which describes how good she is at her job. If cow ii is an ancestor (e.g., a manager of a man
ager of a manager) of cow jj, then we say jj is a subordinate of ii.
Unfortunately, the cows find that it is often the case that a manager has less proficiency than seve
ral of her subordinates, in which case the manager should consider promoting some of her subordinate
s. Your task is to help the cows figure out when this is happening. For each cow ii in the company, 
please count the number of subordinates jj where p(j)>p(i).
n只奶牛构成了一个树形的公司,每个奶牛有一个能力值pi,1号奶牛为树根。
问对于每个奶牛来说,它的子树中有几个能力值比它大的。

输入

The first line of input contains N
The next N lines of input contain the proficiency ratings p(1)…p(N) 
for the cows. Each is a distinct integer in the range 1…1,000,000,000
The next N-1 lines describe the manager (parent) for cows 2…N 
Recall that cow 1 has no manager, being the president.
n,表示有几只奶牛 n<=100000
接下来n行为1-n号奶牛的能力值pi
接下来n-1行为2-n号奶牛的经理(树中的父亲)

输出

Please print N lines of output. The ith line of output should tell the number of 
subordinates of cow ii with higher proficiency than cow i.
共n行,每行输出奶牛i的下属中有几个能力值比i大

样例输入

5
804289384
846930887
681692778
714636916
957747794
1
1
2
3

样例输出

2
0
1
0
0
 
线段树合并练习题。
其实这道题直接用线段树查询子树区间就能做,但为了练一下线段树合并,所以写了一下线段树合并。
首先因为权值太大要离散化。
每个点动态开点建一棵权值线段树,每个点维护区间权值个数,从根开始dfs,回溯时合并每个点和子节点的线段树,都合并完之后单点查询即可。
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m;
int x;
int tot;
int cnt;
char ch[2];
int v[100010];
int h[100010];
int to[100010];
int ans[100010];
int ls[2000010];
int rs[2000010];
int sum[2000010];
int next[100010];
int head[100010];
int root[100010];
void add(int x,int y)
{
    tot++;
    next[tot]=head[x];
    head[x]=tot;
    to[tot]=y;
}
void insert(int &rt,int l,int r,int k)
{
    if(!rt)
    {
        rt=++cnt;
    }
    if(l==r)
    {
        sum[rt]++;
        return ;
    }
    int mid=(l+r)>>1;
    if(k<=mid)
    {
        insert(ls[rt],l,mid,k);
    }
    else
    {
        insert(rs[rt],mid+1,r,k);
    }
    sum[rt]=sum[ls[rt]]+sum[rs[rt]];
}
void merge(int &x,int y)
{
    if(!x||!y)
    {
        x=x+y;
        return ;
    }
    sum[x]=sum[x]+sum[y];
    merge(ls[x],ls[y]);
    merge(rs[x],rs[y]);
}
int query(int rt,int l,int r,int k)
{
    if(l==r)
    {
        return 0;
    }
    int mid=(l+r)>>1;
    if(k<=mid)
    {
        return query(ls[rt],l,mid,k)+sum[rs[rt]];
    }
    else
    {
        return query(rs[rt],mid+1,r,k);
    }
}
void dfs(int x)
{
    for(int i=head[x];i;i=next[i])
    {
        dfs(to[i]);
        merge(root[x],root[to[i]]);
    }
    ans[x]=query(root[x],1,m,v[x]);
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&v[i]);
        h[i]=v[i];
    }
    sort(h+1,h+n+1);
    m=unique(h+1,h+n+1)-h-1;
    for(int i=2;i<=n;i++)
    {
        scanf("%d",&x);
        add(x,i);
    }
    for(int i=1;i<=n;i++)
    {
        v[i]=lower_bound(h+1,h+1+m,v[i])-h;
        insert(root[i],1,m,v[i]);
    }
    dfs(1);
    for(int i=1;i<=n;i++)
    {
        printf("%d\n",ans[i]);
    }
}

转载于:https://www.cnblogs.com/Khada-Jhin/p/9765146.html

相关文章:

  • Bokeh中数据的添加、修改和筛选 | Bokeh 小册子
  • 2018-2019-1 20165226 《信息安全系统设计基础》第3周学习总结
  • Spring boot初体验
  • Angular2入门教程-2 实现TodoList App
  • 3、桶排序
  • Oracle DB 优化-AWR及相关内容
  • 大话 JavaScript 动画
  • Pycharm的使用一
  • iOS__上传应用到AppStore出现Authenticating with the iTunes store
  • 数字联盟刘晶晶:四年只做一个产品
  • java 通过Unsafe不使用构造器直接创建对象
  • jenkins配置用户角色权限,根据不同权限显示视图、Job
  • JVM虚拟机(五):JDK8内存模型—消失的PermGen
  • Java8 new Time Api
  • Ansible常用模块详解
  • ➹使用webpack配置多页面应用(MPA)
  • 230. Kth Smallest Element in a BST
  • bootstrap创建登录注册页面
  • Debian下无root权限使用Python访问Oracle
  • echarts花样作死的坑
  • node入门
  • 从0实现一个tiny react(三)生命周期
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 构建二叉树进行数值数组的去重及优化
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 离散点最小(凸)包围边界查找
  • 排序算法学习笔记
  • 前端路由实现-history
  • 如何借助 NoSQL 提高 JPA 应用性能
  • 使用putty远程连接linux
  • 原生 js 实现移动端 Touch 滑动反弹
  • ​2021半年盘点,不想你错过的重磅新书
  • ![CDATA[ ]] 是什么东东
  • #pragma once与条件编译
  • (C语言)共用体union的用法举例
  • (八)Spring源码解析:Spring MVC
  • (机器学习-深度学习快速入门)第三章机器学习-第二节:机器学习模型之线性回归
  • (蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)
  • (六)vue-router+UI组件库
  • ***检测工具之RKHunter AIDE
  • ***原理与防范
  • .helper勒索病毒的最新威胁:如何恢复您的数据?
  • .Net 访问电子邮箱-LumiSoft.Net,好用
  • .NET 设计模式初探
  • .Net(C#)常用转换byte转uint32、byte转float等
  • .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)
  • .pyc文件还原.py文件_Python什么情况下会生成pyc文件?
  • @DateTimeFormat 和 @JsonFormat 注解详解
  • [20160902]rm -rf的惨案.txt
  • [2019.3.5]BZOJ1934 [Shoi2007]Vote 善意的投票
  • [ABP实战开源项目]---ABP实时服务-通知系统.发布模式
  • [LeetCode] Merge Two Sorted Lists
  • [LitCTF 2023]Http pro max plus
  • [LOJ161] 仙人掌计数
  • [node] Node.js的文件系统