Codeforces 620E New Year tree (Dfs sequence + segment tree)

Source: Internet
Author: User

The topic probably says to a tree, the tree knot has the color (1 to 60), carries on the following two actions: the tree that a node is the root is dyed to a certain color, asks a knot to be the root the subtree to have how many kinds of color.

Subtree, obviously the DFS sequence, which maps the tree nodes to successive intervals. and notice the color 60, so that you can use a 64-bit integer to represent the collection of colors, and then is in this continuous interval with segment tree segments to update the color set and interval query color collection.

1#include <cstdio>2#include <cstring>3 using namespacestd;4 #defineMAXN 5000005 structedge{6     intV,next;7}edge[maxn<<1];8 intNE,HEAD[MAXN];9 voidAddedge (intUintv) {TenEdge[ne].v=v; edge[ne].next=Head[u]; Onehead[u]=ne++; A } -  - intL[MAXN],R[MAXN],DFN; the voidDfsintUintFA) { -l[u]=++DFN; -      for(intI=head[u]; i!=-1; I=Edge[i].next) { -         intv=edge[i].v; +         if(V==FA)Continue; - DFS (v,u); +     } Ar[u]=DFN; at } -  - Long Longtree[maxn<<2],lazy[maxn<<2],z; - intn,x,y; - voidUpdateintIintJintk) { -     if(X<=i && j<=y) { intree[k]=Z; -lazy[k]=Z; to         return; +     } -     if(Lazy[k]) { thetree[k<<1]=Lazy[k]; *tree[k<<1|1]=Lazy[k]; $lazy[k<<1]=Lazy[k];Panax Notoginsenglazy[k<<1|1]=Lazy[k]; -lazy[k]=0; the     } +     intMid=i+j>>1; A     if(x<=mid) Update (i,mid,k<<1); the     if(y>mid) Update (mid+1,j,k<<1|1); +tree[k]=tree[k<<1]|tree[k<<1|1]; - } $ Long LongQueryintIintJintk) { $     if(X<=i && j<=y) { -         returnTree[k]; -     } the     if(Lazy[k]) { -tree[k<<1]=Lazy[k];Wuyitree[k<<1|1]=Lazy[k]; thelazy[k<<1]=Lazy[k]; -lazy[k<<1|1]=Lazy[k]; Wulazy[k]=0; -     } About     intMid=i+j>>1; $     Long Longres=0; -     if(X<=mid) Res|=query (i,mid,k<<1); -     if(Y>mid) Res|=query (mid+1,j,k<<1|1); -     returnRes; A } +  the intGetCount (Long Longs) { -     intCnt=0; $      for(intI=0; i< -; ++i) { the         if(s>>i&1) ++CNT; the     } the     returnCNT; the } -  in intCOLOR[MAXN]; the intMain () { the     intn,m; Aboutscanf"%d%d",&n,&m); the      for(intI=1; i<=n; ++i) { thescanf"%d", color+i); the--Color[i]; +     } -memset (head,-1,sizeof(head)); the     inta,b,c;Bayi      for(intI=1; i<n; ++i) { thescanf"%d%d",&a,&b); the Addedge (A, b); - Addedge (b,a); -     } theDfs1,1); the      for(n=1; n<n; n<<=1); the      for(intI=1; i<=n; ++i) { theX=l[i]; Y=l[i]; z=1ll<<Color[i]; -Update1N1); the     } the      while(m--){ thescanf"%d",&c);94         if(c==1){ thescanf"%d%d",&a,&b); theX=l[a]; Y=r[a]; z=1ll<<b-1; theUpdate1N1);98}Else{ Aboutscanf"%d",&a); -X=l[a]; y=R[a];101printf"%d\n", GetCount (Query (1N1)));102         }103     }104     return 0; the}

Codeforces 620E New Year tree (Dfs sequence + segment 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.