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
1 var 2 left,right,next,head,a,value:array[0..800005]of longint; 3 n,m,k,x,y,e,tot,s:longint; 4 i:longint; 5 ch:char; 6 procedure add(x,y:int64); 7 begin 8 inc(e);a[e]:=y;next[e]:=head[x];head[x]:=e; 9 end; 10 procedure dfs(u,m:longint); 11 var i,v:longint; 12 begin 13 inc(tot); 14 left[u]:=tot; 15 i:=head[u]; 16 while i>0 do 17 begin 18 v:=a[i]; 19 if v<>m then begin dfs(v,u); end; 20 i:=next[i]; 21 end; 22 // inc(tot); 23 right[u]:=tot; 24 25 end; 26 procedure build(u,l,r:longint); 27 var mid:longint; 28 begin 29 if l=r then 30 begin 31 value[u]:=1; 32 exit; 33 end; 34 mid:=(l+r)>>1; 35 build(u*2,l,mid); 36 build(u*2+1,mid+1,r); 37 value[u]:=value[u*2]+value[u*2+1]; 38 end; 39 procedure upate(u,id,l,r:longint);//id 為修改點位置 40 var mid:longint; 41 begin 42 if (l>id)or(r<id) then exit; 43 if (l=id)and(r=id) then 44 begin 45 if value[u]=1 then value[u]:=0 else value[u]:=1; 46 exit; 47 end; 48 mid:=(l+r)>>1; 49 upate(u*2,id,l,mid); 50 upate(u*2+1,id,mid+1,r); 51 value[u]:=value[u*2]+value[u*2+1]; 52 end; 53 function que(u,l,r,ql,qr:int64):int64; 54 var mid:longint; 55 begin 56 if (ql>r)or(qr<l) then exit(0); 57 if (ql<=l)and(r<=qr) then exit(value[u]); 58 mid:=(l+r)>>1; 59 exit(que(u*2,l,mid,ql,qr)+que(u*2+1,mid+1,r,ql,qr)); 60 end; 61 begin 62 readln(n); 63 for i:=1 to n-1 do 64 begin 65 readln(x,y); 66 add(x,y); 67 add(y,x); 68 end; 69 dfs(1,1); 70 build(1,1,tot); 71 readln(k); 72 for i:=1 to k do 73 begin 74 readln(ch,s); 75 if ch='Q' then writeln(que(1,1,tot,left[s],right[s]))else 76 begin 77 upate(1,left[s],1,tot); 78 // upate(1,right[s],1,tot); 79 end; 80 end; 81 end.