Bandits
Time limit5sMemory limit1024 MB
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 villages and bidirectional roads, so any pair of villages is connected by a path of one or more roads. The -th road connects villages and and has length .
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 of the village where the group's headquarters is. A road is protected by a contract if it is part of a path of length at most from 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 . The next lines each contain three space-separated integers , , and , describing a road of length between villages and .
The next line contains the number of queries . The following lines describe the queries. A query that adds a new security contract starts with the character + and is followed by the headquarters village and the radius . A query about the security of a road starts with the character ? and is followed by the number of that road. Roads are numbered from to 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 , in the order the queries are processed.