Given a tree with n nodes and n — 1 edges. There are total k different colors (k <= n). Each node i has a color c(i) ↵
(1 <= c(i) <= k). Cost of a subtree u equals number of distinct colors in subtree u.↵
↵
Given q queries. Each query belongs to two types:↵
Type 1: (1, u, nw) is to change the color of node u to nw. (1 <= nw <= k) ↵
Type 2: (2, u) print the Cost of subtree u.↵
↵
Constraints:↵
1 <= n <= 5e5↵
1 <= q <= 3e5↵
↵
So far I have only thought of counting distinct colors without updating queries by small-to-large or BIT. ↵
For updating, I just know that type 1 query will affect all the ancestors of u.↵
Still, I don't know the optimized way yet. Can you guys help me ? ↵
Thanks.
(1 <= c(i) <= k). Cost of a subtree u equals number of distinct colors in subtree u.↵
↵
Given q queries. Each query belongs to two types:↵
Type 1: (1, u, nw) is to change the color of node u to nw. (1 <= nw <= k)
Type 2: (2, u) print the Cost of subtree u.↵
↵
Constraints:↵
1 <= n <= 5e5↵
1 <= q <= 3e5↵
↵
So far I have only thought of counting distinct colors without updating queries by small-to-large or BIT. ↵
For updating, I just know that type 1 query will affect all the ancestors of u.↵
Still, I don't know the optimized way yet. Can you guys help me ? ↵
Thanks.