【hdu4366】dfs序线段树
#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序线段树的更多相关文章
- Educational Codeforces Round 6 E dfs序+线段树
题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...
- 【BZOJ-3252】攻略 DFS序 + 线段树 + 贪心
3252: 攻略 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 339 Solved: 130[Submit][Status][Discuss] D ...
- Codeforces 343D Water Tree(DFS序 + 线段树)
题目大概说给一棵树,进行以下3个操作:把某结点为根的子树中各个结点值设为1.把某结点以及其各个祖先值设为0.询问某结点的值. 对于第一个操作就是经典的DFS序+线段树了.而对于第二个操作,考虑再维护一 ...
- BZOJ2434 [Noi2011]阿狸的打字机(AC自动机 + fail树 + DFS序 + 线段树)
题目这么说的: 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母.经阿狸研究发现,这个打字机是这样工作的: 输入小 ...
- POJ 3321 DFS序+线段树
单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4: 5: #include < ...
- 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树
题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...
- F - Change FZU - 2277 (DFS序+线段树)
题目链接: F - Change FZU - 2277 题目大意: 题意: 给定一棵根为1, n个结点的树. 有q个操作,有两种不同的操作 (1) 1 v k x : a[v] += x, a[v ' ...
- BZOJ4551[Tjoi2016&Heoi2016]树——dfs序+线段树/树链剖分+线段树
题目描述 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为1),有以下 两种操作:1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均 ...
- BZOJ1103 [POI2007]大都市meg dfs序 线段树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1103 题意概括 一棵树上,一开始所有的边权值为1,我们要支持两种操作: 1. 修改某一条边的权值为 ...
- 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 ...
随机推荐
- 一个php的爬虫,将笔趣阁的书可以都下载下来。
数据库:book 表id ---- 数据库: `book`-- -- -------------------------------------------------------- ---- 表的结 ...
- Sublime Text 3 3126 注册码
转载自:https://fatesinger.com/78252 Sublime Text 3 3126 注册码 第一个测试通过 -– BEGIN LICENSE -– Michael Barnes ...
- iOS:高德地图的使用
本人花了点时间集成了高德地图的几乎所有的功能,包含:地图的显示.地图的绘制.地图的定位.地图的POI数据检索.地图的线路规划.地图导航等下载地址如下:https://github.com/xiayua ...
- mvc ajax dropdownlist onchang事件响应
<script type="text/javascript"> $("#Cycle").on("change", functio ...
- matlab实现分水岭算法处理图像分割
此程序为优化后的分水岭算法,避免了图像过分割 I= imread('D:\Images\pic_loc\1870405130305041503.jpg'); imshow(I); h=fspecial ...
- 英文不好也能快速"记忆" API
英文不好不要紧,把API函数导入打字练习类软件,即是练习打字速度,提高编程效率:也能短时间记忆API. 坚持每天打一遍,约2小时,连续打两周,会对API有很好的记忆,此方法是结合英文学习方法!以下是W ...
- 彻底的放弃.net
最近看了招聘信息,搜一搜 .net 高级信息,发现对比 JAVA ,只有1页,而 JAVA 至少有3页. 最近用了一下嗒嗒巴士,滴滴打车,各种团购,各种外卖,各种移动互联网改变生活的东西,无一和 .n ...
- jqueryui引用出错(base is not a constructor,widget no found)
出错的原因,主要是引用顺序出错. 正确顺序如下: query本身必须放在第一位: <script src="../../../Scripts/Jquery1.7.2/jquery-1. ...
- 使用Redis分布式队列
1.这是处理异常的类 public class MyExceptionAttribute:HandleErrorAttribute { //public static Queue<Excepti ...
- REST实战:SeverClient项目+RESTful理论
理解一个新的技术,无疑就是使用它了,下面我们就通过一个可执行的demo来展现REST的原理和使用. 一 Demo 1.1 服务器端 1 主程序MainServer.java负责启动一个REST服务组件 ...