eolymp
bolt
Попробуйте наш новый интерфейс для отправки задач
Задачи

Inspector is Coming

Inspector is Coming

After the New Great Technical Revolution in \textbf{2017} Mankind decided to conquer the entire Universe. In \textbf{2030} first spaceships were constructed and several expeditions were organized away from the Solar System. These expeditions were aimed at colonization of new planets. In \textbf{2032} one of the spaceships reached ZetaX and the expedition met the aboriginal population. Colonization of that planet was a hard task. There were desperate struggles for every meter of the territory. As soon as the army conquered a considerable part of the land, they built a base station there to hold the territory and protect it from the enemy. A road to another base station was laid as well. But since any sort of construction during the war is a very risky business, they built exactly one road to an already existing station. There was only one army conquering ZetaX, so base stations appeared successively one by one. Now the war is over, the army is gone, and \textbf{N} base stations are still on the planet. However, there is still much work to be done. Before ordinary people are allowed to come and settle here, the planet must be checked by a special Imperial inspection. Inspector Mr. Subtle has come to the planet from the Earth. He seems to be very unfriendly. He doesn’t want to confirm that the planet is livable until the road system works well enough. Inspector formulated some silly improvements to be made. He tried to make them as hard as possible to get a bribe, but nobody wants to pay him. The requirements are: it is necessary to improve all roads that are on the way from base station \textbf{u} to base station \textbf{v} and have length from \textbf{W_min} to \textbf{W_max}. Inspector made \textbf{Q} such requirements. Find the number of roads it is necessary to improve and Inspector will have to mark ZetaX as livable. \InputFile The first line of the input file contains \textbf{N} (\textbf{2} ≤ \textbf{N} ≤ \textbf{100000}) --- the number of base stations. Each of the next \textbf{N-1} lines contains three integers \textbf{a_i}, \textbf{b_i} and \textbf{w_i} (\textbf{1} ≤ \textbf{a_i}, \textbf{b_i} ≤ \textbf{N}, \textbf{1} ≤ \textbf{w_i} ≤ \textbf{1000000}) --- the descriptions of the roads in arbitary order. The next line contains an integer \textbf{Q} (\textbf{0} ≤ \textbf{Q} ≤ \textbf{100000}) --- the number of requirements. Each of the next \textbf{Q} lines contains four integers \textbf{u}, \textbf{v}, \textbf{W_min}, and \textbf{W_max} (\textbf{1} ≤ \textbf{u}, \textbf{v} ≤ \textbf{N}, \textbf{1} ≤ \textbf{W_min} ≤ \textbf{W_max} ≤ \textbf{1000000}) --- the requirements themselves. \OutputFile One integer --- the number of roads it is needed to improve.
Лимит времени 1 секунда
Лимит использования памяти 64 MiB
Входные данные #1
3
1 2 20
2 3 10
2
1 3 5 15
1 3 15 25
Выходные данные #1
2