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下的ubuntu

    办公用Windows确实方便,但对于开发和学习还是用Linux比较好. 在Windows下安装Linux子系统 windows10中推出了Linux子系统,这个功能对开发和学习来说真的很好,非常方便. ...

  2. 小学生绞尽脑汁也学不会的python(反射)

    小学生绞尽脑汁也学不会的python(反射) 1. issubclass, type, isinstance issubclass 判断xxxx类是否是xxxx类的子类 type 给出xxx的数据类型 ...

  3. 2015 Multi-University Training Contest 6 hdu 5358 First One

    First One Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total ...

  4. ubuntu 14.04服务器上使用nginx搭建wordpress博客详解

    过程详解 1.更新apt-get sudo apt-get update 2.安装nginx sudo apt-get install nginx 3.启动nginx sudo service ngi ...

  5. CSVHelper读出乱码 解决方案

    using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read)) using (StreamRe ...

  6. ASP.NET-Request对象

    前言:Request对象主要用于获取来自客户端的数据,如用户填入表单的数据.保存在客户端的Cookie等. 一.Request对象概述 1.主要属性  ApplicationPath  获取服务器上a ...

  7. shell文本过滤编程(一):grep和正則表達式

    [版权声明:转载请保留出处:blog.csdn.net/gentleliu.Mail:shallnew at 163 dot com] Linux系统中有非常多文件,比方配置文件.日志文件.用户文件等 ...

  8. javase复习

    一.总结封装 封装就是将数据和操作数据的方法绑定起来,通过private修饰数据,这样对数据的访问只能通过定义的操作数据的方法get/set来操作数据. 封装优点:1.由于将数据进行了封装,隐藏了不必 ...

  9. poj_1952最大下降子序列,统计个数

    其实不算难的一道题,但憋了我好久,嗯,很爽. #include<iostream> #include<cstdio> #include<string.h> #inc ...

  10. Kettle和ETL的基本构成

    不多说,直接上干货! 这里,我说的通俗易懂点,好方便大家的理解. ETL解决方案就像业务流程一样,具有输入.输出,以及一个或多个工作环节,处理步骤.同样的,这些步骤也具有输入和输出,并可以执行将一个输 ...