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 <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
const int N = ; struct _edge{
int to,next;
};
_edge edge[N*];
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*][];
node man[N];
int ans[N];
int dfscnt;
void dfs(int u,int fa)
{
man[u].l = dfscnt++;
for(int e=head[u];e!=-;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>>=)
if(zkw[x][]<a)
zkw[x][]=a,zkw[x][]=b;
}
int query(int l,int r)
{
int a,b;
a=b=-;
for(l=l+M-,r=r+M+;l^r^;l>>=,r>>=)
{
if(~l& && zkw[l^][]>a) a=zkw[l^][],b=zkw[l^][];
if(r& && zkw[r^][]>a) a=zkw[r^][],b=zkw[r^][];
}
return b;
} void run()
{
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head));
ecnt=;
int a,b,c;
for(int i=;i<n;i++)
{
scanf("%d%d%d",&a,&b,&c);
addedge(a,i);
addedge(i,a);
man[i].a=c;
man[i].b=b;
man[i].id=i;
}
dfscnt=;
dfs(,-);
// for(int i=0;i<n;i++)
// printf("%d %d %d\n",i,man[i].l,man[i].r);
sort(man+,man+n); for(M=;M<=dfscnt+;M*=);
memset(zkw,-,sizeof(zkw)); stack<int> stk;
stk.push();
ans[man[].id]=-;
for(int i=;i<n;i++)
{
if(man[i].a!=man[i-].a)
{
while(!stk.empty())
{
int u = stk.top(); stk.pop();
add(man[u].l,man[u].b,man[u].id);
add(man[u].r,man[u].b,man[u].id);
}
}
stk.push(i);
ans[man[i].id] = query(man[i].l,man[i].r);
}
while(m--)
{
scanf("%d",&a);
printf("%d\n",ans[a]);
}
} int main()
{
freopen("case.txt","r",stdin);
int _;
scanf("%d",&_);
while(_--)
run();
return ;
}

最新文章

  1. 前端开发---ppt展示页面评论区展示
  2. PHP最简单的后门,且难查,不报毒!
  3. 配置log4j
  4. LabVIEW之生产者/消费者模式--队列操作 彭会锋
  5. XPath的使用
  6. Python 决策树的构造
  7. 重构第8天:使用委托代替继承(Replace Inheritance with Delegation)
  8. Effective Java 03 Enforce the singleton property with a private constructor or an enum type
  9. [原创] Web UI 自动化日期控件的处理
  10. 如何测量一个嵌入式Linux系统的功耗/power dissipation/power wastage/consumption
  11. 基于SAML的单点登录介绍
  12. SGU 111.Very simple problem
  13. .Net中如何使用MySql连接池
  14. java udp (使用类调用双通信)1
  15. int与string的相互转换
  16. java深入浅出之数据结构
  17. 『离散化 discrete』
  18. [机器学习入门篇]-Logistic函数与Softmax函数
  19. PTA最短工期
  20. git 不能创建分支

热门文章

  1. activemq 搭建--集群
  2. String代码示例
  3. Java 学习 day04
  4. python书写日志的重要性?
  5. 九度OJ 1163:素数 (素数)
  6. Moving Computation is Cheaper than Moving Data
  7. 【shell】获取第10+个位置参数
  8. P3231 [HNOI2013]消毒
  9. HTML5/CSS3动画下拉菜单
  10. 如何设置确认selinux 模式【转】