Traffickers
Time limit3.5sMemory limit1024 MB
Given a tree of cities and traffickers shuttling between city pairs, add or remove routes and count deliveries on a path within a time range.
- Level
Medium7 of 10
- Topics
- Tree, Segment tree, Math
- Solved
- No attempts yet
Problem
Pablo is one of the most notorious men in the Wild West, with influence over cities connected by roads that form a tree. Pablo's trusted men are traffickers, and they transport merchandise nonstop.
Each trafficker is defined by a pair of cities . The trafficker starts at time 0 in city and travels toward along the shortest path. At each integer time, the trafficker delivers one unit of merchandise and moves on to the next city. When a trafficker reaches and delivers there, at the next moment he teleports back to . So if he delivers in at time , he delivers in at time . The traffickers follow their routes forever.
Pablo wants to know whether the system can be improved. He runs three types of queries.
- 1 u v: Adds a trafficker traveling between and .
- 2 u v: Removes a trafficker traveling between and . Such a trafficker is guaranteed to exist.
- 3 u v t1 t2: Counts the total units of merchandise delivered by all current traffickers, in every city on the shortest path between and (both endpoints included), at times from to (both included).
Input
The first line contains the number of cities . The next lines each contain two integers and , meaning there is a direct road between those two cities.
The next line contains , the number of traffickers initially present. The next lines each contain two integers and , the pair of cities defining that trafficker's route.
The next line contains , the number of queries. The next lines each contain one query in the format described above.
Output
For each query of type 3, print the answer on a separate line.
Constraints
- The path of each trafficker covers at most 20 cities, including its endpoints.