博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu4366 Successor (dfs序+zkw线段树)
阅读量:5059 次
发布时间:2019-06-12

本文共 2750 字,大约阅读时间需要 9 分钟。

Successor

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 2559    Accepted Submission(s): 613

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
 
 
题意: 给一棵树,每个结点有两个值a和b,再有m个查询,查询问一个点的编号u,要求找出u的后代中某个结点v,v.a值比u.a大,v.b是所有后代中最大的那个点编号
 
思路: 先按照员工关系构出一棵树, 然后将树的dfs深度序列转化为线性序列,按这个序列确定各员工在线段树中的位置。将员工按ability从大到小排序,然后逐个插入线段树相应位置,每次插入前查询以其为根的子树区间中loyalty的最大值及其对应员工。处理完上面之后,对于询问O(1)输出。
 
代码:
C++交会RE,然后要手动加栈,时间200+ms
G++不需要手动加栈就过了,时间300+ms
#include 
#include
#include
#include
#include
using namespace std;const int N = 50010;struct _edge{ int to,next;};_edge edge[N*2];int ecnt,head[N];void addedge(int u,int v){ edge[ecnt].to = v; edge[ecnt].next = head[u]; head[u] = ecnt++;}struct node{ int id,a,b,l,r; friend bool operator < (const node &a, const node &b) { return a.a>b.a; }};int n,m,M;int zkw[N*10][2];node man[N];int ans[N];int dfscnt;void dfs(int u,int fa){ man[u].l = dfscnt++; for(int e=head[u];e!=-1;e=edge[e].next) { int &v = edge[e].to; if(v==fa) continue; dfs(v,u); } man[u].r = dfscnt++;}void add(int x,int a,int b){ for(x+=M;x;x>>=1) if(zkw[x][0]
>=1,r>>=1) { if(~l&1 && zkw[l^1][0]>a) a=zkw[l^1][0],b=zkw[l^1][1]; if(r&1 && zkw[r^1][0]>a) a=zkw[r^1][0],b=zkw[r^1][1]; } return b;}void run(){ scanf("%d%d",&n,&m); memset(head,-1,sizeof(head)); ecnt=0; int a,b,c; for(int i=1;i
stk; stk.push(1); ans[man[1].id]=-1; for(int i=2;i

 

转载于:https://www.cnblogs.com/someblue/p/4018968.html

你可能感兴趣的文章
c#自定义控件中的事件处理
查看>>
App.config自定义节点读取
查看>>
unity3d根据手机串号和二维码做正版验证
查看>>
二十六、Android WebView缓存
查看>>
django Models 常用的字段和参数
查看>>
linux -- 嵌入式linux下wifi无线网卡驱动
查看>>
SVN使用教程总结
查看>>
SQL中varchar和nvarchar有什么区别?
查看>>
OpenCV矩阵运算总结
查看>>
Java Build Practice 4:Extend and Invoke Ant API
查看>>
[转] Transformer图解
查看>>
FreeBSD方式安装 MAC OSX
查看>>
Linux 根文件系统制作
查看>>
IOS--沙盒机制
查看>>
My.Ioc 的性能
查看>>
使用 JointCode.Shuttle 访问任意 AppDomain 的服务
查看>>
hdoj 1846 Brave Game(巴什博弈)
查看>>
Round #345 B. Beautiful Paintings(Div.2)
查看>>
51nod 1018排序
查看>>
sqlite的坑
查看>>