BZOJ3757 Apple Tree

Source: Internet
Author: User

The MO team algorithm on the tree ... What about Candy Park, too? 、、、 Konjac Konjac not 233

First the DFS sequence, and then the DFS sequence directly mo team is good ...

But my egg hurts. 3h a problem and let no one live?

1 /**************************************************************2 problem:37573 User:rausen4 language:c++5 result:accepted6 time:4000 Ms7 memory:23096 KB8 ****************************************************************/9  Ten#include <cstdio> One#include <cstring> A#include <cmath> -#include <algorithm> -   the using namespacestd; - Const intN =100005; - Const intMaxLen =3500005; -   + intN, Q; - inttot, first[n]; + intDFN, C[n], w[n <<1]; A intSZ, Pos[n <<1]; at intL, R, Lca; - intsum, Cnt[n], ans[n]; - BOOLVis[n <<1]; - CharBuf[maxlen], *c =buf; - intLen; -   in structTree_node { -     intDEP, fa[ -]; to     BOOLVis; +     intSt, en; - } Tr[n]; the   * structEdges { $     intnext, to;Panax Notoginseng edges () {} -Edgesint_n,int_t): Next (_n), to (_t) {} the} e[n <<1]; +   A structQuery { the     intL, R, LCA, A, B, id; +       -InlineBOOL operator< (ConstQuery &b)Const { $         returnPos[l] = = POS[B.L]? R < B.r:pos[l] <POS[B.L]; $     } - }a[n]; -   the voidAdd_edges (intXinty) { -E[++tot] = Edges (first[x], y), first[x] =tot;WuyiE[++tot] = Edges (First[y], x), first[y] =tot; the } -   WuInlineintRead () { -     intx =0; About      while(*c <'0'||'9'< *c) + +C; $      while('0'<= *c && *c <='9') -x = x *Ten+ *c-'0', ++C; -     returnx; - } A   + voidDfsintp) { the     intx, y; -W[tr[p].st = ++dfn] = p, Tr[p].vis =1; $      for(x =1; X <= -; ++x) theTR[P].FA[X] = tr[tr[p].fa[x-1]].fa[x-1]; the      for(x = first[p]; x; x =e[x].next) the         if(!tr[y =E[x].to].vis) theTR[Y].DEP = TR[P].DEP +1, tr[y].fa[0] =p, Dfs (y); -W[tr[p].en = ++DFN] =p; in } the   theInlineintLcaintXinty) { About     inti; the     if(x = = y)returnx; the     if(Tr[x].dep <tr[y].dep) Swap (x, y); the      for(i = -; I >=0; --i) +         if(TR[TR[X].FA[I]].DEP >= tr[y].dep) x =Tr[x].fa[i]; -     if(x = = y)returnx; the      for(i = -; I >=0; --i)Bayi         if(Tr[x].fa[i]! = Tr[y].fa[i]) x = tr[x].fa[i], y =Tr[y].fa[i]; the     returntr[x].fa[0]; the } -   -InlineintCheckintXinty) { the     returnX! = y && cnt[x] &&Cnt[y]; the } the   theInlinevoidUpdateintx) { -     if(Vis[x]) { the         if(! (--cnt[c[x])) --sum; the}Else the         if(! (cnt[c[x]]++)) ++sum;94VIS[X] ^=1; the } the   the intMain () {98Len = Fread (C,1, MaxLen, stdin); AboutBuf[len] =' /'; -     intI, x, y;101n = Read (), Q =read ();102      for(i =1; I <= N; ++i)103C[i] =read ();104      for(i =1; I <= N; ++i) { thex = Read (), y =read ();106         if(x &&y) add_edges (x, y);107     }108tr[1].DEP =1;109Dfs1); the      111SZ = (int) sqrt (n *2+0.5); the      for(i =1; I <= DFN; ++i)113Pos[i] = (I-1)/sz +1; the      for(i =1; I <= Q; ++i) { thex = Read (), y =read (); theA[I].A = Read (), a[i].b = Read (), a[i].id =i;117         if(Tr[x].st >tr[y].st) Swap (x, y);118Lca =LCA (x, y);119         if(Lca = =x) -A[I].L = tr[x].st, A[I].R =Tr[y].st;121         ElseA[I].L = tr[x].en, A[I].R = tr[y].st, A[i].lca =Lca;122     }123      124Sort (A +1, A + Q +1); the      for(i =1, L =1, r =0; I <= Q; ++i) {126          while(R < A[I].R) Update (w[++R]);127          while(R > A[I].R) Update (w[r--]); -          while(L < A[I].L) Update (w[l++]);129          while(L > A[I].L) Update (w[--l]); the         if(A[I].LCA) update (A[I].LCA);131Ans[a[i].id] = sum-Check (a[i].a, a[i].b); the         if(A[I].LCA) update (A[I].LCA);133     }134      for(i =1; I <= Q; ++i)135printf"%d\n", Ans[i]);136     return 0;137}
View Code

BZOJ3757 Apple Tree

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.