#include <bits/stdc++.h>
#define ll long long
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
const int N = 5e4+; vector<int> ve[N];
int b[N], c[N], tot;
int l[N], r[N], ans[N];
void dfs(int fa, int x){
tot++;
l[x] = tot;
for(int i = ; i < ve[x].size(); i++){
int y = ve[x][i];
if(y == fa) continue ;
dfs(x, y);
}
r[x] = tot;
} struct Node{
int b, pos;
Node(){}
Node(int b, int pos): b(b), pos(pos){}
bool operator <(const Node& a) const{
return b < a.b;
}
};
Node T[N<<]; void pushup(int rt){
T[rt] = max(T[rt<<], T[rt<<|]);
}
void build(int l, int r, int rt){
T[rt].b = T[rt].pos = -;
if(l == r) return ;
int m = l+r >> ;
build(lson);
build(rson);
}
void update(int pos, Node x, int l, int r, int rt){
if(pos == l&&r == pos){
T[rt] = x;
return ;
}
int m = l+r >> ;
if(pos <= m) update(pos, x, lson);
else update(pos, x, rson);
pushup(rt);
}
Node query(int L, int R, int l, int r, int rt){
if(L <= l&&r <= R)
return T[rt];
int m = l+r >> ;
Node ret = Node(-, -);
if(L <= m) ret = max(ret, query(L, R, lson));
if(R > m) ret = max(ret, query(L, R, rson));
return ret;
} struct p{
int b, c, id, kth;
p(){}
p(int b, int c, int id, int kth):b(b), c(c), id(id), kth(kth){}
bool operator < (const p& a) const{
return c != a.c? c > a.c: kth < a.kth;
}
};
p pp[N]; int main(){
int t; scanf("%d", &t);
while(t--){
int n, m, x;
scanf("%d%d", &n, &m);
for(int i = ; i < n; i++) ve[i].clear();
b[] = c[] = -;
for(int i = ; i < n; i++){
scanf("%d%d%d", &x, b+i, c+i);
ve[x].push_back(i);
} tot = ;
dfs(-, );
for(int i = ; i < n; i++)
pp[i] = p(b[i], c[i], i, l[i]);
sort(pp+, pp+n);
build(, n, );
for(int i = ; i < n; i++){
int j = pp[i].id;
int L = l[j]+, R = r[j];
if(L > R) ans[j] = -;
else
ans[j] = query(L, R, , tot, ).pos;
update(l[j], Node(pp[i].b, pp[i].id), , tot, );
}
while(m--){
scanf("%d", &x);
printf("%d\n", ans[x]);
}
}
return ;
}

【hdu4366】dfs序线段树的更多相关文章

  1. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

  2. 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心

    3252: 攻略 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 130[Submit][Status][Discuss] D ...

  3. Codeforces 343D Water Tree(DFS序 + 线段树)

    题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...

  4. BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)

    题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...

  5. POJ 3321 DFS序+线段树

    单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4:   5: #include < ...

  6. 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树

    题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...

  7. F - Change FZU - 2277 (DFS序+线段树)

    题目链接: F - Change FZU - 2277 题目大意: 题意: 给定一棵根为1, n个结点的树. 有q个操作,有两种不同的操作 (1) 1 v k x : a[v] += x, a[v ' ...

  8. BZOJ4551[Tjoi2016&Heoi2016]树——dfs序+线段树/树链剖分+线段树

    题目描述 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均 ...

  9. BZOJ1103 [POI2007]大都市meg dfs序 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1103 题意概括 一棵树上,一开始所有的边权值为1,我们要支持两种操作: 1. 修改某一条边的权值为 ...

  10. Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

随机推荐

  1. Amoeba For MySQL读写分离

    Amoeba安装,参考http://hi.baidu.com/itfenqing/item/79fe989838e1e8ad83d2959c Amoeba版本为:amoeba-mysql-1.3.1- ...

  2. oslo.messaging 1.8.0 bug fix and blueprint

    1366597 由于amqp_auto_delete可配置,但是NotifierPublisher使用的是没有在配置中获取而使用的默认的False,即非auo_delete,因而在用户配置了amqp_ ...

  3. 如何真正抓住微信小程序的红利? 阿禅知乎live总结

    微信App定义 为满足用户某种开发需求.完全基于微信的消息或网页应用,入口是公众号,用户无需离开微信即可完成所有操作,所有需求都在公众号里被满足 微信App的优势 1. 顾客在哪里,就让顾客在哪里看到 ...

  4. git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree fetch origin

    git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree fetch origi ...

  5. windows 文件权限导致的 git 问题

    windows 文件权限导致的 git 问题 在 windows 上使用 git 时,会遇到明明什么都没有改动,但是 git status 显示一堆文件被修改.这时,通过 git diff 可看到什么 ...

  6. 使用curl命令操作elasticsearch

    使用curl命令操作elasticsearch 大岩不灿 发表于 2015年4月25日 浏览 7,426 次 第一:_cat系列_cat系列提供了一系列查询elasticsearch集群状态的接口.你 ...

  7. [翻译] 为什么Uber的数据库从Postgres 切换到 MySql

    Uber工程师团队发布了一个重要的博客文章:他们的数据库从Postgres从移动到MySQL.毫不夸张地说,阅读这篇文章是一种享受,特别是因为他们提到这些细节:磁盘格式和那对他们2个数据库的表现的影响 ...

  8. LaTeX插入图表方法 Lists of tables and figures

    Lists of tables and figures A list of the tables and figures keep the information organized and prov ...

  9. paper 97:异质人脸识别进展的资讯

    高新波教授团队异质人脸图像识别研究取得新突破,有望大大降低刑侦过程人力耗费并提高办案效率         近日,西安电子科技大学高新波教授带领的研究团队,在异质人脸图像识别研究领域取得重要进展,其对香 ...

  10. WIN7远程桌面连接方法!

    WIN7远程桌面连接方法!