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. CSDN Markdown 超链接

    CSDN Markdown 的超链接总是在当前页面打开新的链接,后来发现了一种可以在新窗口打开超链接的语法,如下: <a href="https://zh.wikipedia.org/ ...

  2. Restful API 中的错误处理

    简介 随着移动开发和前端开发的崛起,越来越多的 Web 后端应用都倾向于实现 Restful API. Restful API 是一个简单易用的前后端分离方案,它只需要对客户端请求进行处理,然后返回结 ...

  3. git删除分支步骤

    在本地删除一个分支: git branch -D <本地分支> 在github远程端删除一个分支: git push origin :<远程端分支> 唯一不同的就是冒号代表了删 ...

  4. Selenium+java - 弹出框处理

    一.弹出框分类: 弹出框分为两种,一种基于原生JavaScript写出来的弹窗,另一种是自定义封装好的样式的弹出框,本文重点介绍原生JavaScript写出来的弹窗,另一种弹窗用click()基本就能 ...

  5. Linux虚拟机所装软件说明

    Linux虚拟机所装软件说明 第一台虚拟机192.168.72.201 的 /usr/local/ 目录下放了一下软件: drwxr-xr-x 3 root root 4096 6月 14 19:16 ...

  6. solr使用心得

    /**   * @author  zhipeng  * @date 创建时间:2015-10-10 下午12:15:35   * @parameter     * @return    */ publ ...

  7. Docker相关地址

    Docker社区版(CE)地址: https://hub.docker.com/search/?type=edition&offering=community Docker文档地址: http ...

  8. 算法与数据结构基础 - 链表(Linked List)

    链表基础 链表(Linked List)相比数组(Array),物理存储上非连续.不支持O(1)时间按索引存取:但链表也有其优点,灵活的内存管理.允许在链表任意位置上插入和删除节点.单向链表结构一般如 ...

  9. 精准测试与开源工具Jacoco的覆盖率能力大PK

    导读:本文根据实际使用情况,简要分析了精准测试和类Jacoco等传统白盒工具在设计理念.功能和应用场景的异同点,并阐述了覆盖率技术如何在新型企业开发体系中,发挥应有的重要作用. 覆盖率技术可以说是测试 ...

  10. 【0809 | Day 12】可变长参数/函数的对象/函数的嵌套/名称空间与作用域

    可变长参数 一.形参 位置形参 默认形参 二.实参 位置实参 关键字实参 三.可变长参数之* def func(name,pwd,*args): print('name:',name,'pwd:',p ...