Under construction

Still being built. Things may change

Bandits

Time limit5sMemory limit1024 MB

Summary
Given a weighted tree of villages, add contracts that protect roads within distance R of a village, and report how many contracts protect a given road.
Level

Hard9 of 10

Topics
Tree, Divide and conquer, Sorting
Solved
No attempts yet

Problem

A kingdom has NN villages and N−1N-1 bidirectional roads, so any pair of villages is connected by a path of one or more roads. The ii-th road connects villages AiA_i and BiB_i and has length CiC_i.

Merchants have been attacked by bandits more and more often, so the king asked his advisor to solve the problem by hiring loyal groups of thugs to act as security agencies. Each security contract guarantees security of all roads within radius RjR_j of the village XjX_j where the group's headquarters is. A road is protected by a contract if it is part of a path of length at most RjR_j from XjX_j to some other village. A road can be protected by several contracts, and it is then more secure.

Process queries about new contracts and answer queries about the security of individual roads, that is, the number of contracts currently protecting that road.

Input

The first line contains the number of villages NN. The next N−1N-1 lines each contain three space-separated integers AiA_i, BiB_i, and CiC_i, describing a road of length CiC_i between villages AiA_i and BiB_i.

The next line contains the number of queries QQ. The following QQ lines describe the queries. A query that adds a new security contract starts with the character + and is followed by the headquarters village XjX_j and the radius RjR_j. A query about the security of a road starts with the character ? and is followed by the number YjY_j of that road. Roads are numbered from 11 to N−1N-1 in the order they are given in the input.

Output

For every query of type ?, output one line with the current number of contracts protecting road YjY_j, in the order the queries are processed.

Constraints

  • 1≤N,Q≤1051 \leq N, Q \leq 10^5
  • 0≤Ci,Rj≤1090 \leq C_i, R_j \leq 10^9

Examples1

  1. Example 1

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