#P5852. [USACO19DEC] Bessie's Snow Cow P
[USACO19DEC] Bessie's Snow Cow P
Description
Snow has arrived on the farm, and as she does at the beginning of every winter, Bessie is building a snow-cow! Most of the time, Bessie strives to make her sculpture look as much like a real cow as possible. However, feeling artistically inspired, this year she decides to pursue a more abstract route and build a sculpture in the shape of a tree, consisting of snowballs connected by branches, each connecting a pair of snowballs such that there is a unique path between every pair of snowballs.
Bessie has added a nose to one of the snowballs, so it represents the head of the abstract snow cow. She designates it as snowball number 1. To add more visual interest, she plans to dye some of the snowballs different colors in an artistic fashion by filling old milk pails with colored dye and splashing them onto the sculpture. Colors are identified by integers in the range , and Bessie has an unlimited supply of buckets filled with dyes of every possible color.
When Bessie splashes a snowball with a bucket of dye, all the snowballs in its subtree are also splashed with the same dye (snowball is in the subtree of snowball if lies on the path from to the head snowball). By splashing each color with great care, Bessie makes sure that all colors a snowball has been splashed with will remain visible. For example, if a snowball had colors and Bessie splashes it with color , the snowball will then have colors .
After splashing the snowballs some number of times, Bessie may also want to know how colorful a part of her snow-cow is. The "colorfulness" of a snowball is equal to the number of distinct colors such that snowball is colored . If Bessie asks you about snowball , you should reply with the sum of the colorfulness values of all snowballs in the subtree of
Please help Bessie find the colorfulness of her snow-cow at certain points in time.
Input Format
The first line contains and the number of queries .
The next lines each contain two space-separated numbers and , indicating that there is a branch connecting snowballs and .
The last lines each contain a request, with the format and corresponding meanings as follows:
1 x c(Modification): Indicates that Bessie pours a bucket of paint of color onto snowball , dyeing all snowballs in its subtree.2 x(Query): Queries the sum of colorfulness in the subtree of snowball .
Output Format
For each query, output the sum of colorfulness in the queried subtree. To prevent overflow, you must use 64-bit integers.
5 18
1 2
1 3
3 4
3 5
1 4 1
2 1
2 2
2 3
2 4
2 5
1 5 1
2 1
2 2
2 3
2 4
2 5
1 1 1
2 1
2 2
2 3
2 4
2 5
1
0
1
1
0
2
0
2
1
1
5
1
3
1
1
Hint
After the first query of type , snowball is dyed with color .
After the second query of type , snowballs and are dyed with color .
After the third query of type , all snowballs are dyed with color .
京公网安备 11011102002149号