链接:https://www.nowcoder.com/acm/contest/148/D
来源:牛客网

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 .

输入描述:

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.

输出描述:

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

输入例子:
1
100000 7
1 1 3 1
2
3 2333 6666
2
3 2333 6666
2
3 2333 6666
输出例子:
13002
58489497
12043005

-->

示例1

输入

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

输出

13002
58489497
12043005
操作有两种,1操作是给l-r区间内的数都加w,2操作是让这个数列变为它的前缀和序列
我们知道,2操作之后得到的新的序列差分之后就是操作前的序列,所以如果只有2操作的话,就是给你一个差分了很多次之后的序列求原序列
但是它还有1操作,1操作对于差分的级别没有变化,但我们知道在原来的序列的l-r区间+w,其实就是在它的差分序列l处+w,r+1处-w
那么现在的问题就在于在这样的一个差分序列的表格中,如果我们在某个点+w,造成的影响是什么

我们发现在一个点+w之后,影响的是它右下角的所有点,每个行的系数是杨辉三角的一列,所以如果在(i,j)点+w,那么(x,y)点的系数为C(x-ai+y-j-,x-i-)

然后3操作不超过500次,所以我们就记录下每次操作,然后对每次询问O(n)计算即可
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int p=;
const int N=2e5+;
int n,m,T,op,l,r,w,cnt;
struct orz{
int x,pos,w;
}a[N];
ll fac[N],inv[N];
ll poww(ll x,int y)
{
x%=p;
ll ret=;
while (y)
{
if (y&) ret=ret*x%p;
x=x*x%p;
y>>=;
}
return ret;
}
void pre()
{
fac[]=;
for (int i=;i<N;i++) fac[i]=fac[i-]*i%p;
inv[N-]=poww(fac[N-],p-);
for (int i=N-;i>=;i--) inv[i]=inv[i+]*(i+)%p;
}
ll C(int a,int b)
{
if (b>a||b<) return ;
return fac[a]*inv[b]%p*inv[a-b]%p;
}
ll solve(int x,int y)
{
ll ret=;
for (int i=;i<=cnt;i++)
{
if (a[i].x<=x&&a[i].pos<=y)
ret=(ret+C(x-a[i].x+y-a[i].pos-,x-a[i].x-)*(ll)a[i].w%p)%p;
}
return ret;
}
int main()
{
scanf("%d",&T);
pre();
while (T--)
{
scanf("%d%d",&n,&m);
int now=;
cnt=;
while (m--)
{
scanf("%d",&op);
if (op==)
{
scanf("%d%d%d",&l,&r,&w);
cnt++; a[cnt].x=now-; a[cnt].pos=l; a[cnt].w=w%p;
cnt++; a[cnt].x=now-; a[cnt].pos=r+; a[cnt].w=-w%p;
}
else if (op==) now++;
else
{
scanf("%d%d",&l,&r);
ll ans=((solve(now+,r)-solve(now+,l-))%p+p)%p;
printf("%lld\n",ans);
}
}
}
return ;
}

牛客多校第十场-D- Rikka with Prefix Sum的更多相关文章

  1. 牛客多校第十场 A Rikka with Lowbit 线段树

    链接:https://www.nowcoder.com/acm/contest/148/A来源:牛客网 题目描述 Today, Rikka is going to learn how to use B ...

  2. 牛客多校第十场 B Coffee Chicken 递归

    题意: 给你一个“斐波那契”字符串数列,第n项由第n-1项和第n-2项拼接而成,输出某项的某位及其后10位. 题解: 递归求解即可. #include<bits/stdc++.h> usi ...

  3. 牛客多校第十场 E Hilbert Sort 递归,排序

    题意: 给你一个方阵,再在方阵上给定一些点,按照希尔伯特曲线经过的先后顺序为这些点排序 题解: 定义好比较函数后直接调用排序算法即可. 希尔伯特曲线本来就是用于二维到一维的映射的,因此我们可以考虑对于 ...

  4. 牛客多校第十场 D Han Xin and His Troops 中国剩余定理

    题意: 韩信有若干个兵,给定你若干个模数和余数,再给你一个1e18以内的范围限制,求解同余方程组,如果无解,输出“他一定在撒谎”,如果最小解超出范围限制,输出“他可能在撒谎”,否则输出最小解 注意:不 ...

  5. 牛客多校第十场 H Stammering Chemists 判断图同构

    题意: 给出一个无向图,表示一种有机物质的结构式,问你这个有机物质是列表中的哪个. 题解: 判断图同构需要枚举全排列以对应点,但是此题中几乎只需要将点度数排序后一个一个比较,对于甲基位置再加个特判即可 ...

  6. 牛客多校第十场 F Popping Balloons 线段树维护稀疏矩阵

    题意: 给定一个稀疏矩阵,里面有若干个气球,让你横着开三枪,竖着开三枪,问最多能打爆多少气球,要求相同方向,相邻两枪必须间隔r. 题解: 横向记录每列有多少个气球,分别在哪行上. 然后把这个数据改造成 ...

  7. 牛客多校第三场 F Planting Trees

    牛客多校第三场 F Planting Trees 题意: 求矩阵内最大值减最小值大于k的最大子矩阵的面积 题解: 矩阵压缩的技巧 因为对于我们有用的信息只有这个矩阵内的最大值和最小值 所以我们可以将一 ...

  8. 牛客多校第三场 G Removing Stones(分治+线段树)

    牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...

  9. 牛客多校第四场sequence C (线段树+单调栈)

    牛客多校第四场sequence C (线段树+单调栈) 传送门:https://ac.nowcoder.com/acm/contest/884/C 题意: 求一个$\max {1 \leq l \le ...

随机推荐

  1. HDU 2011 多项式求和

    http://acm.hdu.edu.cn/showproblem.php?pid=2011 Problem Description 多项式的描述如下:1 - 1/2 + 1/3 - 1/4 + 1/ ...

  2. Jenkins Jfrog Artifactory 以及docker下的pipeline 容器编排实践

    1. 测试环境情况: Docker主机 10.24.101.99 JFrog Artifactory 主机 (admin password) jenkinx github原始地址:https://gi ...

  3. auto_increment 自增键的一些说明

    导致auto_increment变小的几种情况: 1. alter table xx auto_increment = yy; 2. truncate table 3. restart mysql 第 ...

  4. React 模板

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  5. Bootstrap 引入文件顺序及IE兼容性js

    <!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...

  6. mysql 由decimal 引起的 Warning: Data truncated for column

    今天在使用python 库mysqldb的rawsql的时候遇到一个问题(其实并不是mysqlbean引起的) cls.raw_sql('update {table} set available_am ...

  7. CentOS 安全优化

    1.操作系统和数据库系统管理用户身份鉴别信息令应有复杂度要求并定期更换. 配置# vi /etc/login.defs 系统默认配置: PASS_MIN_LEN=5 #密码最小长度 PASS_MAX_ ...

  8. JavaScript——变量

    本文简述了JavaScript变量的数据类型,以及变量类型检测与类型转换 一.介绍 JavaScript的变量有6种数据类型,包含5种原始类型和1种对象类型.本人比较喜欢用逻辑图的形式总结知识点,这样 ...

  9. jenkins--java配置

    进入jenkins然后-->系统配置-->Global Tool Configuration

  10. Dining POJ - 3281

    题意: f个食物,d杯饮料,每个牛都有想吃的食物和想喝的饮料,但食物和饮料每个只有一份 求最多能满足多少头牛.... 解析: 一道简单的无源汇拆点最大流   无源汇的一个最大流,先建立超级源s和超级汇 ...