我们可以先将区间方差的公式拆开,得到:
我们还知道,区间平均数为:
因为只需要单点修改,所以我们就只用两颗树状数组维护区间的平方和以及区间和即可。
代码
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll N=3e5+10,P=998244353;
ll a[N];
ll ksm(ll a,ll b){
ll res=1;
for(a%=P;b;b>>=1,(a*=a)%=P) if(b&1) (res*=a)%=P;
return res;
}
struct node{
ll n;
vector<ll> c;
node(ll _n):n(_n),c(_n+2){}
void add(ll x,ll k){
while(x<=n) (c[x]+=k)%=P,x+=(x&(-x));
}
ll query(ll x){
ll sum=0;
while(x) (sum+=c[x])%=P,x-=(x&(-x));
return sum;
}
};
int main(){
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
ll n,m;cin>>n>>m;
node t1(n),t2(n);
for(ll i=1;i<=n;i++){
cin>>a[i];
t1.add(i,a[i]);
t2.add(i,a[i]*a[i]%P);
}
while(m--){
ll op,x,y;cin>>op>>x>>y;
if(op==2){
t1.add(x,y-a[x]);
t2.add(x,((y*y-a[x]*a[x])%P+P)%P);
a[x]=y;
}else{
cout<<ksm(y-x+1,P-2)*((t1.query(y)-t1.query(x-1))%P+P)%P<<" ";
cout<<(((t2.query(y)-t2.query(x-1))%P+P)%P-ksm(y-x+1,P-2)*ksm(((t1.query(y)-t1.query(x-1)+P)%P),2)%P+P)%P<<"\n";
}
}
return 0;
}