Topic links
To a tree, each node has color, two operations, one is a node of the subtree are all dyed C, one is to query a node of the subtree how many different colors, c<=60.
One Bitset maintenance per node is possible.
#include <iostream>#include<vector>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>#include<Set>#include<string>#include<queue>#include<stack>#include<bitset>using namespacestd;#definePB (x) push_back (x)#definell Long Long#defineMK (x, y) make_pair (x, y)#defineLson L, M, rt<<1#defineMem (a) memset (a, 0, sizeof (a))#defineRson m+1, R, rt<<1|1#defineMem1 (a) memset (a,-1, sizeof (a))#defineMEM2 (a) memset (a, 0x3f, sizeof (a))#defineRep (i, N, a) for (int i = A; i<n; i++)#defineFi first#defineSe Secondtypedef pair<int,int>PLL;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-8;Const intMoD = 1e9+7;Const intINF =1061109567;Const intdir[][2] = { {-1,0}, {1,0}, {0, -1}, {0,1} };Const intMAXN = 4e5+5;structnode{intto, Nextt;} E[MAXN*2];intlazy[maxn<<2], A[MAXN],inch[MAXN], out[MAXN], head[maxn*2], num, Clock;bitset< the> s[maxn<<2];voidAddintUintv) {e[num].to= V, e[num].nextt = Head[u], head[u] = num++;}voidPushup (intRT) {S[rt]= s[rt<<1]|s[rt<<1|1];}voidPushdown (intRT) { if(Lazy[rt]) {lazy[rt<<1] = lazy[rt<<1|1] =Lazy[rt]; S[rt<<1].reset (); S[rt<<1|1].reset (); S[rt<<1][LAZY[RT]] = s[rt<<1|1][LAZY[RT]] =1; LAZY[RT]=0; return ; }}voidUpdateintValintLintRintLintRintRT) { if(l<=l&&r>=r) {s[rt].reset (); S[rt][val]=1; LAZY[RT]=Val; return ; } pushdown (RT); intm = l+r>>1; if(l<=m) Update (Val, L, R, Lson); if(r>m) Update (Val, L, R, Rson); Pushup (RT);}voidDfsintUintFA) { inch[U] = + +Block; for(inti = Head[u]; ~i; i =e[i].nextt) { intv =e[i].to; if(v = =FA)Continue; DFS (v, u); } out[U] =clock;} Bitset< the> Query (intLintRintLintRintRT) { if(l<=l&&r>=r) {returnS[rt]; } pushdown (RT); Bitset< the>tmp; Tmp.reset (); intm = l+r>>1; if(l<=m) TMP|=query (L, R, Lson); if(r>m) TMP|=query (L, R, Rson); returntmp;}intMain () {intq, N, m, x, y; intch; memset (Head,-1,sizeof(head)); CIN>>n>>m; for(inti =1; i<=n; i++) scanf ("%d", &A[i]); for(inti =1; i<n; i++) {scanf ("%d%d", &x, &y); Add (x, y); Add (y, x); } DFS (1,0); for(inti =1; i<=n; i++) {update (a[i],inch[I],inch[I],1N1); } while(m--) {scanf ("%d%d", &ch, &x); if(ch = =2) printf ("%d\n", Query (inch[x], out[x],1N1). Count ()); Else { intZ; scanf ("%d", &z); Update (Z,inch[x], out[x],1N1); } } return 0;}
Codeforces 617E. New Year Tree DFS sequence + segment tree +bitset