Successor:http://acm.hdu.edu.cn/showproblem.php?pid=4366

参考:https://blog.csdn.net/colin_27/article/details/37811157

题意:

  有一个公司,每个员工组成一个树形结构,每个员工都有一个能力值和忠诚度。现在问你m次,每次问去掉某个人x,谁可以继任这个职位,要求继任的人是x的子员工,且能力值比x的能力值高,且忠诚度是其他子员工不可想象的(每个员工的忠诚度不同)。

思路:

  我感觉比较巧妙,但可能是一类题的套路。因为对于每个员工,取代他的人是一定的,所以我们直接预处理出所有的员工答案。首先要dfs,把每个员工 Y 变成一段区间,Y的每个子员工的区间的左端点都在Y的区间中。

对区间总长度建立一个线段树,每个节点表示对应区间的最大忠诚度的值。我们把每个人先按照能力从大到小排序,再一个一个的加到这颗线段树中,这样一来,对于每个点x,线段树中存在的点都是能力大于x的,所以我们就可以询问x对应区间的最大忠诚度,又由于每个人和忠诚度一一对应,映射一下就能得到是哪个人。注意这里对相同能力者的处理。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
#include <unordered_map>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxn = ;
int a[maxn],sum[maxn*],ans[maxn];
int le[maxn],ri[maxn];
// int hd[maxn*20];
unordered_map<int,int>hd;
int n,m,cnt;
vector<int>mp[maxn];
struct node {
int h,ab;
int id;
}per[maxn];
bool cmp(node a,node b){
return a.ab > b.ab;
}
void dfs(int u,int fa){ le[u] = ++cnt;
for(int i=; i<mp[u].size(); i++){
int v = mp[u][i];
if(v!=fa){
dfs(v,u);
}
}
ri[u] = ++cnt;
}
void build(int l,int r,int rt){
sum[rt] = -;
if(l==r){
return;
}
int mid = (l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
}
int query(int l,int r,int rt,int L,int R){
if(l >= L && r <= R){
return sum[rt];
}
int mid = (l + r)>>;
int ans = -;
if(mid >= L)ans = max(ans, query(l,mid,rt<<,L,R));
if(mid < R)ans = max(ans, query(mid+,r,rt<<|,L,R));
return ans;
}
void update(int l,int r,int rt,int p,int c){
if(l==r){
// cout<<"p="<<l<<"="<<c<<endl;
sum[rt] = c;
return;
}
int mid = (l + r)>>;
if(mid >= p)update(l,mid,rt<<,p,c);
else update(mid+,r,rt<<|,p,c);
sum[rt] = max(sum[rt<<], sum[rt<<|]);
}
int main(){
int cas; scanf("%d", &cas);
while(cas--){
scanf("%d%d", &n, &m);
// memset(hd, -1, sizeof(hd));
hd.clear();
memset(ans,-,sizeof(ans));
for(int i=; i<=n; i++)mp[i].clear();
for(int i=; i<n; i++){
int a,b,c;
scanf("%d%d%d", &a, &b, &c);
mp[a].pb(i);
per[i].h = b; per[i].ab = c; per[i].id = i;
hd[b] = i;
}
cnt = ;
dfs(,-);
build(,cnt,);
per[].id = ;
per[].h =per[].ab = -; sort(per,per+n,cmp);
for(int i=,j; i<n; i=j){
j = i; while(j<n && per[j].ab == per[i].ab){
int tmp = query(,cnt,,le[per[j].id],ri[per[j].id]);
//cout<<le[per[j].id]<<" "<<ri[per[j].id]<<endl;
if(tmp!=- && hd.count(tmp))ans[per[j].id] = hd[tmp];
else ans[per[j].id] = -;
j++;
}
for(int k=i; k<j; k++){
update(,cnt,,le[per[k].id],per[k].h);
}
}
while(m--){
int x;scanf("%d", &x);
printf("%d\n",ans[x]);
}
}
return ;
}

HDU 4366

HDU - 4366 Successor DFS区间+线段树的更多相关文章

  1. HDU 4366 Successor( DFS序+ 线段树 )

    Successor Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

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

  3. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

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

  4. hdu 1540 Tunnel Warfare (区间线段树(模板))

    http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) ...

  5. Successor HDU - 4366 (预处理,线段树,dfs序)

    Successor HDU - 4366 Sean owns a company and he is the BOSS.The other Staff has one Superior.every s ...

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

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

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

  8. hdu 5692 Snacks(dfs时间戳+线段树)

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

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

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

随机推荐

  1. ERROR 临时

    ERROR ITMS-4238: "Redundant Binary Upload. There already exists a binary upload with build vers ...

  2. 虚拟机ip地址从ipv6改为ipv4相关问题

    有一次打开虚拟机时,Xshell连接不上虚拟机,就很奇怪,然后查看虚拟机的ip地址,发现显示为ipv6格式,然后总结了两种情况如下: 第一种情况: onboot为no时显示ipv6地址, 改为yes即 ...

  3. SpringBoot开发案例之打造十万博文Web篇

    前言 通过 Python 爬取十万博文之后,最重要的是要让互联网用户访问到,那么如何做呢? 选型 从后台框架.前端模板.数据库连接池.缓存.代理服务.限流等组件多个维度选型. 后台框架 SpringB ...

  4. Linux基础文件打包

    一.打包与解压 (一).打包压缩 [root@linux ~]# tar -czf etc1.tar.gz /etc //-z 调用gzip [root@linux ~]# tar -cjf etc2 ...

  5. weblogic10.3.6漏洞修改方案

    1.CVE-2018-2628漏洞 CVE-2018-2628漏洞利用的第一步是与weblogic服务器开放在服务端口上的T3服务建立socket连接,可通过控制T3协议的访问来临时阻断攻击行为. W ...

  6. 为什么unsigned (-1)表示无符号整数的最大值

    整数在计算机中的表示 在计算机中,整数采用补码表示.当前主流编译器中整型在内存中占用四个字节,共32位. 机器数:一个数在计算机中的二进制表示形式, 叫做这个数的机器数.机器数是带符号的,在计算机用一 ...

  7. 利用python自动生成verilog模块例化模板

    一.前言 初入职场,一直忙着熟悉工作,就没什么时间更新博客.今天受“利奇马”的影响,只好宅在家中,写写技术文章.芯片设计规模日益庞大,编写脚本成了芯片开发人员必要的软技能.模块端口动不动就几十上百个, ...

  8. 聊聊目标管理之 OKR

    这篇文章我们不谈技术,聊点轻松的,那聊什么呢?聊一下最近很火的目标管理 OKR.不知道小伙伴你们的公司什么情况,我的公司今年开始推行 OKR,用了大半年的时间,感觉效果还不错,上周六又参加了一天的复盘 ...

  9. loadrunner中的ie浏览器无法使用

    我的loadrunner是12.55版本的,windows10系统 在我们学习loadrunner的过程中,会出现下面一个问题: 在录制脚本时,loadrunner中的ie浏览器无法使用处于飘红状态. ...

  10. python面试总结1(基础章节)

    python语言基础 语言特点 python是静态还是动态类型?是强类型还是弱类型 动态强类型语言 动态还是静态指的是编译期还是运作期确定类型 强类型指的是不会发生隐式类型转换 python作为后端语 ...