Bzoj 1483:[hnoi2009] Fantasy pudding (linked list + heuristic merge)

Source: Internet
Author: User

[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)

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.