"BZOJ1103" Metropolitan Meg (Dfs sequence, tree array)

Source: Internet
Author: User
Tags ord

Test instructions: There is a tree, 1th points for the root, to ensure that the number of small point depth small, the initial state of each edge is not marked, requires two operations online:

A: Marks the edge of the connection x, Y

W: How many edges are not marked on the path from 1 to X

N<=2*10^5

Idea: The special nature of the subject:

1. Mark only one edge at a time and no heavy edges

2. Direct path of 1 to X without LCA

Record the first and last occurrences of the I point in the DFS sequence B[i] and C[i]

It can be found that if (x, y) (x<y) edge is marked only for the interval (B[y],c[y]) has a 1 contribution

Equivalent to prefix and s[b[y]]++ s[c[y]+1]--

As for s[c[y]+1]--or s[c[y]]--is not important, because the question is definitely s[a[i]], and the DFS sequence will not appear duplicate

Single point modification, tree array prefix and implementation

1 varHead,vet,next,a,b,c,t,flag:Array[1..1000000] ofLongint;2 N,m,tot,i,x,y,time,j,k:longint;3 ch:string;4 5 procedureAdd (a,b:longint);6 begin7 Inc (TOT);8next[tot]:=Head[a];9vet[tot]:=b;Tenhead[a]:=tot; One End; A  - procedureDFS (u:longint); - varE,v:longint; the begin -flag[u]:=1; Inc (time); A[time]:=u; b[u]:=Time ; -e:=Head[u]; -   whileE<>0  Do +  begin -v:=Vet[e]; +   ifflag[v]=0  ThenDfs (v); Ae:=Next[e]; at  End; -Inc (time); A[time]:=u; c[u]:=Time ; - End; -  - functionlowbit (x:longint): Longint; - begin inExit (x and(-x)); - End; to  + functionsum (x:longint): Longint; - begin thesum:=0; *   whileX>0  Do $  beginPanax Notoginsengsum:=sum+T[x]; -x:=x-lowbit (x); the  End; + End; A  the procedureUpdate (x,y:longint); + begin -   whilex<=n<<1  Do $  begin $t[x]:=t[x]+y; -x:=x+lowbit (x); -  End; the End; - Wuyi begin theAssign (input,'bzoj1103.in'); Reset (input); -Assign (output,'Bzoj1103.out'); Rewrite (output); Wu READLN (n); -   fori:=1  toN-1  Do About  begin $ readln (x, y); - Add (x, y); - Add (y,x); -  End; ADfs1); +   fori:=1  toN Do the  begin -Update (B[i],1); $Update (c[i],-1); the  End; the Readln (m); the   fori:=1  ton+m-1  Do the  begin -READLN (CH); x:=0; y:=0; in   ifch[1]='W'  Then the   begin the     forj:=3  toLength (CH) Dox:=x*Ten+ord (Ch[j])-ord ('0'); AboutWriteln (SUM (b[x])-1); the   End; the   ifch[1]='A'  Then the   begin +     forj:=3  toLength (CH) Do -    begin the     ifch[j]=' '  ThenBreak ;Bayix:=x*Ten+ord (Ch[j])-ord ('0'); the    End; thek:=J; -     forj:=k+1  toLength (CH) Doy:=y*Ten+ord (Ch[j])-ord ('0'); -Update (b[y],-1); theUpdate (C[y],1); the   End; the  End; the close (input); - close (output); the End.

"BZOJ1103" Metropolitan Meg (Dfs sequence, tree array)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.