[HNOI2009] Dream pudding
Description
N pudding is placed in a row for M operations. Each time a color's pudding is all turned into another color, then ask the current total number of colors. For example, four pudding with a color of 1,2,2,1, a total of 3 colors.
Input
The first line gives N,m the number of pudding and the number of times a friend is operating. Second row n number a1,a2 ... An indicates that the color of the first I pudding has M line from the third line, and for each operation, if the first number is 1 to change the color, then the two integer, x, y, or X, indicates that turning all colors into y,x may be equal to Y. If the first number is 2 to ask how many colors are currently in question, you should output an integer. 0
Output
Ask for the second type of action, and then output the current number of colors.
Sample Input
4 3
1 2 2 1
2
1 2 1
2
Sample Output
3
1
HINT
Source
Analysis:
Make a list of each color, add each point, and by the way the initial ans is counted
If you do 1, first determine whether the color of x color is y before and after each node, one for y then ans-1, two ans-2, and then merge the x color and the Y color list.
But this is n^2 efficiency and must be optimized.
If we merge a small number of nodes in X and Y to the top, the
Bzoj 1483:[hnoi2009] Fantasy pudding (linked list + heuristic merge)