【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 ...
随机推荐
- 奥迪--S5
--型号:S5 --生产:奥迪进口 --价格:60-80W --发动机:3T 333马力 V6,机械增压 --变速箱:7档双离合 --气缸排列:V --总部:德国,英戈尔施塔特 --类型:中型车 -- ...
- [knowledge][basic][hardware] 内存的硬件结构(转)
写的并不太易懂,但概念性的科普已足够. 原文地址:http://blog.csdn.net/miaomao1989/article/details/51508195 Memory中的Channel/R ...
- sql cross join table
A 表数据如下图所示 B表数据如下图所示
- paramiko模块,线程,进程
关于paramiko模块 paramiko是基于Python实现的ssh2远程安全连接,支持认证及密钥方式远程执行命令.文件传输,中间ssh代理等 paramiko的安装: 安装好之后,用parami ...
- spark的安装
官方安装地址:http://spark.apache.org/docs/latest/spark-standalone.html 准备 1.三台机器(m2.m3.m4) 2.m2位master, m3 ...
- EBS R12.2.0启动时报"httpd.pid: Permission denied"错误
启动应用服务: $ /app/oracle/apps/VIS/fs1/inst/apps/VIS_erptest/admin/scripts/adstrtal.sh apps/apps 报出如下错误: ...
- oracle often commands
还是这个好用 Oracle mysql 对比版本 Personal Oracle7 Release 7.3.4.0.0 mysql 3.23.58-nt 启动画面(点击放大) 默认安装目录 C:O ...
- 20145320《Java程序设计》第4周学习总结
20145320<Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 继承 继承作为面向对象的第二大特征,避免多个类间重复定义共同行为.即当多个类中存在相同属性和行为时 ...
- leetcode_222 Count Complete Tree Nodes
题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...
- Linux for QQ 安装
1.sudo apt-get install wine 2.打开网址http://www.ubuntukylin.com/application/show.php?lang=cn&id=279 ...