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. HDU 2079 选课时间(母函数模板题)

    链接:传送门 思路:母函数模板题 /************************************************************************* > Fil ...

  2. 【转】C语言将字符串转换成对应的数字(十进制、十六进制)

    转自:http://wawlian.iteye.com/blog/1315133 1.一个十进制数字的字符串表示转换成对应的整数.举例:将“1234”转换成整数1234 /*将字符串s转换成相应的整数 ...

  3. ASP.NET-AJAX.FORM提交附件失败

    尝试了不少时间在AJAX.FORM提交附件,发现完全不行,经过下面的这个博客的介绍,使用ajax.form.js插件提交成功,记录一下该博文网址和结论: 相关网址:http://www.cnblogs ...

  4. PyQt: LineEdit的智能输入提示

    使用的的类是QtGui.QCompleter from PyQt4 import QtGui,QtCore str = QtCore.QStringList(['a','air','airbus']) ...

  5. 设计网页录入信息与自己定义server数据接收

    需求:设计一个注冊网页用于录入username和登录password.并将数据传入server并显示出来. 1.前言:网页提交的 get 和 post 两种方式. (1)对于get提交方式,以本文中样 ...

  6. Windows环境下教你用Eclipse ADT 插件生成.h/.so文件,Java下调用JNI,轻松学习JNI

    准备工作:Eclipse ADT IDE 开发工具,NDK .Java 环境,博主的配置是:Windows x86 , ADT Build: v22.3.0-887826 , JAVA 1.7, ND ...

  7. android:异步任务asyncTask介绍及异步任务下载图片(带进度条)

    为什么要用异步任务? 在android中仅仅有在主线程才干对ui进行更新操作.而其他线程不能直接对ui进行操作 android本身是一个多线程的操作系统,我们不能把全部的操作都放在主线程中操作 .比方 ...

  8. bzoj1045: [HAOI2008] 糖果传递(数论)

    1045: [HAOI2008] 糖果传递 题目:传送门(双倍经验3293) 题解: 一开始想着DP贪心一顿乱搞,结果就GG了 十分感谢hzwer大佬写的毒瘤数论题解: 首先,最终每个小朋友的糖果数量 ...

  9. redis在项目中的使用(单机版、集群版)

    1.下载jar包:jedis-2.6.2.jar 2.代码: JedisDao.java: package com.test.www.dao; public interface JedisDao { ...

  10. oracle锁表进行关闭

    --查询被锁表 select 'alter system kill session '''||sess.sid||','||sess.serial#||''';', sess.sid, sess.se ...