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. 从 3 个 IT 公司里学到的 57 条经验

    自1999年起我就开始发掘一些科技公司,并帮助它们运营.下面是从干这行中得到的57条经验.我可以列出更多,但恐怕会令你厌烦. 1.做你个人有热情的事情.你是你自己最好的民意代表. 2.用户体验很重要. ...

  2. 怎样打开64位 Ubuntu 的32位支持功能?

    怎样打开64位 Ubuntu 的32位支持功能? 现在有一个让你可以在64位系统中使用32位软件的方法,就在你读了这篇文章然后照着做了之后就可以了.如果你有一个13.10或更高版本的Ubuntu/De ...

  3. 邮件发送小demo

    //send email public static bool SendEmail() { //实例化发件人地址 MailAddress from = new MailAddress("aa ...

  4. 将cocos的app直接在我的设备上测试运行

    首先,你要有一个写好了的,准备在真机上测试的cocos程序. 1.设置ARC,设置的过程在另外一篇博文上有写. 2.在Target的Build Setting里面 找到Valid Archs 删除里面 ...

  5. 1‘b0 什么意思

    在看datasheet 中有类似表达式如下: 3'b000, 1'b1, 1'b0; 3'b000这个表示:b代表二進制.3代表位元數. 1'b1:宣告為一位元二進制之值為1,一般除了可以宣告b外,也 ...

  6. hdu 4738 Caocao's Bridges (tarjan求桥)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4738 题目大意:给一些点,用一些边把这些点相连,每一条边上有一个权值.现在要你破坏任意一个边(要付出相 ...

  7. 我眼中的 Oracle 性能优化

    恒生技术之眼 作者 林景忠 大家对于一个业务系统的运行关心有如下几个方面:功能性.稳定性.效率.安全性.而一个系统的性能有包含了网络性能.应用性能.中间件性能.数据库性能等等. 今天从数据库性能的角度 ...

  8. Java8中Lambda表达式的10个例子

    Java8中Lambda表达式的10个例子 例1 用Lambda表达式实现Runnable接口 //Before Java 8: new Thread(new Runnable() { @Overri ...

  9. ecshop检验邮件是否合法

    <?php /** * 验证输入的邮件地址是否合法 * * @access public * @param string $email 需要验证的邮件地址 * * @return bool */ ...

  10. visual studio 2015 企业版 序列号及官方下载地址

    VisualStudio 2015 正式版已经可以通过官方下载了. Visual Studio 是一套基于组件的软件开发工具和其他技术,可用于构建功能强大.性能出众的应用程序.Visual Studi ...