HDU 4366 Successor

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++交
 
 1 #include <iostream>
 2 #include <vector>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #include <cstring>
 6 #pragma comment(linker, "/STACK:102400000,102400000")
 7 using namespace std;
 8 const int maxn = 200010;
 9 struct node {
10     int maxv,id;
11 } tree[maxn<<2];
12 void update(int L,int R,int lt,int rt,int val,int id,int v) {
13     if(lt <= L && rt >= R) {
14         tree[v].maxv = val;
15         tree[v].id = id;
16         return ;
17     }
18     int mid = (L + R)>>1;
19     if(lt <= mid) update(L,mid,lt,rt,val,id,v<<1);
20     if(rt > mid) update(mid+1,R,lt,rt,val,id,v<<1|1);
21     if(tree[v<<1].maxv > tree[v<<1|1].maxv)
22         tree[v] = tree[v<<1];
23     else tree[v] = tree[v<<1|1];
24 }
25 node query(int L,int R,int lt,int rt,int v) {
26     if(lt <= L && rt >= R) return tree[v];
27     int mid = (L + R)>>1;
28     node a,b;
29     a.maxv = -1,b.maxv = -1;
30     a.id = -1,b.id = -1;
31     if(lt <= mid) a = query(L,mid,lt,rt,v<<1);
32     if(rt > mid) b = query(mid+1,R,lt,rt,v<<1|1);
33     if(a.maxv > b.maxv) return a;
34     return b;
35 }
36 vector<int>g[maxn];
37 int L[maxn],R[maxn],ret[maxn],tt;
38 void dfs(int u) {
39     L[u] = tt++;
40     for(int i = g[u].size()-1; i >= 0; --i)
41         dfs(g[u][i]);
42     R[u] = tt-1;
43 }
44 struct STAFF {
45     int id,ability,loyalty;
46     bool operator<(const STAFF &t)const {
47         if(ability == t.ability) return id < t.id;
48         return ability > t.ability;
49     }
50 } staff[maxn];
51 int main() {
52     int kase,n,m,fa;
53     scanf("%d",&kase);
54     while(kase--) {
55         scanf("%d%d",&n,&m);
56         for(int i = tt = 0; i <= n; ++i) g[i].clear();
57         for(int i = 1; i < n; ++i) {
58             scanf("%d%d%d",&fa,&staff[i-1].loyalty,&staff[i-1].ability);
59             g[fa].push_back(i);
60             staff[i-1].id = i;
61         }
62         dfs(0);
63         sort(staff,staff+n-1);
64         memset(tree,-1,sizeof tree);
65         for(int i = 0; i < n-1; ++i) {
66             node now = query(0,R[0],L[staff[i].id],R[staff[i].id],1);
67             ret[staff[i].id] = now.id;
68             update(0,R[0],L[staff[i].id],L[staff[i].id],staff[i].loyalty,staff[i].id,1);
69         }
70         while(m--) {
71             scanf("%d",&fa);
72             printf("%d\n",ret[fa]);
73         }
74     }
75     return 0;
76 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4694603.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值