zoukankan      html  css  js  c++  java
  • 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
    ",ret[fa]);
    73         }
    74     }
    75     return 0;
    76 }
    View Code
  • 相关阅读:
    使用pynlpir增强jieba分词的准确度
    graph easy绘制ascii简易流程图
    kubernetes版本融合解决方案
    设计的一些kubernetes面试题目
    使用go-template自定义kubectl get输出
    docker、oci、runc以及kubernetes梳理
    启动docker容器时的Error response from daemon: devmapper: Error mounting: invalid argument. 错误解决
    kubernetes endpoint一会消失一会出现的问题剖析
    docker启动容器报错: could not synchronise with container process: not a directory
    docker中执行sed: can't move '/etc/resolv.conf73UqmG' to '/etc/resolv.conf': Device or resource busy错误的处理原因及方式
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4694603.html
Copyright © 2011-2022 走看看