求诸
  • 板块P5142 区间方差
  • 楼主DLDZD
  • 当前回复3
  • 已保存回复3
  • 发布时间2024/10/16 16:41
  • 上次更新2024/10/16 19:43:39
查看原帖
求诸
508593
DLDZD楼主2024/10/16 16:41
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
struct tree{
    int l,r;
    int num;
    long long pf;
}t[400100];
int a[100100];
void push_down(int p){
    t[p].num=t[p<<1].num+t[p<<1|1].num;
    t[p].pf=t[p<<1].pf+t[p<<1|1].pf;
    return ;
}
void build(int p,int l,int r){
    t[p].l=l; t[p].r=r;
    if(l==r){
        t[p].num=a[l];
        t[p].pf=a[l]*a[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(p<<1,l,mid);
    build(p<<1|1,mid+1,r);
    push_down(p);
    return ;
}
void update(int p,int x,int y){
    if(t[p].l==x&&t[p].r==x){
        t[p].num=y;
        t[p].pf=y*y;
        return ;
    }
    int mid=(t[p].r+t[p].l)>>1;
    if(mid>=x) update(p<<1,x,y);
    else update(p<<1|1,x,y);
    push_down(p);
    return ;
}
double ask(int p,int l,int r){
    if(t[p].l>=l&&t[p].r<=r){
        return t[p].num;
    }
    push_down(p);
    int mid=(t[p].l+t[p].r)>>1;
    double ans=0;
    if(mid>=l) ans+=ask(p<<1,l,r);
    if(mid<r) ans+=ask(p<<1|1,l,r);
    return ans;
}
double ask2(int p,int l,int r){
    if(t[p].l>=l&&t[p].r<=r){
        return t[p].pf;
    }
    push_down(p);
    int mid=(t[p].l+t[p].r)>>1;
    double ans=0;
    if(mid>=l) ans+=ask2(p<<1,l,r);
    if(mid<r) ans+=ask2(p<<1|1,l,r);
    return ans;
}
int ksm(int a,int b){
    int ans=1;
    while(b){
        if(b&1) ans=(ans*a)%mod;
        a=(a*a)%mod;
        b>>=1;
    }
    return ans%mod;
}
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    build(1,1,n);
    for(int i=1;i<=m;i++){
        int op,x,y;
        cin>>op>>x>>y;
        if(op==1){
            update(1,x,y);
        }
        else{
            int l=(y-x+1);
            double q1=ask(1,x,y);
            double q2=ask2(1,x,y);
            int num=q2*l-q1*q1;
            int b=ksm(l*l,mod-2)%mod;
            cout<<((b%mod)*(num%mod))%mod<<"\n";
        }
    }

    return 0;
}
2024/10/16 16:41
加载中...