Successor HDU - 4366(线段树+dfs)

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
不知道这个题咋设计的这么折磨人。老板要抄某个人的鱿鱼,但是需要有人代替他。这个人就是它的员工中工作能力比他高,而且是忠诚度最高的人。好奢侈的要求。。
很明显就是一棵树,dfs序是必须的。对于这种两个参数的东西,我们都是固定下来一个(使一个有序)去安排另一个。既然是替代他的人必须能力值比他高,那么我们将能力值由大到小排序后,再依次插入到线段树里,线段树里维护的就是区间忠诚度最大值了。这样查询的时候只需要去找忠诚度最大值就好了。
代码如下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;

const int maxn = 50010;
int t, n, m;
int ans[maxn];
int L[maxn], R[maxn], idx;
bool vis[maxn];
int Max[maxn<<2];
map<int, int> mp;
vector<int> v[maxn];

struct Staff {
    int b, c, id;
}s[maxn];

bool cmp(Staff s1, Staff s2)
{
    if (s1.c != s2.c) {
        return s1.c > s2.c;
    } else {
        return s1.id < s2.id;
    }
}

void build(int l, int r, int rt)
{
    Max[rt] = -1;
    if (l == r) {
        return ;
    }
    int m = (l + r) >> 1;
    build(l, m, rt << 1);
    build(m + 1, r, rt << 1 | 1);
    return ;
}

void update(int l, int r, int rt, int p, int c)
{
    if (l == r) {
        Max[rt] = c;
        return ;
    }
    int m = (l + r) >> 1;
    if (p <= m) {
        update(l, m, rt << 1, p, c);
    } else {
        update(m + 1, r, rt << 1 | 1, p, c);
    }
    Max[rt] = max(Max[rt<<1], Max[rt<<1|1]);
    return ;
}

int query(int l, int r, int rt, int L, int R)
{
    if (L <= l && R >= r) {
        return Max[rt];
    }
    int m = (l + r) >> 1;
    int ret = -99999;
    if (L <= m) {
        ret = max(ret, query(l, m, rt << 1, L, R));
    }
    if (R > m) {
        ret = max(ret, query(m + 1, r, rt << 1 | 1, L, R));
    }
    return ret;
}

void dfs(int x)
{
    L[x] = idx;
    vis[x] = true;
    int size = v[x].size();
    for (int i = 0; i < size; ++i) {
        if (!vis[v[x][i]]) {
            idx++;
            dfs(v[x][i]);
        }
    }
    R[x] = idx;
}

int main()
{
    scanf("%d", &t);
    while (t--) {
        for (int i = 0; i < maxn; ++i) {
            v[i].clear();
        }
        mp.clear();
        scanf("%d%d", &n, &m);
        int a, b, c;
        for (int i = 1; i <= n - 1; ++i) {
            scanf("%d%d%d", &a, &b, &c);
            v[a].push_back(i);
            s[i].b = b;
            s[i].c = c;
            s[i].id = i;
            mp[b] = i;
        }
        mp[-1] = -1;
        sort(s + 1, s + n, cmp);
        build(1, n - 1, 1);
        idx = 0;
        memset(vis, false, sizeof(vis));
        dfs(0);
        for (int i = 1; i <= n - 1; ++i) {
            int ret = query(1, n - 1, 1, L[s[i].id], R[s[i].id]);
            ans[s[i].id] = mp[ret];
            update(1, n - 1, 1, L[s[i].id], s[i].b);
        }
        int num;
        for (int i = 0; i < m; ++i) {
            scanf("%d", &num);
            printf("%d\n", ans[num]);
        }
    }
    return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值