Successor

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2631    Accepted Submission(s): 634

Problem Description
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
 
Author
FZU
 

  题目的意思是给出一棵树, 然后树上每个节点有能力值,忠诚值。给出m个询问,要你找出某个结点后代中,忠诚值最高且能力值比它大的。

  一开始思路不难想出要 , 按照能力值从大到小,编号从小到大排完再插入( 因为题目给出编号小的是上司 ,所以要先插入 ,因为如果下属先插入

的话 ,如果它属于同它能力值相同的上司的后代,他有可能影响了它上司的更新,没有符合能力值严格大于的条件 )。然后取出忠诚值最大的话就用一颗

线段树处理就OK了。关键是怎么保证取的范围是属于这个节点的后代呢, 我就是卡了这个 ,其实,处理完DFS序以后,那个区间正正是该节点的后代了。

最后单点更新该点到DFS序的起始点中就可以了。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <algorithm> using namespace std;
typedef long long LL;
typedef pair<LL,int> pii;
const int N = ;
const int inf = 1e9+; #define X first
#define Y second
#define root 1,n,1
#define lr rt<<1
#define rr rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1 int n,m; struct node{
int id,b,c;
bool operator<(const node &a)const{
if( c != a.c )return c>a.c;
else return id < a.id ;
}
}e[N]; int pos1[N] , pos2[N] , dfs_clock , ans[N] , F[N]; vector<int>g[N]; void Init() {
for( int i = ; i < N ; ++i ) g[i].clear();
}
void dfs( int u ) {
pos1[u] = pos2[u] = ++dfs_clock;
for( int i = ; i < g[u].size() ; ++i ) {
int v = g[u][i];
dfs(v); pos2[u] = pos2[v];
}
} int date[N<<] ; void Up( int rt ) {
if( date[rr] == - || ( F[ date[lr] ] >= F[ date[rr] ] ) ) date[rt] = date[lr] ;
else date[rt] = date[rr];
} void Build( int l , int r , int rt ) {
date[rt] = - ;
if( l == r ) return ;
int mid = (l+r)>>;
Build(lson) , Build(rson) ;
} void Update( int l , int r , int rt , int x ,int id ) {
if( l == r ) {
date[rt] = id ; return ;
}
int mid = (l+r)>>;
if( x <= mid ) Update(lson,x,id);
else Update(rson,x,id);
Up(rt);
} int Query( int l , int r , int rt , int L , int R ) {
if( l == L && r == R ) {
return date[rt];
}
int mid = (l+r)>>;
if( R <= mid ) return Query(lson,L,R);
else if( L > mid ) return Query(rson,L,R);
else {
int id1 = Query(lson,L,mid) , id2 = Query( rson,mid+,R ) ;
if( id2 == - || ( F[id1] >= F[id2] ) ) return id1 ;
else return id2 ;
}
} void Solve() {
dfs_clock = ; dfs();
Build(root);
sort( e , e + n ) ;
for( int i = ; i < n ; ++i ) {
int id = e[i].id ;
ans[id] = Query(root,pos1[id],pos2[id]);
Update(root,pos1[id],id );
}
} void Read() {
scanf("%d%d",&n,&m);
e[].id = , e[].c = e[].b = inf , F[] = inf ;
for( int i = ; i < n ; ++i ) {
int fa ;scanf("%d%d%d",&fa,&e[i].b,&e[i].c);
e[i].id = i ; F[i] = e[i].b ;
g[fa].push_back(i);
}
} void Output() {
while( m-- ) {
int x ;scanf("%d",&x);
printf("%d\n",ans[x]);
}
}
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
ios::sync_with_stdio(false);
int _ ; scanf("%d",&_);
while(_--) Init() , Read() , Solve() , Output() ;
}

HDU 4366 Successor( DFS序+ 线段树 )的更多相关文章

  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.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

  3. HDU - 4366 Successor DFS序 + 分块暴力 or 线段树维护

    给定一颗树,每个节点都有忠诚和能力两个参数,随意指定一个节点,要求在它的子树中找一个节点代替它,这个节点要满足能力值大于它,而且是忠诚度最高的那个. 首先,dfs一下,处理出L[i], R[i]表示d ...

  4. 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 ...

  5. Assign the task HDU - 3974(dfs序+线段树)

    There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...

  6. HDU 5692 Snacks(DFS序+线段树)

    Snacks Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  7. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

  8. 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 130[Submit][Status][Discuss] D ...

  9. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

随机推荐

  1. git账号失效问题解决

    linux开发机上,使用某人账号,进行代码同步.该员工离职,导致该git账号不可用. 此时需要完成3步配置: 1.生成新的公私秘钥:在~/.ssh/config中把私钥文件路径 append到文件末尾 ...

  2. 【学习总结】gcc和gdb

    目录 <> vim.gcc.gdb: gcc: gcc和g++是c/c++的linux系统集成的编译器,源文件的后缀应为 .C/.cpp/.c++/.cc等 编译器可以将C.C++等语言源 ...

  3. 2018-2-13-WPF-延迟加载

    title author date CreateTime categories WPF 延迟加载 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23:3 + ...

  4. scp - 安全复制(远程文件复制程序)

    总览 SYNOPSIS scp -words [-pqrvBC1246 ] [-F ssh_config ] [-S program ] [-P port ] [-c cipher ] [-i ide ...

  5. python常用函数 D

    defaultdict(set_type) 可以定义字典多值映射,入参类型决定value类型. 例子: deque:(int) 保留最后N个元素 例子: Decimal(float) 直接对浮点数进行 ...

  6. Sass函数:Sass Maps的函数-map-has-key($map,$key)

    map-has-key($map,$key) 函数将返回一个布尔值.当 $map 中有这个 $key,则函数返回 true,否则返回 false. 前面的示例,当 $key 不在 $map 中时,使用 ...

  7. MFC程序执行过程剖析(转)

    一 MFC程序执行过程剖析 1)我们知道在WIN32API程序当中,程序的入口为WinMain函数,在这个函数当中我们完成注册窗口类,创建窗口,进入消息循环,最后由操作系统根据发送到程序窗口的消息调用 ...

  8. [BZOJ4826] [HNOI2017] 影魔 单调栈 主席树

    题面 因为是一个排列,所以不会有重复的.如果有重复就没法做了.一开始没有仔细看题目想了半天. 发现,如果是第一种情况,那么边界\(l\)和\(r\)就应该分别是整个区间的最大值和次大值. 然后,对于那 ...

  9. UVA10779 Collectors Problem 【迁移自洛谷博客】

    这是一道不错的练最大流建模的基础题. 这种题目审题是关键. Bob's friends will only exchange stickers with Bob, and they will give ...

  10. zoj 3325 Machine(线段树)

    题意:0~n-1的数组,初始值为0:执行m个操作,每次操作执行后输出当前值为0的连续段的段数. 操作1: p i j : i~j区间的每个元素值减1 操作2: r i j :i~j区间的每个元素值加1 ...