B 君的第二题 (hongkong)

题目大意:

一个长度为\(n(n\le2\times10^5)\)的数组,给定一个数\(k(k\le40)\)。用\(a[i][j]\)表示该数组\(i\)次前缀和中第\(j\)项的值,要求支持以下两种操作:

  1. 输入\(x,y\),将\(a[0][x]\)加上\(y\);
  2. 输入\(x\),求\(a[k][x]\)的值。

思路:

题目询问的实际上就是\(\sum_{i=1}^x\binom{x-i+k-1}{k-1}a[0][i]\)。

我们可以得到

\[\begin{align*}
&\sum_{i=1}^x\binom{x-i+k-1}{k-1}a[0][i]\\
=&\sum_{i=1}^x\sum_{j=0}^{k-1}\binom xj\binom{k-i-1}{k-j-1}a[0][i]\\
=&\sum_{j=0}^{k-1}\binom xj\left(\sum_{i=1}^x\binom{k-i-1}{k-j-1}a[0][i]\right)
\end{align*}
\]

用树状数组维护即可。

时间复杂度\(\mathcal O(mk\log n)\)。

源代码:

#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
register bool neg=false;
while(!isdigit(ch=getchar())) neg|=ch=='-';
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return neg?-x:x;
}
using int64=long long;
constexpr int N=4e5+1,K=41,mod=1e9+7;
int n,m,k,fac[N],ifac[N];
void exgcd(const int &a,const int &b,int &x,int &y) {
if(!b) {
x=1,y=0;
return;
}
exgcd(b,a%b,y,x);
y-=a/b*x;
}
inline int inv(const int &x) {
int ret,tmp;
exgcd(x,mod,ret,tmp);
return (ret%mod+mod)%mod;
}
inline int C(const int &n,const int &m) {
if(n<m) return 0;
return (int64)fac[n]*ifac[m]%mod*ifac[n-m]%mod;
}
class FenwickTree {
private:
int val[K][N];
int lowbit(const int &x) const {
return x&-x;
}
int query(const int &p,const int &v) const {
int ret=0;
if(v==0) return val[k][p];
for(register int i=1;i<=k;i++) {
(ret+=(int64)val[i][p]*C(v+k-i-1,v-1)%mod)%=mod;
}
return ret;
}
public:
int query(const int &p) const {
int ret=0;
for(register int i=p;i;i-=lowbit(i)) {
(ret+=query(i,p-i))%=mod;
}
return ret;
}
void modify(const int &p,const int &x) {
for(register int i=1;i<=k;i++) {
for(register int j=p;j<=n;j+=lowbit(j)) {
(val[i][j]+=(int64)C(i+j-p-1,i-1)*x%mod)%=mod;
}
}
}
};
FenwickTree t;
int main() {
n=getint(),m=getint(),k=getint();
for(register int i=fac[0]=1;i<=n*2;i++) {
fac[i]=(int64)fac[i-1]*i%mod;
}
ifac[n*2]=inv(fac[n*2]);
for(register int i=n*2;i;i--) {
ifac[i-1]=(int64)ifac[i]*i%mod;
}
for(register int i=0;i<m;i++) {
const int opt=getint();
if(opt==0) {
const int x=getint(),y=getint();
t.modify(x,y);
}
if(opt==1) {
printf("%d\n",t.query(getint()));
}
}
return 0;
}

B 君的第二题 (hongkong)的更多相关文章

  1. test20181020 B君的第二题

    题意 分析 考场70分 一看就是裸的kmp,直接打上去. #include<cstdlib> #include<cstdio> #include<cmath> #i ...

  2. test20181016 B君的第二题

    题意 分析 考场暴力50分. 考虑bfs序,一个点的儿子节点的bfs序一定连续,所以对bfs序建线段树,努力打一下就行了. 时间复杂度\(O(n \log n + m \log n)\) #inclu ...

  3. test20181017 B君的第二题

    题意 分析 考场50分 旁边的L君告诉我,求的就是非升子序列的个数,于是写了个树状数组. 但是\(\mod{2333} > 0\)还需要组合数中没有2333的倍数,所以实际上只得了\(a_i \ ...

  4. test20181015 B君的第二题

    题意 分析 考场85分 用multiset暴力,由于教练的机子飞快,有写priority_queue水过了的人. #include<cstdlib> #include<cstdio& ...

  5. test20181019 B君的第二题

    题意 分析 快速子集和变换以及快速超集和变换的裸题. 用\(f(s)\)表示集合s的方案数,初始化为输入中s出现的次数. 做一遍快速子集和变换,此时f(s)表示s及其子集在输入中出现的次数. 对所有f ...

  6. Java蓝桥杯02——第二题集锦:生日蜡烛、星期一、方格计数、猴子分香蕉

    第二题 生日蜡烛(结果填空) 某君从某年开始每年都举办一次生日party,并且每次都要吹熄与年龄相同根数的蜡烛. 现在算起来,他一共吹熄了236根蜡烛. 请问,他从多少岁开始过生日party的? 请填 ...

  7. 05:统计单词数【NOIP2011复赛普及组第二题】

    05:统计单词数 总时间限制:  1000ms 内存限制:  65536kB 描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次 ...

  8. 常见面试第二题之什么是Context

    今天的面试题,也就是我们常见面试题系列的第二题,我们来讲一讲android中的context.我相信大家android开发者一定对于这个context非常熟悉,肯定都有使用过,肯定没有没使用过的.但是 ...

  9. 《学习OpenCV》练习题第五章第二题abc

    代码: #include <stdio.h> #include <opencv/highgui.h> #include <opencv/cv.h> #include ...

随机推荐

  1. Tomcat8配置默认项目

    <!-- 配置默认访问项目 --> <Host name="localhost" appBase="webapps" unpackWARs=& ...

  2. Mojo_1_第一个简单例子

    use Mojolicious::Lite; #根目录,Get方法打开 #正接显示文本text get '/' => sub{ my $service = shift; $service-> ...

  3. Caffe学习笔记4图像特征进行可视化

    Caffe学习笔记4图像特征进行可视化 本文为原创作品,未经本人同意,禁止转载,禁止用于商业用途!本人对博客使用拥有最终解释权 欢迎关注我的博客:http://blog.csdn.net/hit201 ...

  4. LeetCode 20 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  5. Vue组件-动态组件

    动态组件 通过使用保留的 <component> 元素,动态地绑定到它的 is 特性,可以让多个组件使用同一个挂载点,并动态切换: <div id="app6"& ...

  6. Linux-进程间通信(六): 记录锁

    1. 记录锁:记录锁的功能是,当一个进程正在读或者修改文件的某个部分的时候,它可以阻止其他进程修改同一文件区: 2. fcntl记录锁: #include <fcntl.h> int fc ...

  7. c basic library framework - simplec 2.0.0

    前言 - simplec 单元测试 流程介绍 一个关于C基础库 simplec 2.0.0 发布了. 详细的文档介绍请参照 README.md. 说的再多都无用, 抵不上 gdb 一个 b r n. ...

  8. 我的新博客地址http://xxxbw.github.io/

    最近在学github,在github搭了个博客,以后也会使用另外一个博客.有兴趣的小伙伴可以看看~ 地址:http://xxxbw.github.io/

  9. FS Shell命令

    HDFS命令基本格式 hadoop fs -cmd args hdfs dfs -cmd args cat hadoop fs -cat URI [URI .....] 将路径指定文件的内容输出到st ...

  10. CountDownLatch源码浅析

    Cmd Markdown链接 CountDownLatch源码浅析 参考好文: JDK1.8源码分析之CountDownLatch(五) Java并发之CountDownLatch源码分析 Count ...