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. windows下Word使用-快捷键

    1.word全屏显示——Alt+U+V 2.上标——Ctrl+Shift+= 3.下标——Ctrl+=

  2. C/C++中的段错误(Segmentation fault)[转]

    Segment fault 之所以能够流行于世,是与Glibc库中基本所有的函数都默认型参指针为非空有着密切关系的. 来自:http://oss.lzu.edu.cn/blog/article.php ...

  3. Java并发和多线程1:并发框架基本示例

    Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括ThreadPool,Executor,Executors,ExecutorService,Com ...

  4. MongoDB创建\更新\删除文档操作

     一.插入\创建文档 --当插入一个不存在的文档时,会自己主动创建一个文档 [root@racdb ~]# mongo MongoDB shell version: 2.4.14 connecti ...

  5. Unity3d修炼之路:用Mesh绘制一个Cube

    #pragma strict function Awake(){ var pMeshFilter : MeshFilter = gameObject.AddComponent(typeof(MeshF ...

  6. log4j.xml打印日志信息(2)

    log4j.xml文件 <? xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:con ...

  7. java-集合类(二)

    使用场景 1.多线程 (1)在jdk1.5之前原始的集合类中,仅仅有vector.stack.hashtable.enumeration等是线程安全的,其它的都是非线程安全的. 非线程安全的集合在多线 ...

  8. Android studio 分32位64位版本吗?

    下载的时候,是不分32位和64位的.安装完成之后,在bin目录下,有studio.exe和studio64.exe这两个文件.前一个是32位的,后一个是64位的.根据自己的电脑进行选择.

  9. caffe环境配置2

    参考链接: http://blog.csdn.net/enjoyyl/article/details/47397505 http://blog.csdn.net/baobei0112/article/ ...

  10. win10+ubuntu双系统卸载ubuntu

    进入win10下载EasyUEFI,删除ubuntu的引导项.重启如果直接进入了win10,表示卸载成功了.然后可以格式化ubuntu的分区.