https://www.nowcoder.com/acm/contest/148/D

题意

一个A数组,初始全为0。现有三种操作,1:给区间[L,R]+w;2:把每个位置的元素变为其前缀和;3:求区间[L,R]的和

分析

参考:http://www.cnblogs.com/tetew/p/9504595.html

看到题的时候慌了神,因为1、2操作的可能次数实在太大了,认为是什么巧妙的数据结构。。。

实则是组合数学,脑子不够用啊。

首先我们讨论一下对某个位置的数进行+w的操作后,会对后面有什么影响。

纵列看作是2操作的次数,横排看作位置。45°斜着看,有点像杨辉三角!

于是,如果在(i,j)+w,那么对于位于其右下方的点(x,y)来说,贡献为C(x-i+y-j-1,x-i-1)*w

因为3操作不超过500次,我们记录1和2的操作,对于每次3操作,再O(n)查询。

求区间[L,R]的和时,可以直接solve(x+1,R)-solve(x+1,L-1),solve()是求前面所有的操作1和操作2的贡献,并且要加上这一次的求前缀和的贡献(所以是x+1)。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = + ;
const int maxm = + ;
const int mod = ;
ll fac[maxn],inv[maxn];
ll qpow(ll a,ll b){
ll res=;
while(b){
if(b&) res=res*a%mod;
b>>=;
a=a*a%mod;
}
return res;
}
void init(){
fac[]=;
for(int i=;i<maxn;i++) fac[i]=fac[i-]*i%mod;
inv[maxn-]=qpow(fac[maxn-],mod-);
for(int i=maxn-;i>=;i--) inv[i]=inv[i+]*(i+)%mod;
}
ll C(int n,int m){
if(m>n||m<) return ;
return fac[n]*inv[m]%mod*inv[n-m]%mod;
}
struct ND{
int x,pos,w;
}a[maxn];
int cnt;
ll cal(int x,int y){
ll res=;
for(int i=;i<=cnt;i++){
if(a[i].x<=x&&a[i].pos<=y){
res=(res+C(x-a[i].x+y-a[i].pos-,x-a[i].x-)*a[i].w%mod+mod)%mod;
}
}
return res;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
init();
int T;
int n,m,op,x,y;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
int now=;
cnt=;
while(m--){
scanf("%d",&op);
if(op==){
scanf("%d%d%d",&x,&y,&op);
a[++cnt].x=now-,a[cnt].pos=x,a[cnt].w=op;
a[++cnt].x=now-,a[cnt].pos=y+,a[cnt].w=-op;
}else if(op==){
now++;
}else{
scanf("%d%d",&x,&y);
ll ans=(cal(now+,y)-cal(now+,x-)+mod)%mod;
printf("%lld\n",ans);
}
}
}
return ;
}

牛客网暑期ACM多校训练营(第十场)D Rikka with Prefix Sum (组合数学)的更多相关文章

  1. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...

  2. 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学

    牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...

  3. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  4. 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)

    链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

  5. 牛客网暑期ACM多校训练营(第九场) A题 FWT

    链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...

  6. 牛客网暑期ACM多校训练营(第九场)D

    链接:https://www.nowcoder.com/acm/contest/147/D来源:牛客网 Niuniu likes traveling. Now he will travel on a ...

  7. 牛客网暑期ACM多校训练营(第二场)B discount

    链接:https://www.nowcoder.com/acm/contest/140/B来源:牛客网 题目描述 White Rabbit wants to buy some drinks from ...

  8. 2018牛客网暑期ACM多校训练营(第一场)D图同构,J

    链接:https://www.nowcoder.com/acm/contest/139/D来源:牛客网 同构图:假设G=(V,E)和G1=(V1,E1)是两个图,如果存在一个双射m:V→V1,使得对所 ...

  9. 牛客网暑期ACM多校训练营(第二场) I Car 思维

    链接:https://www.nowcoder.com/acm/contest/140/I来源:牛客网 White Cloud has a square of n*n from (1,1) to (n ...

  10. 牛客网暑期ACM多校训练营(第二场) D money 思维

    链接:https://www.nowcoder.com/acm/contest/140/D来源:牛客网 White Cloud has built n stores numbered from 1 t ...

随机推荐

  1. IDEA+Springboot+JRebel热部署实现

    步骤一:在IDEA中安装JRebel插件(File->settings->plugins->search in repositories),如下图 步骤二:安装完成之后,重启idea ...

  2. [bzoj2288][pojChallenge]生日礼物【贪心+堆+链表】

    题目描述 ftiasch 18岁生日的时候,lqp18_31给她看了一个神奇的序列 A1, A2, -, AN. 她被允许选择不超过 M 个连续的部分作为自己的生日礼物. 自然地,ftiasch想要知 ...

  3. PHP-FPM监控shell

    !/bin/bash #监控的网页地址url="http://dev2.jwsmed.com" #fastcgi启动/重启/停止脚本路径PROG=/data/fistsoft/ph ...

  4. bzoj3702/bzoj2212 二叉树 (线段树合并)

    用线段树记每个子树中包含的数,然后合并的时候算出来逆序对的数量(合并a,b时,就是size[ch[a][1]]*size[ch[b][0]]),来决定这个子树要不要翻转 #include<bit ...

  5. Equivalent Sets HDU - 3836 (Tarjan)

    题目说给出一些子集,如果A是B的子集,B是A的子集,那么A和B就是相等的,然后给出n个集合m个关系,m个关系表示u是v的子集,问你最小再添加多少个关系可以让这n个集合都是相等的 如果这n个几个都是互相 ...

  6. 深挖JDK动态代理(一)

     最近在研究RPC框架,避免不了的就是在RPC调用中使用最多的则是动态代理的机制了,基于此,我们先来研究一下JDK动态代理 我们先来尝试着编写一下JDK动态代理的代码 1. 由于JDK动态代理是基于接 ...

  7. webpack入门(六) API in modules

    A quick summary of all methods and variables available in code compiled with webpack. 用webpack编译的一些变 ...

  8. spring activemq 整合

    创建maven项目 项目目录结构为 首先配置相关maven依赖 <!-- 版本管理 --> <properties> <springframework>4.1.8. ...

  9. 【洛谷P2709】小B的询问

    题目大意:给定一个长度为 N 的序列,M 个询问,静态查询区间 [l,r] 内的不同颜色数的平方和. 题解:直接莫队即可. 代码如下 #include <bits/stdc++.h> #d ...

  10. 实现迁徙学习-《Tensorflow 实战Google深度学习框架》代码详解

    为了实现迁徙学习,首先是数据集的下载 #利用curl下载数据集 curl -o flower_photos.tgz http://download.tensorflow.org/example_ima ...