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. 【转】使用命令行方式创建和启动android模拟器

    原文网址:http://blog.csdn.net/tiandinilv/article/details/8953001 1.Android模拟器介绍 Android中提供了一个模拟器来模拟ARM核的 ...

  2. Google Map API 代码示例

  3. HDU --- 4006

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  4. OpenFlow能解决私有云网络VLAN问题么

    本文转载自:http://network.51cto.com/art/201202/314310.htm 在关于私有云网络的文章中,我们首先探讨了物理网络是否影响私有云.本文我们将讨论如何通过软件定义 ...

  5. poj 2479 Maximum sum (最大字段和的变形)

    题目链接:http://poj.org/problem?id=2479 #include<cstdio> #include<cstring> #include<iostr ...

  6. php优化技巧

    PHP优化的目的是花最少的代价换来最快的运行速度与最容易维护的代码.本文给大家提供全面的优化技巧. 1.echo比print快. 2.使用echo的多重参数代替字符串连接. 3.在执行for循环之前确 ...

  7. CentOS6.5解决中文乱码与设置字符集

    [ CleverCode发表在csdn博客中的原创作品,请勿转载,原创地址:http://blog.csdn.net/clevercode/article/details/46377577] 1)说明 ...

  8. Linux命令之dot - 绘制DOT语言脚本描述的图形

    本文链接:http://codingstandards.iteye.com/blog/840055 用途说明 Graphviz (Graph Visualization Software的缩写)是一个 ...

  9. spring beans源码解读之--BeanFactory进化史

    BeanFactory是访问bean容器的根接口,它是一个bean容器的基本客户端视图. 先让我们看看beanfactory的前生后世吧! beanFactory有四个重要的子接口: SimpleJn ...

  10. iOS-系统定位功能

    ios系统定位 前期准备 系统定位功能,需要用到框架:CoreLocation/CoreLocation.h, 然后导入文件#import <CoreLocation/CoreLocation. ...