HDU - 4366 Successor DFS区间+线段树
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区间+线段树的更多相关文章
- HDU 4366 Successor( DFS序+ 线段树 )
Successor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- 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 ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- hdu 1540 Tunnel Warfare (区间线段树(模板))
http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) ...
- Successor HDU - 4366 (预处理,线段树,dfs序)
Successor HDU - 4366 Sean owns a company and he is the BOSS.The other Staff has one Superior.every s ...
- HDU - 4366 Successor DFS序 + 分块暴力 or 线段树维护
给定一颗树,每个节点都有忠诚和能力两个参数,随意指定一个节点,要求在它的子树中找一个节点代替它,这个节点要满足能力值大于它,而且是忠诚度最高的那个. 首先,dfs一下,处理出L[i], R[i]表示d ...
- 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 ...
- hdu 5692 Snacks(dfs时间戳+线段树)
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- HDU 5692 Snacks(DFS序+线段树)
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
随机推荐
- Intellij IDEA 中 使用 Git
前一段时间使用 Microsoft 的 Visual Studio Code 中使用 Git 对前端项目进行项目代码的开发提交. 使用后感觉挺好的,用的多了也觉得挺简单方便的. 现在需要在 Intel ...
- 戴尔PowerEdge T110 Ⅱ服务器U盘安装Windows Server 2019 DataCenter
一. 下载准备 准备工作——下载Microsoft Windows Server 2019 官方简体中文激活版 (MSDN)原版iso镜像 准备工作——安装刻录软件UltraISO,单文件绿色版就够用 ...
- Java线程池的增长过程
通过ThreadPoolExecutor的方式创建线程池 ThreadPoolExecutor 构造方法: public ThreadPoolExecutor(int corePoolSize, in ...
- 【java提高】(17)---Java 位运算符
Java 位运算符 &.|.^.~.<<.>> 以前学过有关java的运算符,不过开发了这么久也很少用过这个.现在由于开发需要,所以现在再来回顾整理下有关java的运算 ...
- io流处理文件夹复制功能(java代码)
拷贝某个目录下得所有文件拷指定位置 思想归纳 首先我们需要做的先获取到资源文件夹路径,这里我们先在程序中写死,然后我们还需要一个目标文件夹就是你需要拷贝到哪里.有了这两个文件夹我就可以进行复制了 然后 ...
- html5新特性-header,nav,footer,aside,article,section等各元素的详解
Html5新增了27个元素,废弃了16个元素,根据现有的标准规范,把HTML5的元素按优先级定义为结构性属性.级块性元素.行内语义性元素和交互性元素四大类. 下面是对各标签的详解,section.he ...
- linux细节操作的
一>安装mysql 可以直接在linux系统下载 下载之前要安装wget插件 下载命令 wget 后面跟安装软件的url 比如mysql wget http://repo.mysql.com/m ...
- MQTT的学习之Mosquitto安装和使用
Mosquitto是一个实现了MQTT3.1协议的代理服务器,由MQTT协议创始人之一的Andy Stanford-Clark开发,它为我们提供了非常棒的轻量级数据交换的解决方案.本文的主旨在于记录M ...
- Web Worker 使用教程
一.概述 JavaScript 语言采用的是单线程模型,也就是说,所有任务只能在一个线程上完成,一次只能做一件事.前面的任务没做完,后面的任务只能等着.随着电脑计算能力的增强,尤其是多核 CPU 的出 ...
- LoRaWAN stack移植笔记(六)_调试2
前言 调试的过程中碰到的问题基本都是以前没有遇到过的,而且需要对整个协议栈及射频方面的工作流程较熟悉才能找到问题的原因,需要多读SX1276的数据手册以及与射频芯片的物理层通信例程和MAC层通信例程进 ...