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. Multipartfile与File类型相互转换

    特殊情况下需要做转换 1.M转F File file = new File(path); FileUtils.copyInputStreamToFile(multipartFile.getInputS ...

  2. js(Mandango:壮汉专用,电影院划位工具)

    Mandango:壮汉专用,电影院划位工具 <body onload="initSeats();"> <div style="margin-top:75 ...

  3. Unity 多场景打包

    本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/50580641 作者:car ...

  4. 一行代码解决IE兼容性问题

    在网站开发中不免因为各种兼容问题苦恼,针对兼容问题,其实IE给出了解决方案Google也给出了解决方案百度也应用了这种方案去解决IE的兼容问题 百度源代码如下 <!Doctype html> ...

  5. C/C++ Swap without using extra variable

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50255379 对于可以线性运算的变量, ...

  6. easyui datagrid 动态加入、移除editor

    使用easyui 行编辑的时候完毕编辑的功能比較简单,可是假设要依据一个框的值动态改变别的值或者编辑的时候禁用某个框的时候就比較麻烦了. 比方像以下这样:加入行的时候每一个值都是手动输入,改动的时候第 ...

  7. 文件类似性推断 -- SimHash

    近期调研了一下simhash算法,它主要用在谷歌网页去重中.网上有非常多原理性的介绍. 既然能够用来推断文件的相似性,就想知道效果怎么样.simhash的准确度是否依赖于分词算法?是否和simhash ...

  8. POJ 题目2774 Long Long Message(后缀数组,求最长公共子串长度)

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 23696   Accepted: 97 ...

  9. UVA 11294 - Wedding(Two-Set)

    UVA 11294 - Wedding 题目链接 题意:有n对夫妻,0号是公主.如今有一些通奸关系(男男,女女也是可能的)然后要求人分配在两側.夫妻不能坐同一側.而且公主对面一側不能有两个同奸的人,问 ...

  10. tableau desktop(三)--构建数据视图(二)

    前段时间忙于工作的事情,好久没有来记录一点东西了,今天利用周末做点记录吧,近期因为工作的原因,也有两三周没实用tableau了.今天继续上一篇构建数据试图(二). 3.7 參考线和參考区间 參考线通经 ...