这道题做的崩溃。。。从昨天晚上开始想,一直做到今天下午4点多。。。题目的意思啊!!!!我看错了好多次啊啊啊啊啊。。竟然是离线的,题目你这么坑,你父母知道吗。。。还有那个父节点啊,第一个输入是父节点啊啊啊啊啊。。尼玛啊,我直接当成职位编号了。。。。题目是一颗树啊啊啊啊啊,是我英语不好还是出题人英语不好啊啊啊啊。。。。最后还是多亏了hyf大神为渣渣答疑解惑,渣渣从坑里爬出来啊啊啊啊啊啊啊啊。。。崩溃啊。。还有吐槽一下数据弱啊啊啊啊,排序的时候能力相同按原始编号排也能过啊啊啊啊啊。。尼玛这怎么可能啊啊啊,后来我准备在相同的时候按节点的辖域排。。。hyf大神直接说按dfs层排就好了啦。。。跪拜hyf大神。。。尼玛啊,这道题以前写过类似的,是dfs序+树状数组。这道题就是dfs序+线段树。就是一水题啊啊啊啊啊,我还写过类似的啊啊啊啊,还被卡了这么久啊啊啊。。。火死了。。。
#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <climits>
#define maxn 200005
#define eps 1e-6
#define mod 1000000007
#define INF 99999999
#define lowbit(x) (x&(-x))
#define lson o<<1, L, mid
#define rson o<<1 | 1, mid+1, R
typedef long long LL;
using namespace std;
int H[maxn], next[maxn], v[maxn];
struct node
{
int b, c;
int number;
int pre, back, floor;
}tmp[maxn];
int dfs_clock, dfs_floor;
int cnt, n, m;
int segtree[maxn];
int ans, print[maxn];
int ql, qr, p, loynum;
queue<int> q;
void addedge(int x, int y)
{
next[cnt]=H[x], H[x]=cnt, v[cnt]=y, cnt++;
}
void read(void)
{
int i, up;
scanf("%d%d",&n,&m);
cnt=0;
tmp[0].b=-1, tmp[0].c=-1;
tmp[1].b=INF, tmp[1].c=INF;
tmp[1].number=1;
memset(H, -1, sizeof H);
for(i=2;i<=n;i++){
scanf("%d%d%d",&up,&tmp[i].b,&tmp[i].c);
tmp[i].number=i;
up++;
addedge(up, i);
}
}
void dfs(int now)
{
dfs_clock++, dfs_floor++;
tmp[now].pre=dfs_clock;
for(int e=H[now];~e;e=next[e]) dfs(v[e]);
tmp[now].back=dfs_clock;
tmp[now].floor=dfs_floor--;
}
int cmp(node a, node b)
{
if(a.c!=b.c) return a.c>b.c;
else return a.floor<b.floor;
}
int query(int o, int L, int R)
{
if(ql<=L && qr>=R) return segtree[o];
int mid=(R+L)/2, temp;
if(ql<=mid){
temp=query(lson);
if(tmp[temp].b>tmp[ans].b) ans=temp;
}
if(qr>mid){
temp=query(rson);
if(tmp[temp].b>tmp[ans].b) ans=temp;
}
return ans;
}
void updata(int o, int L, int R)
{
if(L==R){
segtree[o]=loynum;
return;
}
int mid=(R+L)>>1;
if(p<=mid) updata(lson);
else updata(rson);
if(tmp[segtree[o<<1]].b>tmp[segtree[o<<1 | 1]].b)
segtree[o]=segtree[o<<1];
else segtree[o]=segtree[o<<1 | 1];
}
void solve(void)
{
int temp;
sort(tmp+1, tmp+n+1, cmp);
memset(segtree, 0, sizeof segtree);
for(int i=1;i<=n;i++){
ql=tmp[i].pre, qr=tmp[i].back;
ans=0;
ans=query(1, 1, n);
if(!ans) print[tmp[i].number-1]=-1;
else print[tmp[i].number-1]=tmp[ans].number-1;
p=tmp[i].pre, loynum=i;
updata(1, 1, n);
}
while(m--){
scanf("%d", &temp);
printf("%d\n",print[temp]);
}
}
int main(void)
{
int _;
while(scanf("%d",&_)!=EOF){
while(_--){
read();
dfs_clock=0;
dfs_floor=0;
dfs(1);
solve();
}
}
return 0;
}