题解:P4927 [1007] 梦美与线段树

· · 题解

思路

小学数学警告。没学过的同学左转。

不难注意到对于每个节点走到它的概率是这样的:

P(u)=\frac{sumson}{sumall} \times \frac{sumgrandson}{sumson} \times ... \times \frac{sumu}{sumg...gson}=\frac{sumu}{sumall}

使用小学数学约分。

所以整个题其实就要动态地维护 \frac{\sum_{i=1}^{tl}sumu^2}{sumall}。其中 tl 是整个线段树的节点数量。其中 sumall 是容易的。上面那个东西混乱邪恶,我们考虑使用小学数学知识:(a+b)^2=a^2+2ab+b^2,那么放在线段树上我们就考虑分别维护这几个东西就行。

build & pushup

设 fl 为 \sum_{u} (u.r-u.l+1)^2,意思是对于每一个子树内节点所管辖的叶子节点数平方再加和;

$val$ 为 $\sum_{u} (u.r-r.l+1) \times u.sum$ 意思是对于每一个子树内节点所管辖的叶子节点数乘上权值再加和。 ($fl$ 和 $val$ 都是为了维护区间平方而设立的,这里因为只用求全局的答案所以不用再设立一个变量,直接用 $ans$ 就行了。) $tag$ 就是懒标记,这里是区间加。 根据定义不难写出 pushup 和 build。注意在 pushup 的时候 ans 也会随着权值的更改而更改。 ## addtag & pushdown 考虑 pushdown。设变化值为 $x$。 发现 $fl$ 始终不变; $tag$ 套路是一样的,都是加上 $x$。 $sum$ 套路也是一样的,加上区间长度 $r-l+1$ 和 $x$ 的积。 $val$ 比较特殊,考虑推式子: $val=\sum (u.r-u.l+1)*u.sum

现在的 nval:

nval=\sum (u.r-u.l+1)(u.sum+x*(u.r-u.l+1))=val+u.fl \times x

运用小学数学知识乘法分配律即可。

update

然后就是在 update 的时候递归到出口的时候除了打 tag 还要更新 ans。设当前节点管辖 z 个叶子节点。

具体地,原本 ans=y^2,现在变成了 ans=(y+z \times x)^2=y^2+2 \times y \times z \times x +(z \times x)^2 =ans+2 \times val \times x+fl \times x \times x,这里就体现了 val 和 fl 的作用。

operation

在输出的时候先把 ans 和现在的 sumall 约分,然后用费马小定理或者 exgcd 做就可以了。

总的时间复杂度是 O((n+m)\log_{2}{n})。瓶颈在于线段树。

代码

注意要使用 int128,在计算的过程中会到 10^{32} 左右,但是你没有什么好的办法在计算中就取模,所以只能算完。当然你要写高精度也没人拦着你。

#include<bits/stdc++.h>
#define int __int128
using namespace std;
int mod=998244353;
int qpow(int a,int b){
    int tot=1;
    while(b){
        if(b&1) tot=tot*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return tot;
}
int read(){
    int f=1,h=0;
    char ch=getchar();
    while((ch>'9'||ch<'0')&&ch!='-') ch=getchar();
    if(ch=='-') f=-1,ch=getchar();
    while(ch>='0'&&ch<='9') h=h*10+ch-'0',ch=getchar();
    return f*h;
}
void print(int x){
    if(x<0){
        putchar('-');
        print(-x);
        return ;
    }
    if(x>9) print(x/10);
    putchar(x%10+'0');
}
int n,m,fz,fm,ans,op,t1,t2,t3,tt,a[100010];
struct node{
    int l,r,tag,fl,sum,val;
}tr[400010];
void pushup(int p){
    ans=ans-tr[p].sum*tr[p].sum+(tr[p*2].sum+tr[p*2+1].sum)*(tr[p*2].sum+tr[p*2+1].sum);
    tr[p].sum=tr[p*2].sum+tr[p*2+1].sum;
    tr[p].fl=tr[p*2].fl+tr[p*2+1].fl+(tr[p].r-tr[p].l+1)*(tr[p].r-tr[p].l+1);
    tr[p].val=tr[p*2].val+tr[p*2+1].val+(tr[p].r-tr[p].l+1)*tr[p].sum;
}
void build(int p,int l,int r){
    tr[p].l=l,tr[p].r=r;
    if(l==r){
        ans=ans+a[l]*a[l];
        tr[p].tag=0,tr[p].sum=tr[p].val=a[l],tr[p].fl=1;
        return ;
    }
    int mid=(l+r)/2;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
    pushup(p);
}
void addtag(int p,int x){
    tr[p].tag+=x;
    tr[p].sum+=x*(tr[p].r-tr[p].l+1);
    tr[p].val+=x*tr[p].fl;
}
void pushdown(int p){
    if(tr[p].tag){
        int mid=(tr[p].l+tr[p].r)/2;
        addtag(p*2,tr[p].tag);
        addtag(p*2+1,tr[p].tag);
        tr[p].tag=0;
    }   
}
void update(int p,int l,int r,int x){
    if(l<=tr[p].l&&tr[p].r<=r){
        ans=ans+2*x*tr[p].val+x*x*tr[p].fl;
        addtag(p,x);
        return ;
    }
    int mid=(tr[p].l+tr[p].r)/2;
    pushdown(p);
    if(l<=mid) update(p*2,l,r,x);
    if(mid+1<=r) update(p*2+1,l,r,x);
    pushup(p);
}
signed main(){
    n=read(),m=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
        tt+=a[i];
    }
    build(1,1,n);
    while(m--){
        op=read();
        if(op==1){
            t1=read(),t2=read(),t3=read();
            tt=tt+(t2-t1+1)*t3;
            update(1,t1,t2,t3);
        }
        else{
            fz=ans/__gcd(ans,tt)%mod;fm=tt/__gcd(ans,tt)%mod;
            print(fz*qpow(fm,mod-2)%mod);
            puts("");
        }
    }
    return 0;
}