Under construction

Still being built. Things may change

Regular meeting 6

Time limit5sMemory limit1024 MB

Summary
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 10910^9 vertices. The input format is described below.

MM residents live in Gyeonggak village. Each resident is given a distinct number from 1 to MM. The residents plan to hold QQ regular meetings.

Resident ii lives in vertex xix_i. Several residents can live in the same vertex. Residents dislike traveling far, so resident ii can move at most did_i away from home. The distance between two vertices is the minimum number of edges on a path between them.

Each regular meeting involves residents lil_i through rir_i. For each meeting, find the number of vertices where all participating residents can gather.

Input

The first line contains three integers NN, MM, and QQ. NN is the initial number of vertices, MM is the number of residents, and QQ is the number of regular meetings.

From the second line onward, the next N−1N-1 lines describe the tree. The ii-th line contains three integers uiu_i, viv_i, and cic_i. This means that cic_i new vertices are added on the path between vertices uiu_i and viv_i.

The new vertices are created as follows. The cic_i vertices created by the ii-th input are connected in order from uiu_i toward viv_i. Their numbers run from N+∑j=1i−1cj+1N + \sum_{j=1}^{i-1} c_j + 1 to N+∑j=1icjN + \sum_{j=1}^{i} c_j in sequence.

The given edges are guaranteed to form a single tree.

The next MM lines describe the residents. The ii-th line contains two integers xix_i and did_i. This means resident ii lives in vertex xix_i and can move at most did_i.

The next QQ lines describe the meetings. The ii-th line contains two integers lil_i and rir_i. This means residents lil_i through rir_i take part in that meeting.

Output

Print QQ lines. On the ii-th line, print the number of vertices that all residents taking part in the ii-th meeting can reach.

Constraints

  • 1≤N,M,Q≤100 0001 \leq N, M, Q \leq 100\,000
  • 1≤ui,vi≤N1 \leq u_i, v_i \leq N (1≤i≤N−11 \leq i \leq N-1)
  • ui≠viu_i \neq v_i (1≤i≤N−11 \leq i \leq N-1)
  • ci≥0c_i \geq 0 (1≤i≤N−11 \leq i \leq N-1)
  • N+∑i=1N−1ci≤109N + \sum_{i=1}^{N-1} c_i \leq 10^9
  • 1≤xi≤N+∑j=1N−1cj1 \leq x_i \leq N + \sum_{j=1}^{N-1} c_j (1≤i≤M1 \leq i \leq M)
  • 1≤di≤1091 \leq d_i \leq 10^9 (1≤i≤M1 \leq i \leq M)
  • 1≤li≤ri≤M1 \leq l_i \leq r_i \leq M (1≤i≤Q1 \leq i \leq Q)

Examples1

  1. Example 1

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