Kejin Player

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1901 Accepted Submission(s): 789

Problem Description
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.

Input
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≤109, 1≤xi≤i, 0≤ai≤109), space separated. Note that pi is given in the form of a fraction risi.

The next q lines are q queries. Each of these queries are two space-separated integers l and r (1≤l<r≤n+1).

The sum of n and sum of q from all t test cases both does not exceed 106.

Output
For each query, output answer in the fraction form modulo 109+7, that is, if the answer is PQ, you should output P⋅Q−1 modulo 109+7, where Q−1 denotes the multiplicative inverse of Q modulo 109+7.

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


题意很简单,就是说一个人可以花钱升一级,但是只有一定概率成功,否则掉会x级。

直接看图:


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+10,p=1e9+7;
int T,n,q,dp[N],r[N],s[N],x[N],a[N],L,R;
inline int qmi(int a,int b=p-2){
	int res=1;
	while(b){if(b&1) res=res*a%p; b>>=1; a=a*a%p;}
	return res;
}
signed main(){
	cin>>T;
	while(T--){
		scanf("%lld %lld",&n,&q);
		for(int i=1;i<=n;i++)
			scanf("%lld %lld %lld %lld",&r[i],&s[i],&x[i],&a[i]);
		for(int i=1;i<=n;i++)
			dp[i+1]=((dp[i]+a[i]-dp[x[i]]+p)%p*s[i]%p*qmi(r[i])%p+dp[x[i]])%p;
		while(q--){
			scanf("%lld %lld",&L,&R);	printf("%lld\n",(dp[R]-dp[L]+p)%p);
		}
	}
	return 0;
}