考场上切了不考虑没有逆元的情况(出题人真良心).

把概率都乘到一起后发现求的就是线段树上每个节点保存的权值和的平方的和.

这个的修改和查询都可以通过打标记来实现.

考场代码:

#include <cstdio>
#include <algorithm>
#define lson (now<<1)
#define rson (now<<1|1)
#define ll long long
#define setIO(s) freopen(s".in","r",stdin) , freopen(s".out","w",stdout)
using namespace std;
char *p1, *p2, buf[100000];
namespace IO
{
#define nc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1 ++ )
int rd() {
int x = 0, f = 1;
char c = nc();
while (c < 48) {
if (c == '-')
f = -1;
c = nc();
}
while (c > 47) {
x = (((x << 2) + x) << 1) + (c ^ 48), c = nc();
}
return x * f;
}
};
const int mod=998244353,N=120005;
int arr[N],n,Q;
inline ll qpow(ll base,ll k) {
ll tmp=1;
for(;k;base=base*base%mod,k>>=1)if(k&1)tmp=tmp*base%mod;
return tmp;
}
inline ll inv(ll k) {
return qpow(k,mod-2);
}
struct Node {
int len;
ll sum,sqr,sumlen,sqrlen,lazy;
}t[N<<2];
inline void pushup(int l,int r,int now) {
int mid=(l+r)>>1;
t[now].sum=t[lson].sum;
t[now].sqr=t[lson].sqr;
t[now].sumlen=t[lson].sumlen;
t[now].sqrlen=t[lson].sqrlen;
if(r>mid) {
t[now].sum=(t[now].sum+t[rson].sum)%mod;
t[now].sqr=(t[now].sqr+t[rson].sqr)%mod;
t[now].sumlen=(t[now].sumlen+t[rson].sumlen)%mod;
t[now].sqrlen=(t[now].sqrlen+t[rson].sqrlen)%mod;
}
t[now].sqr=(t[now].sqr+(ll)t[now].sum*t[now].sum)%mod;
t[now].sumlen=(t[now].sumlen+t[now].len*t[now].sum%mod)%mod;
t[now].sqrlen=(t[now].sqrlen+(ll)t[now].len*t[now].len%mod)%mod;
}
inline void mark(int l,int r,int now,ll v)
{
t[now].lazy+=v, t[now].lazy%=mod;
t[now].sqr=(t[now].sqr+((v*v)%mod)*t[now].sqrlen%mod+2ll*v*t[now].sumlen%mod)%mod;
t[now].sumlen=(t[now].sumlen+(v*t[now].sqrlen)%mod)%mod;
t[now].sum=(t[now].sum+(t[now].len*v)%mod)%mod;
}
inline void pushdown(int l,int r,int now)
{
int mid=(l+r)>>1;
if(t[now].lazy)
{
mark(l,mid,lson,t[now].lazy);
if(r>mid) mark(mid+1,r,rson,t[now].lazy);
t[now].lazy=0;
}
}
void build(int l,int r,int now) {
t[now].len=r-l+1;
if(l==r) {
t[now].sum=arr[l];
t[now].sqr=(ll)arr[l]*arr[l]%mod;
t[now].sumlen=t[now].len*t[now].sum%mod;
t[now].sqrlen=(ll)t[now].len*t[now].len%mod;
return;
}
int mid=(l+r)>>1;
if(l<=mid) build(l,mid,lson);
if(r>mid) build(mid+1,r,rson);
pushup(l,r,now);
}
void update(int l,int r,int now,int L,int R,ll v) {
if(l>=L&&r<=R) {
mark(l,r,now,v);
return;
}
pushdown(l,r,now);
int mid=(l+r)>>1;
if(L<=mid) update(l,mid,lson,L,R,v);
if(R>mid) update(mid+1,r,rson,L,R,v);
pushup(l,r,now);
}
int main() {
using namespace IO;
int i,j,cas;
// setIO("b");
n=rd(),Q=rd();
for(i=1;i<=n;++i) arr[i]=rd();
build(1,n,1);
for(cas=1;cas<=Q;++cas) {
int opt,l,r,v;
opt=rd();
if(opt==1) {
l=rd(),r=rd(),v=rd(), update(1,n,1,l,r,v);
}
if(opt==2) {
ll a=t[1].sqr,b=t[1].sum;
printf("%lld\n",a*inv(b)%mod);
}
}
return 0;
}

  

luogu 4927 [1007]梦美与线段树 概率与期望 + 线段树的更多相关文章

  1. [Codeforces235D]Graph Game——概率与期望+基环树+容斥

    题目链接: Codeforces235D 题目大意:给出一棵基环树,并给出如下点分治过程,求点数总遍历次数的期望. 点分治过程: 1.遍历当前联通块内所有点 2.随机选择联通块内一个点删除掉 3.对新 ...

  2. Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树)

    Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树) Description 一棵树上有n个节点,编号分别 ...

  3. 【Luogu P3371&P4779】【模板】单源最短路径(线段树优化Dijkstra)

    线段树优化$\rm dijkstra$ 线段树每个节点维护$[l,r]$中$dist$最小的点,删除则把该点$dist$赋值为$+\infty$,然后更新该点影响到的线段树上的其他节点即可. 可以得到 ...

  4. luogu P6088 [JSOI2015]字符串树 可持久化trie 线段树合并 树链剖分 trie树

    LINK:字符串树 先说比较简单的正解.由于我没有从最简单的考虑答案的角度思考 所以... 下次还需要把所有角度都考察到. 求x~y的答案 考虑 求x~根+y~根-2*lca~根的答案. 那么问题变成 ...

  5. st表、树状数组与线段树 笔记与思路整理

    已更新(2/3):st表.树状数组 st表.树状数组与线段树是三种比较高级的数据结构,大多数操作时间复杂度为O(log n),用来处理一些RMQ问题或类似的数列区间处理问题. 一.ST表(Sparse ...

  6. luogu P3799 妖梦拼木棒

    二次联通门 : luogu P3799 妖梦拼木棒 /* luogu P3799 妖梦拼木棒 用一个桶存下所有的木棒 美剧两根短的木棒长度 后随便乘一乘就 好了.. */ #include <a ...

  7. 2019南昌网络赛  I. Yukino With Subinterval 树状数组套线段树

    I. Yukino With Subinterval 题目链接: Problem Descripe Yukino has an array \(a_1, a_2 \cdots a_n\). As a ...

  8. 树(一)——线段树

    问题 现在有1~30这30个数,数N被抽上的概率正比于1/sqrt(N+1),求满足这个概率分布的随机数发生器. 思路 第一,如何解决这个"概率正比"问题. 第二,如何产生满足条件 ...

  9. BZOJ-1036 树的统计Count 链剖线段树(模板)=(树链剖分+线段树)

    潇爷昨天刚刚讲完...感觉得还可以...对着模板打了个模板...还是不喜欢用指针.... 1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec Memory Lim ...

随机推荐

  1. vue中,基于echarts 地图实现一个人才回流的大数据展示效果

    0.引入echarts组件,和中国地图js import eCharts from 'echarts' import 'echarts/map/js/china.js'// 引入中国地图 1. 设置地 ...

  2. 开启sentry权限控制hue

    参考: cloudera官方授权:包括webui, ldap,sentry https://www.cloudera.com/documentation/enterprise/6/6.2/topics ...

  3. 洛谷 P1879 玉米田Corn Fields 题解

    题面 一道思维难度不大的状态压缩,也并不卡常,但细节处理要格外注意: f[i][j]表示前i行最后一行状态是j的方案数 #include <bits/stdc++.h> #define p ...

  4. 洛谷 U78696 图书馆馆长的考验 题解

    题面 1. 图书馆馆长的考验(library) 红魔馆的拥有者蕾米莉亚的好友帕秋莉是红魔馆的大图书馆的馆长.擅长操纵五行,名言是“万物都有属性.所谓的属性,和弱点是一样的”. 一天,因为魔理沙看了神之 ...

  5. c#获取网络时间

    public static DateTime GetInternetDate()        {            var client = new TcpClient("time.n ...

  6. HTML-复杂动画和变形

    1.复杂动画 (1)涉及到的属性: animation-name:动画名称: animation-duration:单次动画总时长: animation-timing-function:时间函数: a ...

  7. vue单页应用首次加载太慢之性能优化

    问题描述: 最近开发了一个单页应用,上线后发现页面初始加载要20s才能完成,这就很影响用户体验了,于是分析原因,发现页面加载时有个 vendor.js达到了3000多kb,于是在网上查找了一下原因,是 ...

  8. ES6拷贝方法

    ES6 中对象拷贝方法: 方法一: Object.assign() // 对象浅拷贝, 复制所有可枚举属性 const obj1 = {a: 1}; const obj2 = {b: 2}; // c ...

  9. 机器学习-回归中的相关度和R平方值

    1. 皮尔逊相关系数(Pearson Correlation Coefficient) 1.1 衡量两个值线性相关强度的量 1.2 取值范围[-1, 1] 正相关:>0, 负相关:<0, ...

  10. NSUserDefaults的用法

    NSUserDefaults适合存储轻量级的本地数据,比如要保存一个登陆界面的数据,用户名.密码之类的,个人觉得使用NSUserDefaults是首选.下次再登陆的时候就可以直接从NSUserDefa ...