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

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

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

考场代码:

#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. 【电子电路技术】短波红外InGaAs探测器简析

    核心提示: 红外线是波长介于微波与可见光之间的电磁波,波长在0.75-1000μm之间,其在军事.通讯.探测.医疗等方面有广泛的应用.目前对红外线的分类还没有统一的标准,各个专业根据应用的需要,有着自 ...

  2. Java基础/网络经验

    一.Java新特性好文--掘金 1.Java8 新特性指导手册 2.Java 11 已发布,String 还能这样玩 二.Java避坑 1.为什么阿里巴巴不建议在for循环中使用"+&quo ...

  3. mv 命令 移动或重命名文件

    mv 命令 移动或重命名文件 [root@localhost soft]# .txt [root@localhost soft]# [root@localhost soft]# ls .txt [ro ...

  4. LayaAir疑难杂症之四:laya引擎自动断点到bundle.js文件中且无报错,但程序不再执行

    在一次断点调试中,突然程序不再按照博主指定的断点执行,莫名其妙端点到了bundle.js文件中的某一行中,这是不应该的,第一次时间反应就是引擎出了问题,但是总不能让博主卸载重装吧. 经过查找资料,询问 ...

  5. RabbitMQ入门教程(十六):RabbitMQ与Spring集成

    原文:RabbitMQ入门教程(十六):RabbitMQ与Spring集成 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https: ...

  6. markdown的日常使用

    # POI前端接口 ``` 描述: 项目poi接口 作者: X-Wolf 时间: -- ``` ------ [TOC] ------ ##声明 ### 请求地址 ``` DOMAIN/strateg ...

  7. Git 操作 GitHub

    Git安装 https://www.cnblogs.com/taopanfeng/p/11076702.html 设置用户名(设置一次 以后就不用再设置了) git config --global u ...

  8. mybatis 动态SQL查询总结

    背景 ××项目需要提供系统部分函数第三方调用接口,基于安全性和避免暴露数据库表信息的基础上进行函数接口的设计,根据第三方调用身份的权限提供某张表的自定义集合.本项目基于mybatis的持久层框架,支持 ...

  9. 设置SVC模式

    清0:bic 置1:orr 访问cpsr和spdr要用到mrs和msr指令 mrs是把状态寄存器的值赋给通用寄存器 msr是把通用寄存器的值赋给状态寄存器 .text .global _start _ ...

  10. 一、Signalr WebApi客服-数据传输实体

    一.定义消息传输的格式 res不受自己控制 接受ret是自己处理,但是必须包含头像等一系列信息,所有发送的时候消息也是需要传头像的.