题意
给你一个序列 , , 询问一个区间求从中任选一个子区间的权值和的期望带修改
题解
设 sum(L,R)=∑Ri=Lwi, s u m ( L , R ) = ∑ i = L R w i , 可以想到
即所有情况的和除以每种情况的概率
考虑怎么求 ∑Ri=L∑Rj=isum(i,j) ∑ i = L R ∑ j = i R s u m ( i , j )
考虑到每一个 wi w i 会存在于 (i−L+1)×(R−i+1) ( i − L + 1 ) × ( R − i + 1 ) 个区间中 ( ( 分别枚举左右端点
可以得到
化简得
其中那三样东西都可以用线段树维护
特殊的 ∑ni=1i2=(2n+1)n(n+1)6 ∑ i = 1 n i 2 = ( 2 n + 1 ) n ( n + 1 ) 6
注意这题中的一些细节
把边权化为点权时如果直接 −−R − − R 的话那么分母应该是 (R−L+22) ( R − L + 2 2 )
#include<bits/stdc++.h>
#define fp(i,a,b) for(register int i=a,I=b+1;i<I;++i)
#define fd(i,a,b) for(register int i=a,I=b-1;i>I;--i)
#define go(u) for(register int i=fi[u],v=e[i].to;i;v=e[i=e[i].nx].to)
#define file(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
char ss[1<<17],*A=ss,*B=ss;
inline char gc(){return A==B&&(B=(A=ss)+fread(ss,1,1<<17,stdin),A==B)?-1:*A++;}
template<class T>inline void sd(T&x){
char c;T y=1;while(c=gc(),(c<48||57<c)&&c!=-1)if(c==45)y=-1;x=c-48;
while(c=gc(),47<c&&c<58)x=x*10+c-48;x*=y;
}
char sr[1<<21],z[20];int C=-1,Z;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
template<class T>inline void we(T x){
if(C>1<<20)Ot();if(x<0)sr[++C]=45,x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
const int N=1e5+5;
typedef int arr[N];
typedef long long ll;
int n,m,tg[N<<2];ll tr[N<<2][3];
#define lc p<<1
#define rc p<<1|1
inline ll calc(int x){return (ll)(x<<1|1)*x*(x+1)/6;}
inline void Tag(int p,int L,int R,int w){
int x=(R-L+1);tg[p]+=w;
tr[p][0]+=(ll)w*x;
tr[p][1]+=(ll)w*x*(L+R)>>1;
tr[p][2]+=(calc(R)-calc(L-1))*w;
}
inline void down(int p,int L,int R){
int mid=(L+R)>>1,w=tg[p];tg[p]=0;
Tag(lc,L,mid,w),Tag(rc,mid+1,R,w);
}
inline void up(int p){fp(i,0,2)tr[p][i]=tr[lc][i]+tr[rc][i];}
void mdy(int p,int L,int R,int a,int b,int w){
if(a<=L&&R<=b)return Tag(p,L,R,w);
if(tg[p])down(p,L,R);int mid=(L+R)>>1;
if(a<=mid)mdy(lc,L,mid,a,b,w);
if(b>mid)mdy(rc,mid+1,R,a,b,w);up(p);
}
ll qry(int p,int L,int R,int a,int b,int y){
if(a<=L&&R<=b)return tr[p][y];
if(tg[p])down(p,L,R);int mid=(L+R)>>1;
if(b<=mid)return qry(lc,L,mid,a,b,y);
if(a>mid)return qry(rc,mid+1,R,a,b,y);
return qry(lc,L,mid,a,b,y)+qry(rc,mid+1,R,a,b,y);
}
int main(){
#ifndef ONLINE_JUDGE
file("roadxw");
#endif
sd(n),sd(m);--n;
int L,R;char c;ll x,y,g;
while(m--){
while(c=gc(),c<32);sd(L),sd(R);--R;
if(c=='C')sd(x),mdy(1,1,n,L,R,x);
else{
y=(ll)(R-L+2)*(R-L+1)>>1;
x=qry(1,1,n,L,R,0)*(R+1)*(1-L)+qry(1,1,n,L,R,1)*(R+L)-qry(1,1,n,L,R,2);
g=__gcd(x,y);we(x/g),sr[C]='/',we(y/g);
}
}
return Ot(),0;
}