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
 
Author
FZU
【分析】
简单的DFS序题,DFS一下变成序列问题,预处理后块内二分就可以做了。
交上去一直RE,跟别人对拍了好像没错,不知道怎么回事......
 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map> const int N = + ;
const int SIZE = ;//块状链表的大小
const int M = + ;
using namespace std;
typedef long long ll;
struct DATA {
int a, b;
}data[N], list[N], Sort[N];
bool operator < (DATA a,DATA b) {
return a.b < b.b;
}
map<int,int> Map;
vector<int>G[N];
int pos[N], tot, Max[N];
int size[N], n, q; //二分搜索
int search(int l, int r, int val){
if (Sort[r].b <= val) return -;
if (Sort[l].b > val) return Max[l];
while (l + < r) {
int mid = (l + r) >> ;
if (Sort[mid].b > val) r = mid;
else l = mid;
}
return Max[r];
}
int dfs(int u){
pos[u] = tot;
list[tot] = Sort[tot] = data[u];
tot++;//tot为时间序
int cnt = ;
for (int i = ; i < G[u].size(); i++){
int v = G[u][i];
cnt += dfs(v);
}
return size[pos[u]] = cnt;
} void prepare(){
memset(data, -, sizeof(data));
memset(Sort, -, sizeof(Sort));
memset(list, -, sizeof(list));
memset(Max, , sizeof(Max));
memset(size, , sizeof(size));
memset(pos, , sizeof(pos));
scanf("%d%d", &n, &q);
for (int i = ; i < n; i++) G[i].clear();//初始化邻接表
Map.clear();
Map[-] = -;
}
void init(){
for (int i = ; i < n; i++){
int fa, x, y;
scanf("%d%d%d", &fa, &x, &y);
G[fa].push_back(i);
data[i].a = x;
data[i].b = y;
Map[x] = i;
}
tot = ;
dfs();//构图
}
void dp(){
//预处理出每一个块内的值,好二分
for (int i = ; i < n; i += SIZE){
int j = i + SIZE;
if (j > n) break;
sort(Sort + i, Sort + j);
Max[j - ] = Sort[j - ].a;
//块内地推
for (int k = j - ; k >= i;k--) Max[k] = max(Max[k + ], Sort[k].a);
}
}
void query(int l, int r, int val){
int ans = -;
for (int i = l; i <= r;){
if (i % SIZE == && i + SIZE - <= r){
int tmp = search(i, i + SIZE - , val);
ans = max(ans, tmp);
i += SIZE;
}else{//暴力
if (list[i].b > val && list[i].a > ans) ans = list[i].a;
i++;
}
}
//printf("%d\n", ans);
printf("%d\n", Map[ans]);
}
void work(){
for (int i = ; i <= q; i++){
int x, val;
scanf("%d", &x);
val = data[x].b;
x = pos[x];
int y = x + size[x] - ;//size用来存子树的大小
//printf("*%d*\n", y);
query(x, y, val);
}
} int main(){
int T;
#ifdef LOCAL
freopen("data.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
scanf("%d", &T);
while (T--){
prepare();//初始化
init();
dp();
work();
}
return ;
}

【HDU4366】【DFS序+分块】Successor的更多相关文章

  1. HDU4366 Successor【dfs序 分块】

    HDU4366 Successor 题意: 给出一棵根为\(1\)的树,每个点有两个权值\(x,y\),每次询问一个点的子树中\(x\)比这个点的\(x\)大且\(y\)值最大的那个点 题解: 如果以 ...

  2. HDU 4366 Successor(dfs序 + 分块)题解

    题意:每个人都有一个上司,每个人都有能力值和忠诚值,0是老板,现在给出m个询问,每次询问给出一个x,要求你找到x的所有直系和非直系下属中能力比他高的最忠诚的人是谁 思路:因为树上查询很麻烦,所以我们直 ...

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

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

  4. 2016 ACM/ICPC Asia Regional Dalian Online 1010 Weak Pair dfs序+分块

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...

  5. BZOJ4867 Ynoi2017舌尖上的由乃(dfs序+分块)

    容易想到用dfs序转化为序列上的问题.考虑分块,对每块排序,修改时对于整块打上标记,边界暴力重构排序数组,询问时二分答案,这样k=sqrt(nlogn)时取最优复杂度nsqrt(nlogn)logn, ...

  6. BZOJ 4765 普通计算姬 dfs序+分块+树状数组+好题!!!

    真是道好题...感到灵魂的升华... 按dfs序建树状数组,拿前缀和去求解散块: 按点的标号分块,分成一个个区间,记录区间子树和 的 总和... 具体地,需要记录每个点u修改后,对每一个块i的贡献,记 ...

  7. bzoj 4765 普通计算姬 dfs序 + 分块

    题目链接 Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些.普通计算机能计算数列区间和,而普通计算姬能 ...

  8. 2018.06.30 BZOJ4765: 普通计算姬(dfs序+分块+树状数组)

    4765: 普通计算姬 Time Limit: 30 Sec Memory Limit: 256 MB Description "奋战三星期,造台计算机".小G响应号召,花了三小时 ...

  9. CF 375D. Tree and Queries加强版!!!【dfs序分块 大小分类讨论】

    传送门 题意: 一棵树,询问一个子树内出现次数$\ge k$的颜色有几种,Candy?这个沙茶自带强制在线 吐槽: 本来一道可以离散的莫队我非要强制在线用分块做:上午就开始写了然后发现思路错了...: ...

随机推荐

  1. HDOJ(HDU) 2520 我是菜鸟,我怕谁(等差数列)

    Problem Description lin2144是一只小菜鸟,都是笨鸟先飞,lin2144想来个菜鸟先飞,他从0点出发 一开始的飞行速度为1m/s,每过一个单位时间lin2144的飞行速度比上一 ...

  2. JVM分代垃圾回收策略的基础概念

    由于不同对象的生命周期不一样,因此在JVM的垃圾回收策略中有分代这一策略.本文介绍了分代策略的目标,如何分代,以及垃圾回收的触发因素. 文章总结了JVM垃圾回收策略为什么要分代,如何分代,以及垃圾回收 ...

  3. 应用360云盘与SVN版本管理服务器搭建基于云端的版本控制软件

    步骤一:(安装软件) 1.TortoiseSVN 2.VisualSVN-Server-2.71 3.安装云盘客户端360wangpan_setup 步骤二:(VisualSVN Server设置) ...

  4. 如何仿写thinkphp的C方法?

    config.php代码如下: <?php return array( 'db_user'=>'root', 'db_pass'=>'root', 'db_name'=>'te ...

  5. C# WinForm 透明控件 PictureBox透明 分类: WinForm 2014-07-30 13:27 591人阅读 评论(0) 收藏

    1.要实现C# WinForm中的控件与背景的透明,可以通过设置控件的BackColor属性为Transparent,同时设置其父控件.因为在C#中,控件的透明指对父窗体透明.如果不设置Parent属 ...

  6. EnterpriseArchitectect 软件的勾选的几个选项对应的中文意思

    Business Process 业务流程 Requirements 需求分析 Use Case 用例 Domain Model 领域模型 Class 类 Database 数据库设计 Compone ...

  7. C#截取指定字符串函数

    本文转载:http://www.cnblogs.com/liufei88866/archive/2012/05/12/2497395.html 一.通过函数方式进行获取. public string ...

  8. lenky的个人站点 ----LINUX 内核进程

    http://www.lenky.info/archives/category/nix%E6%8A%80%E6%9C%AF/%E5%86%85%E6%A0%B8%E6%8A%80%E6%9C%AF

  9. MapReduce实战:统计不同工作年限的薪资水平

    1.薪资数据集 我们要写一个薪资统计程序,统计数据来自于互联网招聘hadoop岗位的招聘网站,这些数据是按照记录方式存储的,因此非常适合使用 MapReduce 程序来统计. 2.数据格式 我们使用的 ...

  10. hadoop2.2 伪分布式环境

    在安装JDK之前,请确认系统是32还是64,根据系统版本,选择JDK版本.Hadoop版本 下面是以在CentOS-6.5-x86_64系统上安装为例 安装前准备 在"/usr"下 ...