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
3 2
0 100 99
1 101 100
1
2
Sample Output
2
-1
-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 ;
}