Successor

Time Limit: 1000ms
Memory Limit: 65536KB

This problem will be judged on HDU. Original ID: 4366
64-bit integer IO format: %I64d      Java class name: Main

 
Sean owns a company and he is the BOSS.The other Staff has one Superior.every staff has a loyalty and ability.Some times Sean will fire one staff.Then one of the fired man’s Subordinates will replace him whose ability is higher than him and has the highest loyalty for company.Sean want to know who will replace the fired man.

 

Input

In the first line a number T indicate the number of test cases. Then for each case the first line contain 2 numbers n,m (2<=n,m<=50000),indicate the company has n person include Sean ,m is the times of Sean’s query.Staffs are numbered from 1 to n-1,Sean’s number is 0.Follow n-1 lines,the i-th(1<=i<=n-1) line contains 3 integers a,b,c(0<=a<=n-1,0<=b,c<=1000000),indicate the i-th staff’s superior Serial number,i-th staff’s loyalty and ability.Every staff ‘s Serial number is bigger than his superior,Each staff has different loyalty.then follows m lines of queries.Each line only a number indicate the Serial number of whom should be fired.

 

Output

For every query print a number:the Serial number of whom would replace the losing job man,If there has no one to replace him,print -1.

 

Sample Input

1
3 2
0 100 99
1 101 100
1
2

Sample Output

2
-1

Source

 
解题:线段树,dfs序列得出区间,然后将员工按能力降序排序,然后利用线段树查询即可!不过G++爆栈,选择C++交
 
 #include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <cstring>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int maxn = ;
struct node {
int maxv,id;
} tree[maxn<<];
void update(int L,int R,int lt,int rt,int val,int id,int v) {
if(lt <= L && rt >= R) {
tree[v].maxv = val;
tree[v].id = id;
return ;
}
int mid = (L + R)>>;
if(lt <= mid) update(L,mid,lt,rt,val,id,v<<);
if(rt > mid) update(mid+,R,lt,rt,val,id,v<<|);
if(tree[v<<].maxv > tree[v<<|].maxv)
tree[v] = tree[v<<];
else tree[v] = tree[v<<|];
}
node query(int L,int R,int lt,int rt,int v) {
if(lt <= L && rt >= R) return tree[v];
int mid = (L + R)>>;
node a,b;
a.maxv = -,b.maxv = -;
a.id = -,b.id = -;
if(lt <= mid) a = query(L,mid,lt,rt,v<<);
if(rt > mid) b = query(mid+,R,lt,rt,v<<|);
if(a.maxv > b.maxv) return a;
return b;
}
vector<int>g[maxn];
int L[maxn],R[maxn],ret[maxn],tt;
void dfs(int u) {
L[u] = tt++;
for(int i = g[u].size()-; i >= ; --i)
dfs(g[u][i]);
R[u] = tt-;
}
struct STAFF {
int id,ability,loyalty;
bool operator<(const STAFF &t)const {
if(ability == t.ability) return id < t.id;
return ability > t.ability;
}
} staff[maxn];
int main() {
int kase,n,m,fa;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d",&n,&m);
for(int i = tt = ; i <= n; ++i) g[i].clear();
for(int i = ; i < n; ++i) {
scanf("%d%d%d",&fa,&staff[i-].loyalty,&staff[i-].ability);
g[fa].push_back(i);
staff[i-].id = i;
}
dfs();
sort(staff,staff+n-);
memset(tree,-,sizeof tree);
for(int i = ; i < n-; ++i) {
node now = query(,R[],L[staff[i].id],R[staff[i].id],);
ret[staff[i].id] = now.id;
update(,R[],L[staff[i].id],L[staff[i].id],staff[i].loyalty,staff[i].id,);
}
while(m--) {
scanf("%d",&fa);
printf("%d\n",ret[fa]);
}
}
return ;
}

HDU 4366 Successor的更多相关文章

  1. HDU - 4366 Successor DFS区间+线段树

    Successor:http://acm.hdu.edu.cn/showproblem.php?pid=4366 参考:https://blog.csdn.net/colin_27/article/d ...

  2. hdu 4366 Successor - CDQ分治 - 线段树 - 树分块

    Sean owns a company and he is the BOSS.The other Staff has one Superior.every staff has a loyalty an ...

  3. HDU 4366 Successor(树链剖分+zkw线段树+扫描线)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4366 [题目大意] 有一个公司,每个员工都有一个上司,所有的人呈树状关系,现在给出每个人的忠诚值和 ...

  4. HDU 4366 Successor 分块做法

    http://acm.hdu.edu.cn/showproblem.php?pid=4366 今日重新做了这题的分块,果然是隔太久了,都忘记了.. 首先,用DFS序变成一维的问题 关键是它有两个权值, ...

  5. HDU 4366 Successor( DFS序+ 线段树 )

    Successor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  6. HDU 4366 Successor(dfs序 + 分块)题解

    题意:每个人都有一个上司,每个人都有能力值和忠诚值,0是老板,现在给出m个询问,每次询问给出一个x,要求你找到x的所有直系和非直系下属中能力比他高的最忠诚的人是谁 思路:因为树上查询很麻烦,所以我们直 ...

  7. HDU - 4366 Successor DFS序 + 分块暴力 or 线段树维护

    给定一颗树,每个节点都有忠诚和能力两个参数,随意指定一个节点,要求在它的子树中找一个节点代替它,这个节点要满足能力值大于它,而且是忠诚度最高的那个. 首先,dfs一下,处理出L[i], R[i]表示d ...

  8. Successor HDU - 4366 (预处理,线段树,dfs序)

    Successor HDU - 4366 Sean owns a company and he is the BOSS.The other Staff has one Superior.every s ...

  9. Successor hdu 4366 线段树

    题意: 现在n个人,其中编号0的是老板,之后n-1个员工,每个员工只有一个上司,有一个忠诚值和能力值.每次要解雇一个人的时候,从他的下属中选取能力值大于他的且忠诚值最高的一个,若不存在则输出-1.共m ...

随机推荐

  1. Codeforces 675B Restoring Painting

    链接:传送门 题意:给出3 × 3的方块,其中任意2 × 2的方块和左上角2 × 2的和相等,还给出9个格子中的4个--a,b,c,d ,在1~n中选择一些数(可重复)填入剩下5个格子中,问有多少种填 ...

  2. UVALive-8079 Making a Team 排列组合公式化简

    题目链接:https://cn.vjudge.net/problem/UVALive-8079 题意 n个人组队,队伍人数小于等于n,每个队伍需要4个不同的职务的领导. 问这n个人可以组成多少队? n ...

  3. Hive学习:Hive连接JOIN用例详解

    1 准备数据: 1.1 t_1 01 张三 02 李四 03 王五 04 马六 05 小七 06 二狗 1.2 t_2 01 11 03 33 04 44 06 66 07 77 08 88 1.3 ...

  4. 利用 ST-LINK Utility软件下载程序

    先在电脑上安装STM32 ST-LINK Utility,软件安装一路Next就可以了,安装好软件之后界面如下:    下载程序只需要使用3个图标就可以了 第一个图标Connect to the ta ...

  5. Mysql学习总结(34)——Mysql 彻底解决中文乱码的问题

    mysql 中常常出现对中文支持不友好的情况 常见的错误 "Illegal mix of collations for operation" 下面我们规整一下 mysql 数据库中 ...

  6. Nginx 实现反向代理

    Nginx 实现反向代理 两个域名指向同一台 Nginx 服务器,用户访问不同的域名显示不同的网页内容 两个域名分别是 www.test1.com www.test2.com 1.准备工作 下载及安装 ...

  7. UVALive 5545 Glass Beads

    Glass Beads Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origin ...

  8. Servlet体验之旅(二)——Session、Cookie

    我们知道Session和Cookie都是用于会话跟踪的,仅仅是实现的方式不大一样,那么他们到底有什么不同呢?以下跟着我脚步来了解一下: Session.Cookie的含义: Session 一种ser ...

  9. 【Android】Android程序自己主动更新

    App自己主动更新的步骤可分为三步: 检查更新(假设有更新进行第2步,否则返回) 下载新版的APK安装包 安装APK 以下对这三步进行解释.当中会穿插相应代码.App自己主动更新的这三步所有被封装到了 ...

  10. EF的CRUD

    已经知道EF就是一个能够使得编程人员用面向对象的思想操作数据库的框架,那么在最初学习SQL的时候我们就知道对数据库的操作就是增删改查.万变不离其宗. EF也是操作数据库的当然也就是要对数据库实现增删改 ...