Tree in a Segment#2924

Tree in a Segment

2 δευτερόλεπτα
256 megabyte
Εύκολο

Given a rooted tree with (1 \leq n \leq 300\,000) vertices. Each vertex is colored with one of colors. For each vertex , you are given (1 \leq m \leq 10) queries; each query consists of a range l_i, r_i, and you must count how many times colors from this range appear in the subtree rooted at . A vertex color is considered in the range if l_i \leq c \leq r_i.

Είσοδος

The first line contains two integers and .

The next lines describe the vertices, one per line. For each vertex , the description is given by p_i c_i, where p_i is the parent of vertex , and c_i is the color of vertex (1 \leq c_i \leq n). For the root vertex, p_i = 0.

The next lines give the queries, each formed by two numbers l_i, r_i (1 \leq l_i \leq r_i \leq n).

Έξοδος

Output lines, each with numbers. The numbers in the -th line are the counts of vertices in the subtree rooted at vertex whose color falls within each respective query range.

Παραδείγματα

Παράδειγμα 1

Loading
Loading

Παράδειγμα 2

Loading
Loading

Παράδειγμα 3

Loading
Loading

Παράδειγμα 4

Loading
Loading