点击跳转
假设区间长度是 L L L
根据期望的定义推导一下,得到单次查询的答案为
1 2 c ( L ( C 2 + C ) − ∑ i = l r x i 2 + ∑ i = l r x i ) \frac{1}{2c} \left( L(C^2+C) - \sum_{i=l}^r x_i^2 + \sum_{i=l}^rx_i \right) 2c1(L(C2+C)−i=l∑rxi2+i=l∑rxi)
所以只要拿线段树维护一下普通的和、二次方和就行了
#include
#include
#include
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
#define mod 1000000007ll
struct EasyMath
{
ll prime[maxn], phi[maxn], mu[maxn];
bool mark[maxn];
ll fastpow(ll a, ll b, ll c)
{
ll t(a%c), ans(1ll);
for(;b;b>>=1,t=t*t%c)if(b&1)ans=ans*t%c;
return ans;
}
void exgcd(ll a, ll b, ll &x, ll &y)
{
if(!b){x=1,y=0;return;}
ll xx, yy;
exgcd(b,a%b,xx,yy);
x=yy, y=xx-a/b*yy;
}
ll inv(ll x, ll p) //p是素数
{return fastpow(x%p,p-2,p);}
ll inv2(ll a, ll p)
{
ll x, y;
exgcd(a,p,x,y);
return (x+p)%p;
}
void shai(ll N)
{
ll i, j;
for(i=2;i<=N;i++)mark[i]=false;
*prime=0;
phi[1]=mu[1]=1;
for(i=2;i<=N;i++)
{
if(!mark[i])prime[++*prime]=i, mu[i]=-1, phi[i]=i-1;
for(j=1;j<=*prime and i*prime[j]<=N;j++)
{
mark[i*prime[j]]=true;
if(i%prime[j]==0)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
mu[i*prime[j]]=-mu[i];
phi[i*prime[j]]=phi[i]*(prime[j]-1);
}
}
}
ll CRT(vector<ll> a, vector<ll> m) //要求模数两两互质
{
ll M=1, ans=0, n=a.size(), i;
for(i=0;i<n;i++)M*=m[i];
for(i=0;i<n;i++)(ans+=a[i]*(M/m[i])%M*inv2(M/m[i],m[i]))%=M;
return ans;
}
}em;
struct SegmentTree
{
ll sum[maxn<<2], add[maxn<<2], sum2[maxn<<2], L[maxn<<2], R[maxn<<2];
void maketag_add(ll o, ll v)
{
add[o]+=v;
sum2[o] += v*v%mod*(R[o]-L[o]+1) + 2*v*(sum[o]); sum2[o]%=mod;
sum[o]+=(R[o]-L[o]+1)*v; sum[o]%=mod;
}
void pushdown(ll o)
{
if(L[o]==R[o])return;
if(add[o])
{
maketag_add(o<<1,add[o]);
maketag_add(o<<1|1,add[o]);
add[o]=0;
}
}
void pushup(ll o)
{
sum[o]=sum[o<<1]+sum[o<<1|1];
sum2[o]=sum2[o<<1]+sum2[o<<1|1];
}
void build(ll o, ll l, ll r, ll* array=NULL)
{
ll mid(l+r>>1);
L[o]=l, R[o]=r;
add[o]=0;
if(l==r)
{
if(array)sum[o]=array[l], sum2[o]=array[l]*array[l]%mod;
return;
}
build(o<<1,l,mid,array);
build(o<<1|1,mid+1,r,array);
pushup(o);
}
void Add(ll o, ll l, ll r, ll v)
{
ll mid(L[o]+R[o]>>1);
if(l<=L[o] and r>=R[o]){maketag_add(o,v);return;}
pushdown(o);
if(l<=mid)Add(o<<1,l,r,v);
if(r>mid)Add(o<<1|1,l,r,v);
pushup(o);
}
ll Sum(ll o, ll l, ll r)
{
pushdown(o);
ll mid(L[o]+R[o]>>1), ans(0);
if(l<=L[o] and r>=R[o])return sum[o];
if(l<=mid)ans+=Sum(o<<1,l,r);
if(r>mid)ans+=Sum(o<<1|1,l,r);
return ans%mod;
}
ll Sum2(ll o, ll l, ll r)
{
pushdown(o);
ll mid(L[o]+R[o]>>1), ans(0);
if(l<=L[o] and r>=R[o])return sum2[o];
if(l<=mid)ans+=Sum2(o<<1,l,r);
if(r>mid)ans+=Sum2(o<<1|1,l,r);
return ans%mod;
}
}segtree;
ll n, x[maxn];
int main()
{
ll i, C, Q;
n=read(), C=read(), Q=read();
rep(i,1,n)x[i]=read();
segtree.build(1,1,n,x);
while(Q--)
{
ll l=read(), r=read(), L=r-l+1, add=read();
ll ans = C*C%mod*L + L*C - segtree.Sum2(1,l,r) + segtree.Sum(1,l,r);
ans = ans%mod * em.inv(2*C,mod) %mod;
printf("%lld\n",(ans+mod)%mod);
segtree.Add(1,l,r,add);
}
return 0;
}