题库练习 Gasoline prices
← 上一题 下一题 →

A15836 | Gasoline prices

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

Berland — is a huge country consisting of $n$ cities. The road network of Berland can be represented as a root tree, that is, there is only $n - 1$ road in the country, and you can get from any city to any other exactly one way, if you do not visit any city twice. For the convenience of representing the country, for each city $i$ , the city $p_i$ is fixed, equal to the first city to which you need to go from the city $i$ to get to the city $1$ . In other words, the city $p_i$ is equal to the ancestor of the city $i$ if the tree is hung for the city $1$ .There is one gas station in each city of Berland. Gas stations have special pricing, and for each gas station there is a fixed range of prices for which they are ready to sell gasoline. A gas station in the city with the number $i$ is ready to sell gasoline at any price from $l_i$ to $r_i$ inclusive.

The King of Berland — is an exemplary family man, and for $m$ years, two sons were born to him every year. The king's children have been involved in public affairs since early childhood, and at the end of each year they check the honesty of gasoline prices. From birth, the king's children, who are born in the year $i$ , are responsible for checking gasoline prices on the ways from the city of $a_i$ to the city of $b_i$ and from the city of $c_i$ to the city of $d_i$ , respectively.

The check is as follows: both children simultaneously start their journey from the cities $a_i$ and $c_i$ , respectively. The first son of the king, born in the year $i$ , moves along the path from the city $a_i$ to the city $b_i$ , and the second — from the city $c_i$ to the city $d_i$ . Children check that the price of gasoline in the city of $a_i$ coincides with the price of gasoline in the city of $c_i$ . Next, they check that the price of gasoline in the second city on the way from $a_i$ to $b_i$ coincides with the price in the second city on the way from $c_i$ to $d_i$ . Then they repeat the same thing for a couple of third cities on their paths and so on. At the end, they check that the price of gasoline in the city of $b_i$ coincides with the price of gasoline in the city of $d_i$ . It is guaranteed that the length of the path from the city $a_i$ to the city $b_i$ coincides with the length of the path from the city $c_i$ to the city $d_i$ .

Gas stations must strictly obey the laws, and therefore all checks of gasoline prices should not reveal violations. Help Berland gas stations find out how many ways they can set gasoline prices for $m$ years. In other words, for each $i$ from $1$ to $m$ , calculate how many ways you can set gasoline prices at all gas stations so that after the birth of the first $i$ pairs of the king's children, all their checks did not reveal violations, and at any gas station the price was in the acceptable price range. Since the number of such methods can be large, calculate the answer modulo $10^9 + 7$ .

输入格式

The first line contains a single integer $n$ ( $1 \le n \le 200\,000$ ) — the number of cities in Berland.

The second line contains $(n - 1)$ numbers $p_2,\ p_3,\ p_4,\ \ldots,\ p_n$ ( $1 \le p_i \le n$ ), where $p_i$ denotes the number of the next city on the way from city $i$ to city $1$ .

In each of the following lines, two integers are given $l_i$ and $r_i$ ( $1 \le l_i \le r_i < 10^9+7$ ), specifying the acceptable range of prices at the gas station number $i$ .

The following line contains a single integer $m$ ( $1 \le m \le 200\,000$ ) — the number of years during which two sons were born to the king.

In each of the following $m$ lines, four integers are given $a_i$ , $b_i$ , $c_i$ and $d_i$ ( $1 \le a_i, b_i, c_i, d_i \le n$ ), specifying two paths on which the king's children will check gasoline prices, born in the year $i$ . It is guaranteed that the length of the path between the cities $a_i$ and $b_i$ is equal to the length of the path between the cities $c_i$ and $d_i$ .

输出格式

In $m$ lines, print one number each. The number in the $i$ line should be equal to the number of ways to set gasoline prices in all cities so that the king's children born in the years up to and including $i$ do not reveal violations in inspections. Output the numbers modulo $10^9 + 7$ .

输入输出样例

输入 #1
5
1 1 2 2
2 4
1 3
1 3
2 4
4 4
4
1 1 2 2
1 2 2 1
3 4 4 3
3 4 3 5
输出 #1
18
18
4
0
输入 #2
8
1 2 3 4 5 8 6
3 7
2 6
3 8
5 10
5 8
2 9
3 8
6 8
4
1 3 7 6
4 1 5 7
1 7 7 1
1 8 2 7
输出 #2
720
120
120
1
C++ 编辑器
输入
输出