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 ...
随机推荐
- SpringMvc新建实例配置
一.创建项目: 1.建立新的动态web项目: 2.为项目命名为:SpringMVC_01 3.添加tomcat运行时环境\依赖库 如果是MyEclipse的话创建web项目时就不需要此步骤 右键项目 ...
- 有趣的Flex布局
对于刚接触前端的小白,在还原页面样式的时候,往往会遇到页面布局(layout)上的问题,用着生硬的padding来固定盒子的位置,不仅代码看的沉重,还得适应各种浏览器页面,始终没有办法做到统一.接下来 ...
- 02、Java的lambda表达式和JavaScript的箭头函数
前言 在JDK8和ES6的语言发展中,在Java的lambda表达式和JavaScript的箭头函数这两者有着千丝万缕的联系:本次试图通过这篇文章弄懂上面的两个"语法糖". 简介 ...
- 【原创】TextCNN原理详解(一)
最近一直在研究textCNN算法,准备写一个系列,每周更新一篇,大致包括以下内容: TextCNN基本原理和优劣势 TextCNN代码详解(附Github链接) TextCNN模型实践迭代经验总结 ...
- 【Java例题】4.5异常处理
5. 对于输入的数,如果出现小数,则作为异常处理,并舍去小数,显示结果:如果输入的数据类型不对也作为异常处理,显示结果0. package chapter4; import java.util.*; ...
- loadrunner中的ie浏览器无法使用
我的loadrunner是12.55版本的,windows10系统 在我们学习loadrunner的过程中,会出现下面一个问题: 在录制脚本时,loadrunner中的ie浏览器无法使用处于飘红状态. ...
- Simple Windows Service in C++
本文是来自CodeProject中的一篇名为Simple Windows Service in C++的译文,原文地址为:https://www.codeproject.com/Articles/49 ...
- go 学习笔记之值得特别关注的基础语法有哪些
在上篇文章中,我们动手亲自编写了第一个 Go 语言版本的 Hello World,并且认识了 Go 语言中有意思的变量和不安分的常量. 相信通过上篇文章的斐波那契数列,你已经初步掌握了 Go 语言的变 ...
- PL/SQL 调用 JAVA代码
1.直接在 SQL Developer中写入代码 create or replace and compile java source named "HelloWorld" as p ...
- 解决pyinstaller打包可执行文件,存放路径包含中文无法运行的问题
一.实验环境 1.Windows7x64_SP1 2.anaconda2.5.0 + python2.7(anaconda集成,不需单独安装) 3.pyinstaller3.0 二.问题描述 1.使用 ...