#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. IIS不能对网站添加默认文档(由于权限不足而无法写入配置文件)

    IIS7以上版本配置网站时需要手动配置网站目录的文件夹权限 增加"IIS_IUSER"用户的修改权限 但增加后仍然提示“ 由于权限不足无法写入配置文件” 通常是Web.config ...

  2. ExtJs 使用点滴 十四 通过设置CheckboxSelectionModel属性值来实现GridPanel复选框可用不可用

    var sm = new Ext.grid.CheckboxSelectionModel({singleSelect : false,renderer:function(v, p, record)   ...

  3. LeetCode Graph Valid Tree

    原题链接在这里:https://leetcode.com/problems/graph-valid-tree/ 题目: Given n nodes labeled from 0 to n - 1 an ...

  4. 网页内嵌入QQ通信组件,唤起QQ,针对不同平台的处理方式

    web浏览器中嵌入QQ通信组件,目前发现有两种方式,主要是区分 IOS平台(苹果系列)和其他平台(PC 安卓等……),下面是代码区别: <li><a href="http: ...

  5. Python之路-python(set集合、文本操作、字符编码 )

    一.集合操作(set)                                                                                          ...

  6. R数据实战vehicles--1

    新建项目vehicles-project 数据文件vehicles.csv与varlabels.txt放在项目文件中

  7. TCP状态变迁图

    服务端,端口的状态变化 先在本机(IP地址为:192.168.1.10)配置FTP服务,然后在其它计算机(IP地址为:192.168.1.1)访问FTP服务,从TCPView看看端口的状态变化. 下面 ...

  8. Quartz.NET

    http://www.360doc.com/userhome.aspx?userid=11741424&cid=2#

  9. JDBC-java访问数据库

    定义:JDBC是一个独立于数据库管理系统的,通用的SQL数据库存取和操作的公共接口 (由JAVA类和接口组成,接口由各数据库厂家负责实现) 使用方法: 1.加载驱动 Class.forName(&qu ...

  10. ubuntu15.04 安装搜狗输入法

    首先:打开 系统设置->软件和更新,添加以下源, deb http://archive.ubuntukylin.com:10006/ubuntukylin trusty main 然后 sudo ...