dfs序线段树
dfs序+线段树,啥?如果在一棵树上,需要你修改一些节点和查询一些节点,如果直接dfs搜的话肯定超时,那用线段树?树结构不是区间啊,怎么用?用dfs序将树结构转化为一个区间,就能用线段树进行维护了。
dfs序是指:每个节点在dfs深度优先遍历中的进出栈的时间序列,记录每个点进栈和出栈的时间点,会发现每个点在栈中出现两次
比如下面这个图的dfs序:
(转载来的图片,太懒不想画)
那么这样转化后我们就可以在上面进行线段树了。对于进栈时间点,我们记录为left[ u ],出栈时间点为right[ u ] 。对于这个区间,我们将每个节点标记为 1~len(dfs序长度)
以这个为区间1~len建线段树,然后那棵树就没用了!,没用了!对于修改一个节点x,就是修改left[x](但你的len是等于n的,或者你如果建的是两个节点都算的,你需要update左右编号),对于查询一个区间,就是查询left[x]~right[x],实际上就是线段树。
但是我在刚理解时候总会受原来那颗树的影响,实际上,可以这样理解,比如在这个dfs序中你要修改树节点1(原本的值都是1)
------------------------------>
附上一个例题
There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.
The tree has N forks which are connected by branches. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won't grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.
The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?
Input
The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N - 1 lines each contain two integers u and v, which means fork u and fork v are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
"C x" which means the existence of the apple on fork x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
"Q x" which means an inquiry for the number of apples in the sub-tree above the fork x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning
Output
Sample Input
3
1 2
1 3
3
Q 1
C 2
Q 1
Sample Output
3
2
var
left,right,next,head,a,value:array[..]of longint;
n,m,k,x,y,e,tot,s:longint;
i:longint;
ch:char;
procedure add(x,y:int64);
begin
inc(e);a[e]:=y;next[e]:=head[x];head[x]:=e;
end;
procedure dfs(u,m:longint);
var i,v:longint;
begin
inc(tot);
left[u]:=tot;
i:=head[u];
while i> do
begin
v:=a[i];
if v<>m then begin dfs(v,u); end;
i:=next[i];
end;
// inc(tot);
right[u]:=tot; end;
procedure build(u,l,r:longint);
var mid:longint;
begin
if l=r then
begin
value[u]:=;
exit;
end;
mid:=(l+r)>>;
build(u*,l,mid);
build(u*+,mid+,r);
value[u]:=value[u*]+value[u*+];
end;
procedure upate(u,id,l,r:longint);//id 为修改点位置
var mid:longint;
begin
if (l>id)or(r<id) then exit;
if (l=id)and(r=id) then
begin
if value[u]= then value[u]:= else value[u]:=;
exit;
end;
mid:=(l+r)>>;
upate(u*,id,l,mid);
upate(u*+,id,mid+,r);
value[u]:=value[u*]+value[u*+];
end;
function que(u,l,r,ql,qr:int64):int64;
var mid:longint;
begin
if (ql>r)or(qr<l) then exit();
if (ql<=l)and(r<=qr) then exit(value[u]);
mid:=(l+r)>>;
exit(que(u*,l,mid,ql,qr)+que(u*+,mid+,r,ql,qr));
end;
begin
readln(n);
for i:= to n- do
begin
readln(x,y);
add(x,y);
add(y,x);
end;
dfs(,);
build(,,tot);
readln(k);
for i:= to k do
begin
readln(ch,s);
if ch='Q' then writeln(que(,,tot,left[s],right[s]))else
begin
upate(,left[s],,tot);
// upate(,right[s],,tot);
end;
end;
end.
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 ...
随机推荐
- DOMNodeInserted,DOMNodeRemoved 和监听内容变化插件
元素的增加 删除 及事件监听 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...
- Java学习笔记——集合
类集简介 从JDK1.2开始Java引入了类集开发框架,所谓的类集指的就是一套动态对象数组的实现方案,在实际开发之中没有有何一项开发可以离开数组,但是传统的数组实现起来非常的繁琐.而且长度是其致命伤, ...
- u-boot分析(七)----内存初始化
u-boot分析(七) 上篇博文我们按照210的启动流程,分析到了时钟初始化,今天我们继续按照u-boot的启动流程对内存的初始化进行分析. 今天我们会用到的文档: 1. 2440芯片手 ...
- SpringMVC ------JstlView
摘要: Spring为展现层提供的基于MVC设计理念的优秀的Web框架,是目前最主流的MVC框架之一 .Spring3.0后全面超越Struts,成为最优秀的MVC框架 .SpringMVC通过一套M ...
- DIV命名规范
DIV命名规范 企业DIV使用频率高的命名方法 网页内容类 --- 注释的写法: /* Footer */ 内容区/* End Footer */ 摘要: summary 箭头: arrow 商标: ...
- window下编译ffmpeg
网上关于编译ffmpeg的帖子很多,我也尝试了很多次,但是很多都过不了,一部分原因是版本问题,还有就是有的路劲没说的太明白导致的,经过一天的摸索,最终编译好了,下面把编译方式写下来,希望对看到帖子的人 ...
- Maven一些零散的知识点
Maven常用命令: 1. 创建Maven的普通java项目: mvn archetype:create -DgroupId=com.yida.framework -DartifactId=hello ...
- IOS NSURLConnection(大文件下载)
NSURL:请求地址 NSURLRequest:一个NSURLRequest对象就代表一个请求,它包含的信息有 一个NSURL对象 请求方法.请求头.请求体 请求超时 … … NSMutableURL ...
- IOS 打开照相机 打开相册
/** * 打开照相机 */ - (void)openCamera { if (![UIImagePickerController isSourceTypeAvailable:UIImagePicke ...
- 后缀数组入门(二)——Height数组与LCP
前言 看这篇博客前,先去了解一下后缀数组的基本操作吧:后缀数组入门(一)--后缀排序. 这篇博客的内容,主要建立于后缀排序的基础之上,进一步研究一个\(Height\)数组以及如何求\(LCP\). ...