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. 【转】VMware Workstation 11 永久激活码key 非注册机

    原文网址:http://www.landiannews.com/archives/12565.html 昨天我们发布了<跨越式提升:VMware Workstation 11 发布 附下载地址& ...

  2. (经常看看)jdk 设计模式

    在JDK(Java Development Kit)类库中,开发人员使用了大量设计模式,正因为如此,我们可以在不修改JDK源码的前提下开发出自己的应用软件,本文列出了部分JDK中的模式应用实例,有兴趣 ...

  3. 通过UIImagePickerController完成照相和相片的选取

    UIImagePickerController是用于选取现有照片,或者用照相机现场照一张相片使用的 定义: @interface ShowViewController : UIViewControll ...

  4. Cogs 1070. [焦作一中2012] 玻璃球游戏 带权并查集,逆序处理

    题目: http://cojs.tk/cogs/problem/problem.php?pid=1070 1070. [焦作一中2012] 玻璃球游戏 ★   输入文件:marbles.in   输出 ...

  5. linux 内核驱动加载过程中 向文件系统中的文件进行读写操作

    utils.h 文件: #ifndef __UTILS_H__ #define __UTILS_H__ void a2f(const char *s, ...); #endif utils.c 文件: ...

  6. 公司框架将入参Map化

    1.Map<String,Object> map = BeanUtil.describe(inDto);

  7. 浅谈qmake之pro、pri、prf、prl文件

    浅谈qmake之pro.pri.prf.prl文件 转载自:http://blog.csdn.net/dbzhang800/article/details/6348432 尽管每次和cmake对比起来 ...

  8. iOS开发总结-类似京东分类,UICollectionView

    // // TypeViewController.m // BJ // // Created by shirenfeng on 16/11/6. // Copyright © 2016年 com.ws ...

  9. php 判断数组相等 数组运算符介绍

    如何判断两个数组相等呢?其实很简单,用 == 或者 === 就可以了 php手册里说明如下, 例子 名称 结果 $a + $b 联合 $a 和 $b 的联合. $a == $b 相等 如果 $a 和  ...

  10. 【android】ImageView的src和background的区别以及两者的妙用

    一.ImageView中XML属性src和background的区别: background会根据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小,不会进行拉伸 .src是图片内 ...