bzoj.3757. Apple tree (the tree on the MO team)

Source: Internet
Author: User

Facet links

/*code correctness is not guaranteed. in the DFS sequence do mo teamwhen a point is not another point of the LCA, it is necessary to add their LCA contribution*/#include <cmath>#include <cstdio>#include <cctype>#include <algorithm>#define GC () GetChar ()Const intn=4e5+5, m=1e5+5;intn,m,size,enum,h[n],nxt[n<<1],to[n<<1],dep[n],fa[n],top[n],son[n],sz[n];intnow,ans[m],seq[n<<1],in[n],out[n],id,a[n],tm[n];BOOLVis[n];structques{intL,r,lca,id,x,y;BOOL operator< (ConstQues &a)Const{returnl/size<a.l/size?r<a.r:l/size<a.l/size; }}Q[M];inline intRead () {intnow=0, f=1;Register CharC=GC (); for(;! IsDigit (c); C=GC ())if(c=='-') f=-1; for(; IsDigit (c); now=now*Ten+c-' 0 ', C=GC ());returnNow*f;}inline voidAddedge (intUintV) {to[++enum]=v, nxt[enum]=h[u], h[u]=enum; To[++enum]=u, Nxt[enum]=h[v], h[v]=enum;}voidPre_dfs (intx) {in[x]=++id, seq[id]=x, sz[x]=1;intmx=0; for(intV,I=H[X]; I I=nxt[i])if((V=to[i])!=fa[x]) {fa[v]=x, dep[v]=dep[x]+1, Pre_dfs (v), sz[x]+=sz[v];if(SZ[V]&GT;MX) Mx=sz[v],son[x]=v; } Out[x]=++id, seq[id]=x;}voidDFS2 (intXintTP) {TOP[X]=TP;if(Son[x]) {DFS2 (SON[X],TP); for(intI=H[X]; I I=nxt[i])if(To[i]!=fa[x]&&to[i]!=son[x])    DFS2 (To[i],to[i]); }}intQuery_lca (intXintY) { while(Top[x]!=top[y]) {if(Dep[top[x]]<dep[top[y]]) std::swap (x, y);    X=FA[TOP[X]]; }returndep[x]>dep[y]?y:x;}voidCalc (intP) {if(Vis[p])if(!--Tm[a[p]])--now;Else;Else if(++tm[a[p]]==1) ++now; vis[p]^=1;}intMain () {N=read (), M=read (), Size=sqrt (n<<1); for(intI=1; i<=n; ++i) A[i]=read (); for(intU,v,i=1; i<=n;    ++i) U=read (), V=read (), Addedge (U,V); Pre_dfs (0), DFS2 (0,0); for(intU,v,w,i=1; i<=m; ++i) {u=read (), V=read (), W=query_lca (u,v), Q[i].x=read (), Q[i].y=read ();if(In[u]>in[v]) Std::swap (U,V);if(w==u) q[i].l=in[w],q[i].r=in[v],q[i].lca=0;or else if (w==v) q[i].r=in[w],q[i].r=in[u],q[i].lca=0;        ElseQ[i].l=out[u],q[i].r=in[v],q[i].lca=w;    Q[i].id=i; } std::sort (q+1Q+1+M); tm[0]=n; for(intL=1, r=0, i=1; i<=m; ++i) { while(L&LT;Q[I].L) Calc (seq[l++]); while(L&GT;Q[I].L) Calc (Seq[--l]); while(R&LT;Q[I].R) Calc (Seq[++r]); while(R&GT;Q[I].R) Calc (seq[r--]);if(Q[I].LCA) Calc (Q[I].LCA);if(TM[Q[I].X]&AMP;&AMP;TM[Q[I].Y]&AMP;&AMP;Q[I].X&AMP;&AMP;Q[I].Y) Ans[q[i].id]=now-1;ElseAns[q[i].id]=now;if(Q[I].LCA)    Calc (Q[I].LCA); } for(intI=1; i<=m; ++i) printf ("%d\n", Ans[i]);return 0;}

bzoj.3757. Apple tree (the tree on the MO team)

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.