题目传送门
Cuber QQ always envies those Kejin players, who pay a lot of RMB to get a higher level in the game. So he worked so hard that you are now the game designer of this game. He decided to annoy these Kejin players a little bit, and give them the lesson that RMB does not always work.
This game follows a traditional Kejin rule of “when you are level i, you have to pay ai RMB to get to level i+1”. Cuber QQ now changed it a little bit: “when you are level i, you pay ai RMB, are you get to level i+1 with probability pi; otherwise you will turn into level xi (xi≤i)”.
Cuber QQ still needs to know how much money expected the Kejin players needs to "ke’’ so that they can upgrade from level l to level r, because you worry if this is too high, these players might just quit and never return again.
The first line of the input is an integer t, denoting the number of test cases.
For each test case, there is two space-separated integers n (1≤n≤500 000) and q (1≤q≤500 000) in the first line, meaning the total number of levels and the number of queries.
Then follows n lines, each containing integers ri, si, xi, ai (1≤ri≤si≤10^9, 1≤xi≤i, 0≤ai≤10^9), space separated. Note that pi is given in the form of a fraction r i s i \frac{ri}{si} siri.
The next q lines are q queries. Each of these queries are two space-separated integers l and r (1≤l The sum of n and sum of q from all t test cases both does not exceed 106. For each query, output answer in the fraction form modulo 1e9+7, that is, if the answer is P Q \frac{P}{Q} QP, you should output P⋅Q−1 modulo 1e9+7, where Q−1 denotes the multiplicative inverse of Q modulo 1e9+7. Huge IO (Over 40MB)! IO optimization is preferred. 我们先定义几个量: r , s , x , m r,s,x,m r,s,x,m,分别代表升级概率的分子、分母,退级到第 x x x个,每次需要的钱数 m m m。然后我们再定义 f [ i ] f[i] f[i]表示从第 i i i级升到第 i + 1 i+1 i+1级的期望, s u m [ i ] sum[i] sum[i]表示从第 1 1 1级升到第 i + 1 i+1 i+1级的期望。 如果我们要算出来从第 i i i级升到第 i + 1 i+1 i+1级的期望 f ( i ) f(i) f(i),不妨列一个中学数学学过的期望表格: 从 x x x到 i i i的期望,我们可以用 s u m [ i − 1 ] − s u m [ x − 1 ] sum[i-1]-sum[x-1] sum[i−1]−sum[x−1]表示, 所以嘛,我们可以得出以下方程式: 然后移项化简,得:Output
Sample Input
1
3 2
1 1 1 2
1 2 1 3
1 3 3 4
1 4
3 4
Sample Output
22
12
Hint
解题思路:
m m m
m + 从 x 到 i 的 期 望 + f [ i ] m+从x到i的期望+f[i] m+从x到i的期望+f[i]
r s \frac{r}{s} sr
1 − r s 1-\frac{r}{s} 1−sr
f [ i ] = m × r s + ( m + s u m [ i − 1 ] − s u m [ x − 1 ] + f [ i ] ) × ( 1 − r s ) f[i]=m\times \frac{r}{s}+(m+sum[i-1]-sum[x-1]+f[i])\times (1-\frac{r}{s}) f[i]=m×sr+(m+sum[i−1]−sum[x−1]+f[i])×(1−sr)
f [ i ] = s × m + ( s − r ) ( s u m [ i − 1 ] − s u m [ x − 1 ] ) r f[i]=\frac{s\times m+(s-r)(sum[i-1]-sum[x-1])}{r} f[i]=rs×m+(s−r)(sum[i−1]−sum[x−1])
这样我们就可以算出来 s u m sum sum数组了,然后每次询问 x x x到 y y y的期望,答案就是: s u m [ y − 1 ] − s u m [ x − 1 ] sum[y-1]-sum[x-1] sum[y−1]−sum[x−1]程序如下
#include