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 ...
随机推荐
- Win常用软件
本节只适合windows系统 VScode 下载 安装 双击安装 打开目录方式 右键文件夹->使用VSCode打开 命令行打开 code folder [dzlua@win10:~]$ ls a ...
- Redis 学习笔记(篇七):Redis 持久化
因为 Redis 是内存数据库,它将自己的数据储存在内存里面,所以如果不想办法将储存在内存中的数据库状态保存到磁盘里面,那么一旦服务器进程退出,服务器中的数据也将会丢失,为了解决这个问题,Redis ...
- 使用jvisualvm.exe工具远程监视tomcat的线程运行状态
一.简述 在web项目中,常使用tomcat作为web容器.代码编写的时候,由于业务需要,也常会使用线程机制.在系统运行一段时间之后,若出现响应慢或线程之间出现死锁的情况,要查出问题所在,需要使用jd ...
- 大数阶乘(c++实现)
#include <iostream>using namespace std;#define N 1000int BigNumFactorial(int Num[], int n);voi ...
- JAVA面向对象面试题带答案(墙裂推荐)
1) 在Java中,如果父类中的某些方法不包含任何逻辑,并且需要有子类重写,应该使用(c)关键字来申明父类的这些方法. a) Finalc b) Static c) Abstract d) Void2 ...
- Leetcode的SQL题解:185. 部门工资前三高的员工
题目 查询部门工资前三高的员工. 我用的数据库是oracle. 下面是数据表的信息. Employee表数据: | ID | NAME | Salary | DepartmentId | | -- | ...
- Go中的命名规范
1.命名规范 1.1 Go是一门区分大小写的语言. 命名规则涉及变量.常量.全局函数.结构.接口.方法等的命名. Go语言从语法层面进行了以下限定:任何需要对外暴露的名字必须以大写字母开头,不需要对外 ...
- mysql复制那点事(2)-binlog组提交源码分析和实现
mysql复制那点事(2)-binlog组提交源码分析和实现 [TOC] 0. 参考文献 序号 文献 1 MySQL 5.7 MTS源码分析 2 MySQL 组提交 3 MySQL Redo/Binl ...
- Web 字体 font-family 再探秘
之前写过一篇关于Web字体简介及使用技巧的文章: 你该知道的字体 font-family. 该篇文章基本没有太多移动端的字体选择及分析.并且过了这么久,如今的 Web 字体又有了一些新的东西,遂有此文 ...
- MacOS VSCode 安装 GO 插件失败问题解决
0x00 问题重现 Installing golang.org/x/tools/cmd/guru FAILED Installing golang.org/x/tools/cmd/gorename F ...