Rikka with Prefix Sum

题目描述

Prefix Sum is a useful trick in data structure problems.
For example, given an array A of length n and m queries. Each query gives an interval [l,r] and you need to calculate . How to solve this problem in O(n+m)? We can calculate the prefix sum array B in which Bi is equal to . And for each query, the answer is Br-Bl-1.
Since Rikka is interested in this powerful trick, she sets a simple task about Prefix Sum for you:
Given two integers n,m, Rikka constructs an array A of length n which is initialized by Ai = 0. And then she makes m operations on it.
There are three types of operations:
1. 1 L R w, for each index i ∈ [L,R], change Ai to A+ w.
2. 2, change A to its prefix sum array. i.e., let A' be a back-up of A, for each i ∈ [1,n], change Ai to .
3. 3 L R, query for the interval sum .
Intput:

The first line contains a single number t(1≤ t ≤ 3), the number of the testcases.
For each testcase, the first line contains two integers n,m(1 ≤ n,m ≤ 10^5)
And then m lines follow, each line describes an operation(1 ≤ L ≤ R≤ n, 0 ≤ w ≤ 10^9).
The input guarantees that for each testcase, there are at most 500 operations of type 3.

output:

For each query, output a single line with a single integer, the answer modulo 998244353.

test:

Intput:

1
100000 7
1 1 3 1
2
3 2333 6666
2
3 2333 6666
2
3 2333 6666

output

13002
58489497
12043005

中文题意:给定一个序列A,长度最长为100000,初始值为0,现在有三种操作:

1.对区间[l,r]中所有的数都加上一个值。

2.对整个序列求一次前缀和。

3.询问[l,r]区间内所有a的和。

现在对1,0,0,0求3次前缀和得到下图

可以发现(1,1)对右下角的点的贡献是

接下来我们定义一个变量now记录数组了进行了几次求数组前缀和也就是题目的2号操作。

对于操作1,在[l,r]区间内每个数增加w。

相当于在上次进行2号操作前,在点 L 增加w,在点 R+1 减少w。

例如:在3到5号位置增加1

序号    1 2 3 4 5 6 7 8 9

now     0 0 1 1 1 0 0 0 0     求前缀和后

now-1  0 0 1 0 0 -1 0 0 0   求前缀和前

对于询问的话只要求下次求完前缀和 (位置 R 的值) - (位置 L-1 的值)

对于进行前缀和操作只要将now++即可

具体操作看代码

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = ,mod=;
struct Stack
{
ll lie,time,value;
} st[maxn];
ll fac[maxn+],ifac[maxn+],top; ll quick_pow(ll a,ll b)
{
ll ans=;
while(b)
{
if(b&)
ans=1ll*ans*a%mod;
a=1ll*a*a%mod;
b>>=;
}
return ans;
} void init()
{
fac[]=;
for(int i=; i<=maxn; i++)
fac[i]=1ll*fac[i-]*i%mod;
ifac[maxn]=quick_pow(fac[maxn],mod-);
for(int i=maxn-; i>=; i--)
ifac[i]=1ll*ifac[i+]*(i+)%mod;
} ll C(ll n,ll m)
{
return 1ll*fac[n]*ifac[m]%mod*ifac[n-m]%mod;
} inline ll solve(ll x,ll now)
{
if(x==)return ; ll sum = ;
for(int i=; i<top; i++) ///计算每个更新对点的贡献值
{ if(st[i].lie>x)continue;
ll lie = st[i].lie;
ll per = st[i].time;
ll value = st[i].value; ll aa = x-lie + now-per -;
ll bb = now-per -; sum = (sum + value*C(aa,bb)%mod)%mod;
}
return sum;
} int main()
{
init(); ///预处理阶乘和逆元将计算组合数的时间复杂度降为O(1)
ll t,n,m,op;
scanf("%lld",&t);
while(t--)
{
scanf("%lld%lld",&n,&m);
ll now = ,l,r,value;
top = ;
while(m--)
{
scanf("%lld",&op);
if(op==)
{
scanf("%lld%lld%lld",&l,&r,&value);
st[top].value = value%mod,st[top].time=now-,st[top].lie=l;
top++;
st[top].value =(mod-value)%mod,st[top].time=now-,st[top].lie=r+;
top++;
///将更新存入数组
}
else if(op==)
{
now++;
}
else
{
scanf("%lld%lld",&l,&r);
ll ans = solve(r,now+)-solve(l-,now+);
ans = (ans+mod)%mod;
printf("%lld\n",ans);
}
}
}
return ;
}

Rikka with Prefix Sum(组合数学)的更多相关文章

  1. 2018牛客网暑假ACM多校训练赛(第十场)D Rikka with Prefix Sum 组合数学

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round10-D.html 题目传送门 - https://www.n ...

  2. 牛客网暑期ACM多校训练营(第十场)D Rikka with Prefix Sum (数学)

    Rikka with Prefix Sum 题意: 给出一个数组a,一开始全为0,现在有三种操作: 1.  1 L R W,让区间[L,R]里面的数全都加上W: 2.  2     将a数组变为其前缀 ...

  3. Rikka with Prefix Sum

    Rikka with Prefix Sum 题目 https://www.nowcoder.com/acm/contest/148/D 题目有三个操作 l到r都添加一个数 取一次前缀和 查询区间和 这 ...

  4. 牛客网暑期ACM多校训练营(第十场)D Rikka with Prefix Sum (组合数学)

    https://www.nowcoder.com/acm/contest/148/D 题意 一个A数组,初始全为0.现有三种操作,1:给区间[L,R]+w:2:把每个位置的元素变为其前缀和:3:求区间 ...

  5. 牛客多校10 D Rikka with Prefix Sum 不是数据结构

    https://www.nowcoder.com/acm/contest/148/D 题意: 1e5个数,1e5个操作,操作分为: 1.区间加. 2.整个数列替换为前缀和. 3.区间查询. 查询数小于 ...

  6. 牛客第十场Rikka with Prefix Sum

    由于其中的2操作非常多,我们就需要将其快速的更改,就会用到组合数的东西 其实自己手写一下就可以发现对于一个点增加的值在经过不断地前缀和累加过程中对于一点的贡献满足杨辉三角 所以我们就需要记录一下其中的 ...

  7. 牛客多校第十场-D- Rikka with Prefix Sum

    链接:https://www.nowcoder.com/acm/contest/148/D来源:牛客网 Prefix Sum is a useful trick in data structure p ...

  8. 4.4 CUDA prefix sum一步一步优化

    1. Prefix Sum 前缀求和由一个二元操作符和一个输入向量组成,虽然名字叫求和,但操作符不一定是加法.先解释一下,以加法为例: 第一行是输入,第二行是对应的输出.可以看到,Output[1] ...

  9. CodeForces - 1204E Natasha, Sasha and the Prefix Sums (组合数学,卡特兰数扩展)

    题意:求n个1,m个-1组成的所有序列中,最大前缀之和. 首先引出这样一个问题:使用n个左括号和m个右括号,组成的合法的括号匹配(每个右括号都有对应的左括号和它匹配)的数目是多少? 1.当n=m时,显 ...

随机推荐

  1. 10个常用的linux的命令

    以下就是今天我们要介绍的Linux命令:  man  touch, cat and less  sort and grep  cut  sed  tar  find  diff  uniq  chmo ...

  2. C语言编程入门之--第五章C语言基本运算和表达式-part1

    导读:程序要完成高级功能,首先要能够做到基本的加减乘除.本章从程序中变量的概念开始,结合之前学的输出函数和新介绍的输入函数制作简单人机交互程序,然后讲解最基础的加减法运算,自制简单计算器程序练手. 5 ...

  3. 【算法】【排序】【交换类】快速排序QuickSort

    #include<stdio.h> //快速排序 int main(){ ,,,,,,,,}; +; //基准指针 ; //慢指针 int* j=a; //快指针 int QS(int* ...

  4. 【Java例题】5.1 多项式计算

    1. 计算下列多项式的值. pn=an*x^n+...+a1*x+a0其中,"^"表示乘方. x.n以及ai(i=0,1,...,n-1)由键盘输入. package chapte ...

  5. mysql docker 主从配置

    主从复制相关 前置条件: docker安装的mysql是5.7.26版本 1. 编排docker-compose文件如下: version: '3' services: mysql-master: v ...

  6. Spring Boot 修改静态资源一定要重启项目才会生效吗?未必!

    回顾热部署 Spring Boot 中的热部署相信大家都用过吧,只需要添加 spring-boot-devtools 依赖就可以轻松实现热部署.Spring Boot 中热部署最最关键的原理就是两个不 ...

  7. [Inno Setup]写入注册表时32位系统和64位系统的路由

    昨天下午组内一位同事跟说,他想在Inno Setup的安装包中写入一个注册表.目标位置是HKLM:\Software\下面创建自己的注册表项.然后说尝试了好几次都不行, 但是往HKCU下面写入却是OK ...

  8. Python3源代码编译安装

    Python3源代码编译安装 安装必要工具 yum-utils ,它的功能是管理repository及扩展包的工具 (主要是针对repository) $ sudo yum install yum-u ...

  9. JVM总结(一)

    JVM总结(1) 1.JVM组成: JVM由类加载器子系统.运行时数据区.执行引擎以及本地方法接口组成. 2.JVM运行原理: Java源文件经编译器,编译成字节码程序,通过JVM将每一条指令翻译成不 ...

  10. Netty基础系列(4) --堆外内存与零拷贝详解

    前言 到目前为止,我们知道Nio当中有三个最最核心的组件,分别是:Selelctor,Channel,Buffer.在Netty基础系列(3) --彻底理解NIO 这一篇文章中只是进行了大致的介绍. ...