Title Link: hysbz 2243 dyeing
The main idea of the topic: slightly.
Problem-solving ideas: Tree chain split + segment tree interval merging, but the interval merge is relatively simple, the node only to record the left and right end of the color can be.
#include <cstdio>#include <cstring>#include <algorithm>using namespace STD;Const intMAXN =1e5+5;intN, M, NE, VAL[MAXN], FIRST[MAXN], JUMP[MAXN *2];intID, FAR[MAXN], SON[MAXN], DEP[MAXN], TOP[MAXN], CNT[MAXN], IDX[MAXN];structEdge {intU, v;void Set(intUintV) { This->u = u; This->v = v; }}ED[MAXN *2];inline voidAdd_edge (intAintb) {Ed[ne].Set(A, B); Jump[ne] = First[a]; First[a] = ne++;}voidDfs_fir (intUintPreintd) {Far[u] = pre; Dep[u] = D; Son[u] =0; Cnt[u] =1; for(inti = First[u]; i +1; i = Jump[i]) {intK = ED[I].V;if(k = = pre)Continue; Dfs_fir (k, u, D +1); Cnt[u] + = cnt[k];if(Cnt[son[u]] < cnt[k]) son[u] = k; }}voidDfs_sec (intUintRot) {Top[u] = rot; Idx[u] = id++;if(Son[u]) dfs_sec (Son[u], rot); for(inti = First[u]; i +1; i = Jump[i]) {intK = ED[I].V;if(k = = Far[u] | | k = = Son[u])Continue; Dfs_sec (k, k); }}#Define Lson (x) ((x) <<1)#Define Rson (x) (((x) <<1) |intLC[MAXN <<2], RC[MAXN <<2], V[MAXN <<2];structSeg {intL, R, S; Seg (ints =0,intL =0,intR =0) { This->l = l; This->r = R; This->s = s; }voidMaintain (intD) {s =1; L = R = D; }}ND[MAXN <<2];inlineSeg Merge (Constseg& L,Constseg& R) {if(L.s = =0)returnRif(R.S = =0)returnLreturnSeg (l.s + r.s + (L.R = = r.l?-1:0), L.L, R.R);}inline voidPushdown (intu) {if(V[u]! =-1) {V[lson (u)] = V[rson (u)] = V[u]; Nd[lson (U)].maintain (V[u]); Nd[rson (U)].maintain (V[u]); V[u] =-1; }}inline voidPushup (intu) {nd[u] = merge (Nd[lson (U)], Nd[rson (U)]);}voidBuild (intUintLintR) {Lc[u] = l; Rc[u] = r; V[u] =-1;if(L = = r) {Nd[u].maintain (-1);return; }intMid = (L + r)/2; Build (Lson (U), L, mid); Build (Rson (U), Mid +1, R); Pushup (u);}voidModify (intUintLintRintW) {if(l <= Lc[u] && Rc[u] <= R) {V[u] = W; Nd[u].maintain (w);return; } pushdown (U);intMID = (Lc[u] + rc[u])/2;if(L <= mid) Modify (Lson (U), L, R, W);if(R > Mid) Modify (Rson (U), L, R, W); Pushup (u);} Seg Query (intUintLintR) {if(l <= Lc[u] && Rc[u] <= R)returnNd[u]; Pushdown (U); SEG ret;intMID = (Lc[u] + rc[u])/2;if(L <= mid) ret = merge (ret, query (Lson (U), L, R));if(R > Mid) ret = merge (ret, query (Rson (U), L, R)); Pushup (U);returnRET;}voidInit () {ne =0; ID =1;memset(First,-1,sizeof(first)); for(inti =1; I <= N; i++)scanf("%d", &val[i]);intA, B; for(inti =1; i < N; i++) {scanf("%d%d", &a, &b); Add_edge (A, b); Add_edge (b, a); } Dfs_fir (1,0,0); Dfs_sec (1,1); Build1,1+ i); for(inti =1; I <= N; i++) Modify (1, Idx[i], idx[i], val[i]);/* for (int i = 1; I <= N; i++) {SEG ret = query (1, idx[i], idx[i]); printf ("%d", RET.R); } printf ("\ n"); */}voidModifyintAintBintW) {intp = top[a], q = top[b]; while(P! = q) {if(Dep[p] < dep[q]) {Swap (P, q); Swap (A, b); } Modify (1, Idx[p], Idx[a], W); A = Far[p]; p = Top[a]; }if(Dep[a] > Dep[b]) swap (A, b); Modify1, Idx[a], Idx[b], W);}intQueryintAintb) {intp = top[a], q = top[b]; Seg one, both; while(P! = q) {if(Dep[p] < dep[q]) {Swap (P, q); Swap (A, b); Swap (one, both); } one = merge (query (1, Idx[p], Idx[a]), one); A = Far[p]; p = Top[a]; }if(Dep[a] > Dep[b]) {Swap (A, b); Swap (one, both); } The merge (query (1, Idx[a], idx[b]);intret = one.s + Two.s;if(ONE.L = = TWO.L) ret--;returnRET;}intMain () { while(scanf("%d%d", &n, &m) = =2) {init ();intA, B, w;Charop[5]; while(m--) {scanf("%s%d%d", OP, &a, &b);if(op[0] ==' C ') {scanf("%d", &w); Modify (A, B, W); }Else printf("%d\n", query (A, b)); } }return 0;}
HYSBZ 2243 staining (tree-chain split)