Tree in a Segment
Given a rooted tree with n vertices (1 ≤ n ≤ 300000), each vertex is painted with one of n colors. For each vertex v, you need to determine the number of vertices in its subtree that have colors falling within m specified range queries l[i]
, r[i]
. A vertex's color is considered within the range if its color number c satisfies l[i]
≤ c ≤ r[i]
.
Input
The first line contains two integers, n and m. The following n lines provide details for each vertex. Each line contains two integers, p[i]
and c[i]
, where p[i]
is the parent of vertex i, and c[i]
is the color of vertex i (1 ≤ c[i]
≤ n). The root of the tree has p[i]
= 0.
The next m lines contain the range queries, each specified by two integers l[i]
and r[i]
(1 ≤ l[i]
≤ r[i]
≤ n).
Output
Output n lines, each containing m integers. Each line corresponds to a vertex from 1 to n, and each integer represents the count of vertices in the subtree of that vertex whose colors fall within the respective range queries.