luogu 4927 [1007]梦美与线段树 概率与期望 + 线段树
时间:2019-08-22
本文章向大家介绍luogu 4927 [1007]梦美与线段树 概率与期望 + 线段树,主要包括luogu 4927 [1007]梦美与线段树 概率与期望 + 线段树使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
考场上切了不考虑没有逆元的情况(出题人真良心).
考场代码:
#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; }
原文地址:https://www.cnblogs.com/guangheli/p/11393873.html
- 你绝对想不到,数据地图还能这么玩~
- TensorFlow从0到1 - 17 - Step By Step上手TensorBoard
- 深度强化学习-Actor-Critic算法原理和实现
- 深度强化学习-Policy Gradient基本实现
- TensorFlow从0到1 - 7 - TensorFlow线性回归的参数溢出之坑
- 买卖股票算法题的后续!
- 一个例子教你如何与出题人斗智斗勇
- 用数据来聊聊国产电影~
- 如何买卖股票?不要慌,我有妙招!
- 2017.11.7解题报告
- TensorFlow从0到1 - 11 - 74行Python实现手写体数字识别
- 让priority_queue支持小根堆的几种方法
- 一招解决4道leetcode hard题,动态规划在字符串匹配问题中的应用
- 细数Python中的数据类型以及他们的方法
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Linux端口映射转发的方法
- Centos7如何备份和还原Redis数据的方法
- linux contos6.8下部署kafka集群的方法
- 基于Electron的touchbar开发
- Redis数据结构-简单动态字符串
- 使用new Function创建async方法
- Canvas系列(14):实战-小球碰撞
- IOC CreateBean
- Hugo搭建博客(二)— Hugo+Github Pages搭建博客
- 创建bean的实例
- Spring-IOC-默认标签的解析
- Spring-IoC -加载 Bean-总览
- Spring-IOC-创建Bean-属性填充
- vi与vim如何修改tab为4个空格方法示例
- Linux使用Crontab定时访问某个路由地址的方法