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. 【Android】Error:Execution failed for task ':app:lint'

    详细信息如下: Error:Execution failed for task ':app:lint'. > Lint found errors in the project; aborting ...

  2. Android Studio 蓝牙开发实例——基于Android 6.0

    因项目需要做一个Android 的蓝牙app来通过手机蓝牙传输数据以及控制飞行器,在此,我对这段时间里写的蓝牙app的代码进行知识梳理和出现错误的总结. 该应用的Compile Sdk Version ...

  3. Something wrong with EnCase v8 index search results

    My friend told me that she installed EnCase v8.05 on her workstation which OS version is Win 10. She ...

  4. S2:ArrayList

    1.ArrayList   ArrayList非常类似于数组,也有人称它为数组列表,ArrayList可以动态维护. 因为数组的长度是固定的,而SArrayList的容量可以根据需要自动扩充. Arr ...

  5. Appium+python自动化(二十九)- 模拟手指在手机上多线多点作战 - 多点触控(超详解)

    简介 在网页中我们经常使用缩放操作来便利的查看具体的信息,在appium中使用MultiAction多点触控的类来实现.MultiAction是多点触控的类,可以模拟用户多点操作.主要包含加载add( ...

  6. Python基础总结之初步认识---class类的继承(下)。第十五天开始(新手可相互督促

    年薪百万的步伐慢了两天hhhh严格意义是三天.最近买了新的玩具,在家玩玩玩!~~~~ 今天开始正式认识类的继承.类的继承是怎么继承呢?看下代码: class Animal(object): #父类 d ...

  7. 机器学习中的误差 Where does error come from?

    误差来自于偏差和方差(bias and variance)   对于随机变量 X,假设其期望和方差分别为 μ 和 σ2.随机采样 N 个随机变量构成样本,计算算术平均值 m,并不会直接得到 μ (除非 ...

  8. android——卡片式布局

    一.CardView <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk ...

  9. 体验使用MUI上手练习app页面开发

    因为公司安排需要先学习一点app开发,而安排学习的框架就是MUI,上手两天体验还算可以(来自后端人员的懵逼),靠着MUI的快捷键可以快速的完成自己想要的样式模板,更多的交互性的内容则需要使用js来完成 ...

  10. Java连载16-++传参&关系运算符

    一.++再举例 int a = 10; System.out.print(a++);//这里会打印出10,因为他们内部这个print函数有参数相当于参数x=a++ System.out.println ...