Gasoline Prices
Time limit3.5sMemory limit1024 MB
Given a tree with a price range per station, count valid price assignments after each year's pair of path constraints forces matching prices along both paths.
- Level
Hard9 of 10
- Topics
- Tree, Union-find, String matching
- Solved
- No attempts yet
Problem
Berlandia is a large country with cities. Its road network is a rooted tree, so the country has roads, and between any two cities there is exactly one path that visits no city twice. For each city , is the city that comes first on the way from city to city 1. In other words, when the tree is hung from city 1, is the parent of city . Each city has one gas station. Station can sell gasoline at any price from to inclusive. The king has two sons every year for years. The sons take part in state affairs from early childhood, and at the end of each year they check that gasoline prices are fair. The sons born in year check prices on the path from to and on the path from to . The check works as follows. Both sons start at the same time, from and . The first son walks from to , and the second walks from to . They check that the price at equals the price at . Then they check that the prices at the second cities of the two paths are equal. They repeat this for the third cities, and so on. Finally they check that the price at equals the price at . The two paths are guaranteed to have the same length. The stations must obey the law, so no check may find a violation. Count the ways the stations can set prices over years. For each from 1 to , count the ways to set prices at all stations so that after the first pairs of sons are born, every check passes and every station's price is within its allowed range. Print the answer modulo .
Input
The first line contains (), the number of cities. The second line contains integers (). Each of the next lines contains and (), the allowed price range at station . The next line contains (). Each of the next lines contains , , , and (). The lengths of the paths from to and from to are guaranteed to be equal.
Output
Print lines, each with one integer. The -th line is the number of ways to set prices so that the sons born in years up to and including year find no violation, modulo .
Hint
Consider the first example. After the first two sons are born, the prices at cities 1 and 2 must be equal. There are 2 ways to choose one shared price for cities 1 and 2 that lies in both allowed ranges. So the total is . The second pair checks the paths and . The prices at cities 1 and 2 are already equal, so the answer does not change after the second pair. The third pair checks the paths and . Then the prices at cities 3 and 4 must be equal, and the prices at cities 1 and 2 must be equal. Cities 1 and 2 already match. There are 2 ways to choose a shared price for cities 3 and 4 within their ranges. So the total is . The fourth pair checks the paths and . Then the prices at cities 4 and 5 must be equal. Since cities 3 and 4 already match, cities 3, 4, and 5 must share one price. The price at city 3 must be at most 3, and the price at city 5 must be at least 4. So after the fourth pair is born, there is no way to set prices that pass every check and keep each price in its range.