Regular meeting 6
Time limit5sMemory limit1024 MB
Count the vertices within distance d_i of home x_i for every resident i in each queried range l to r, on a tree with up to 1e9 vertices.
- Level
Hard9 of 10
- Topics
- Tree, Segment tree, Binary search
- Solved
- No attempts yet
Problem
Gyeonggak village has a tree structure. The village is so large that the tree can have up to vertices. The input format is described below.
residents live in Gyeonggak village. Each resident is given a distinct number from 1 to . The residents plan to hold regular meetings.
Resident lives in vertex . Several residents can live in the same vertex. Residents dislike traveling far, so resident can move at most away from home. The distance between two vertices is the minimum number of edges on a path between them.
Each regular meeting involves residents through . For each meeting, find the number of vertices where all participating residents can gather.
Input
The first line contains three integers , , and . is the initial number of vertices, is the number of residents, and is the number of regular meetings.
From the second line onward, the next lines describe the tree. The -th line contains three integers , , and . This means that new vertices are added on the path between vertices and .
The new vertices are created as follows. The vertices created by the -th input are connected in order from toward . Their numbers run from to in sequence.
The given edges are guaranteed to form a single tree.
The next lines describe the residents. The -th line contains two integers and . This means resident lives in vertex and can move at most .
The next lines describe the meetings. The -th line contains two integers and . This means residents through take part in that meeting.
Output
Print lines. On the -th line, print the number of vertices that all residents taking part in the -th meeting can reach.
Constraints
- ()
- ()
- ()
- ()
- ()
- ()