Description to build up a communications system on n isolated planets. This communication system is a tree that connects N points. The edge of the tree is added by a single line. At some point, the load on an edge is the number of simple paths that are passing through it on the currently connected tree. For example, in, there are now altogether 5 sides. where (3,8) the load on this side is 6, because there are six simple paths 2-3-8,2-3-8-7,3-8,3-8-7,4-3-8,4-3-8-7 passing through (3,8). Now, your task is to dynamically answer the question about the load on some side as you add the side. The first line of input contains two integer n,q, representing the number of planets and the number of operations. The planets numbered starting from 1. The next Q line, each line is one of the following two formats: A x y means an edge is attached between x and Y. Ensure that the X and Y are not connected before. Q x y indicates the load on the edge of the query (x, y). Ensure that there is an edge between x and Y. 1≤n,q≤100000output for each query operation, output the load of the side being queried. Sample Input8 6
A 2 3
A 3 4
A 3 8
A 8 7
A 6 5
Q 3 8Sample Output6The following:Just listen to Jiry_2 said the line of tree merge, so found this problem to do. I hear you can do it with LCT.
first build the final tree (or forest) and ask for a DFS sequence. in the beginning, each node is a unicom block, corresponding to a DFS sequence as subscript of the segment tree, its corresponding DFS sequence inserted into the segment treewhen connecting, use and check to maintain the merge, record the size, and merge the two line segment trees. When asked, found two points deep depth of one, find its DFS subtree there are several points in the Unicom block (that is, the section of the link tree corresponding to the link and), set up a link block x points, the subtree has y points in the Unicom block, the answer is (x-y) *y. Code:
1 var2 I,n,m,cnt,cnt2,mm:longint;3 J,k,l:int64;4B,FA:Array[0..200001,1..2] ofInt64;5Q:Array[0..200001,0..2] ofLongint;6R,v,dep,c:Array[0..200001] ofLongint;7XhArray[0..200001,1..2] ofLongint;8T:Array[0..4000001,-2..2] ofLongint;9 Ch:char;Ten procedureSS (X,fa:longint); One varI:longint; A begin -v[x]:=1; I:=C[X]; Inc (CNT); Xh[x,1]:=cnt; dep[x]:=dep[fa]+1; - whileI>0 Do the begin - ifB[i,1]<>fa ThenSS (B[i,1],x); -I:=b[i,2]; - End; +Xh[x,2]:=CNT; - End; + functionHB (X,y:longint): Longint; A begin at ifT[x,1]=t[x,2] Then - beginT[x,0]:=t[x,0]+t[y,0]; Exit (x);End; - if(t[x,-1]=0) and(t[y,-1]>0) Thent[x,-1]:=t[y,-1]Else - if(t[x,-1]>0) and(t[y,-1]>0) Thent[x,-1]:=HB (t[x,-1],t[y,-1]); - if(t[x,-2]=0) and(t[y,-2]>0) Thent[x,-2]:=t[y,-2]Else - if(t[x,-2]>0) and(t[y,-2]>0) Thent[x,-2]:=HB (t[x,-2],t[y,-2]); inT[x,0]:=t[t[x,-1],0]+t[t[x,-2],0]; - exit (x); to End; + functionNewt (L,r:longint): Longint; - begin theInc (CNT2); T[cnt2,1]:=l; T[cnt2,2]:=R; exit (Cnt2); * End; $ procedurecl (x,y:longint);Panax Notoginseng varLl,rr:longint; - begin theLl:=t[x,1]; Rr:=t[x,2]; Inc (T[X,0]); + ifLl=rr Thenexit; A ify<= (LL+RR)Div 2 Then the begin + ift[x,-1]=0 Thent[x,-1]:=NEWT (ll, (LL+RR)Div 2); -CL (t[x,-1],y); $ End Else $ begin - ift[x,-2]=0 Thent[x,-2]:=NEWT ((LL+RR)Div 2+1, RR); -CL (t[x,-2],y); the End; - End;Wuyi functionQQ (X,l,r:longint): Longint; the varLl,rr:longint; - begin Wu ifx=0 ThenExit0); -Ll:=t[x,1]; Rr:=t[x,2]; About if(ll=l) and(RR=R) ThenExit (T[x,0]); $ ifr<= (LL+RR)Div 2 ThenExit (QQ (t[x,-1],l,r)); - ifL> (LL+RR)Div 2 ThenExit (QQ (t[x,-2],l,r)); -Exit (QQ (t[x,-1],l, (LL+RR)Div 2) +qq (t[x,-2], (LL+RR)Div 2+1, R)); - End; A functionGet (X:longint): Longint; + begin the ifFa[x,1]<>x ThenFa[x,1]:=get (Fa[x,1]); -Exit (Fa[x,1]); $ End; the begin the readln (n,m); the fori:=1 toM Do the begin -Read (CH); READLN (J,K); Q[i,1]:=j; Q[i,2]:=K; in ifCh='A' Then the begin theInc (MM); B[MM,1]:=k; B[MM,2]:=C[J]; c[j]:=mm; AboutInc (MM); B[MM,1]:=j; B[MM,2]:=C[K]; c[k]:=mm; the End ElseQ[i,0]:=1; the End; the fori:=1 toN Do + ifv[i]=0 ThenSS (I,0); - fori:=1 toN Do the beginBayiFa[i,1]:=i; Fa[i,2]:=1; theR[I]:=NEWT (1, CNT); CL (R[i],xh[i,1]); the End; - fori:=1 toM Do - begin the ifQ[i,0]=0 Then the begin theJ:=q[i,1]; K:=q[i,2]; theJ:=get (j); k:=get (k); -Fa[k,1]:=j; Fa[j,2]:=fa[j,2]+fa[k,2]; ther[j]:=HB (R[j],r[k]); the End Else the begin94J:=q[i,1]; K:=q[i,2]; the ifDEP[J]>DEP[K] Then beginL:=j; J:=k; K:=l;End; theJ:=get (j); K:=QQ (R[j],xh[k,1],xh[k,2]); theWriteln (k* (Fa[j,2]-k));98 End; About End; - End.
View Code
BZOJ4530[BJOI2014] Great Fusion