Coloring Roads

Time limit4sMemory limit1024 MB

Summary
Color every edge on a root path with a given color and then count colors used on exactly m edges, answering Q updates online.
Level

Hard8 of 10

Topics
Tree, Segment tree, Hash map, Implementation
Solved
No attempts yet

Problem

In RUN-land there are nn cities numbered 11 to nn. Some pairs of cities are connected by a bidirectional road. There are n−1n-1 roads in total, and for any two cities there is a unique path from one to the other.

City 11 is the capital. Initially no road has a color. Alex, the king of RUN-land, asks you to perform the following query QQ times.

  • u c mu\ c\ m: Given a city uu, a color cc, and an integer mm, color every road on the unique path from uu to the capital in color cc. If a road already has a color, change its color to cc. After coloring, compute the number of colors in which exactly mm roads are colored.

Given QQ queries in total, compute the answer to the second part of each query.

Input

The first line of the input contains three integers n, C, Qn,\ C,\ Q (1≤n, C, Q≤200,0001\leq n,\ C,\ Q\leq 200,000), separated by a single space: the number of cities in RUN-land, the number of possible colors, and the number of queries.

Each of the next n−1n-1 lines contains two integers u, vu,\ v (1≤u, v≤n1\leq u,\ v\leq n), meaning that a bidirectional road directly connects the cities numbered uu and vv.

Each of the next QQ lines contains a query, which consists of 33 integers u, c, mu,\ c,\ m as described in the statement. (1≤u≤n1\leq u\leq n, 1≤c≤C1\leq c\leq C, 0≤m≤n−10\leq m\leq n-1)

Output

Print QQ lines, one for each query. Each line must contain one integer, the answer to the corresponding query.

Examples1

  1. Example 1

    Input
    6 5 5
    1 3
    2 3
    1 4
    6 3
    5 2
    5 1 3
    6 2 2
    2 3 1
    4 4 1
    1 5 0
    
    Expected output
    1
    2
    2
    3
    1