Meaning of the questions: Given the current level of spending needed to upgrade each upgrade may fail and then level off q times inquiry from l to r-level spending expectations
Ideas: a single upgrade to expect, we can list the equation:
So we can look at statistics prefix and answer every inquiry O1
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const int N = 5e5+7; const int inf = 0x3f3f3f3f; const double eps = 1e-6; typedef long long ll; const ll mod = 1e9+7; ll dp[N],r[N],s[N],x[N],a[N]; ll q_pow(ll a,ll n){ ll ans=1; ll base=a; while(n){ if(n&1) ans=(ans*base)%mod; base=base*base%mod; n>>=1; } return years; } ll inv(ll a,ll b){ return q_pow(a,b-2); } int main () { ios::sync_with_stdio(false); cin.tie ( 0 ); cout.tie ( 0 ); int t; cin>>t; while(t--){ int n,q; cin>>n>>q; for(int i=1;i<=n;i++){ cin>>r[i]>>s[i]>>x[i]>>a[i]; } for(int i=1;i<=n;i++){ dp[i+1]=(s[i]*((dp[i]+a[i])%mod)%mod-(s[i]-r[i])%mod*dp[x[i]]%mod+mod)%mod*inv(r[i],mod)%mod; } for(int i=1;i<=q;i++){ int l,r; cin>>l>>r; cout<<(dp[r]-dp[l]+mod)%mod<<endl; } } }