CF446C DZY Loves Fibonacci Numbers 线段树 + 数学
有两个性质需要知道:
$1.$ 对于任意的 $f[i]=f[i-1]+f[i-2]$ 的数列,都有 $f[i]=fib[i-2]\times f[1]+fib[i-1]\times f[2]$
其中 $fib[i]$ 为第 $i$ 项斐波那契数列.
$2$. 对于任意满足上述条件的数列,都有 $\sum_{i=1}^{n}f[i]=f[n+2]-f[2]$
$3.$ 任意两断满足上述条件的数列每一项依次叠加,依然满足 $g[i]=g[i-1]+g[i-2]$,且上述两个性质都满足.
$4.$ 任何一段斐波那契数列也满足上述所有性质.
有了上述预备知识后,再考虑这道题:
我们用线段树来维护区间和,线段树上每个节点维护 $3$ 个信息,为 $sum,f1,f2$
即节点所维护的区间和,以及该节点及线段树中区间要加上一个前两项为 $f1,f2$ 的上述递推数列.
那么,我们只需考虑如何下传标记,如何查询即可.
假设当前节点已经有了 $f1,f2$,那么将标记下传给左子树是轻松的:直接下传即可,区间和的贡献可按照上述公式 $O(1)$ 求出.
而如果要下传给右儿子的话就不能直接传了,因为右儿子区间开头的两项并不是 $f1,f2$.
而根据上述三条性质,我们知道斐波那契数列的任何一段也是斐波那契数列.
所以,直接算出右儿子的 $f1,f2$ 即 $f1\times fib[mid-l]+f2\times fib[mid-l+1]$ 与 $f1\times fib[mid-l+1]+f2\times fib[mid-l+2]$
然后还知道 $f1,f2$ 都满足叠加性,所以直接叠加到左右儿子的 $f1,f2$ 上即可.
#include <bits/stdc++.h>
#define N 400004
#define LL long long
#define lson now<<1
#define rson now<<1|1
#define setIO(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
using namespace std;
const LL mod=1000000009;
int n,m;
LL fib[N<<1],sum[N<<1];
struct node
{
LL f1,f2,sum;
int l,r,len;
}t[N<<2];
void build(int l,int r,int now)
{
t[now].l=l;
t[now].r=r;
t[now].len=r-l+1;
if(l==r) return ;
int mid=(l+r)>>1;
if(l<=mid) build(l,mid,lson);
if(r>mid) build(mid+1,r,rson);
}
void mark(int now,LL f1,LL f2)
{
(t[now].f1+=f1)%=mod;
(t[now].f2+=f2)%=mod;
(t[now].sum+=f1*fib[t[now].len]%mod+f2*fib[t[now].len+1]%mod-f2+mod)%=mod;
}
void pushup(int now)
{
t[now].sum=(t[lson].sum+t[rson].sum)%mod;
}
void pushdown(int now)
{
if(t[now].f1==0&&t[now].f2==0) return;
int mid=(t[now].l+t[now].r)>>1;
mark(lson,t[now].f1,t[now].f2);
if(t[now].r>mid)
mark(rson,t[now].f1*fib[t[lson].len-1]%mod+t[now].f2*fib[t[lson].len]%mod,t[now].f1*fib[t[lson].len]%mod+t[now].f2*fib[t[lson].len+1]%mod);
t[now].f1=t[now].f2=0;
}
void update(int l,int r,int now,int L,int R)
{
if(l>=L&&r<=R)
{
mark(now,fib[l-L+1],fib[l-L+2]);
return;
}
pushdown(now);
int mid=(l+r)>>1;
if(L<=mid) update(l,mid,lson,L,R);
if(R>mid) update(mid+1,r,rson,L,R);
pushup(now);
}
LL query(int l,int r,int now,int L,int R)
{
if(l>=L&&r<=R)
{
return t[now].sum;
}
pushdown(now);
int mid=(l+r)>>1;
LL re=0ll;
if(L<=mid) re+=query(l,mid,lson,L,R);
if(R>mid) re+=query(mid+1,r,rson,L,R);
return re%mod;
}
int main()
{
// setIO("input");
int i,j;
scanf("%d%d",&n,&m);
fib[1]=fib[2]=1;
for(i=3;i<N;++i) fib[i]=(fib[i-1]+fib[i-2])%mod;
for(i=1;i<=n;++i) scanf("%lld",&sum[i]), (sum[i]+=sum[i-1])%=mod;
build(1,n,1);
for(i=1;i<=m;++i)
{
int opt,l,r;
scanf("%d%d%d",&opt,&l,&r);
if(opt==1) update(1,n,1,l,r);
else printf("%lld\n",(query(1,n,1,l,r)+sum[r]-sum[l-1]+mod*2)%mod);
}
return 0;
}
CF446C DZY Loves Fibonacci Numbers 线段树 + 数学的更多相关文章
- ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)
Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...
- codeforces 446C DZY Loves Fibonacci Numbers 线段树
假如F[1] = a, F[2] = B, F[n] = F[n - 1] + F[n - 2]. 写成矩阵表示形式可以很快发现F[n] = f[n - 1] * b + f[n - 2] * a. ...
- Codeforces 446C DZY Loves Fibonacci Numbers [线段树,数论]
洛谷 Codeforces 思路 这题知道结论就是水题,不知道就是神仙题-- 斐波那契数有这样一个性质:\(f_{n+m}=f_{n+1}f_m+f_{n}f_{m-1}\). 至于怎么证明嘛-- 即 ...
- Codeforces446C DZY Loves Fibonacci Numbers(线段树 or 分块?)
第一次看到段更斐波那契数列的,整个人都不会好了.事后看了题解才明白了一些. 首先利用二次剩余的知识,以及一些数列递推式子有下面的 至于怎么解出x^2==5(mod 10^9+9),我就不知道了,但是要 ...
- 【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers
我这种maintain写法好zz.考试时获得了40pts的RE好成绩 In mathematical terms, the sequence Fn of Fibonacci numbers is de ...
- cf446C DZY Loves Fibonacci Numbers
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...
- Codeforces 446-C DZY Loves Fibonacci Numbers 同余 线段树 斐波那契数列
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- Codeforces 446C —— DZY Loves Fibonacci Numbers(线段树)
题目:DZY Loves Fibonacci Numbers 题意比較简单,不解释了. 尽管官方的题解也是用线段树,但还利用了二次剩余. 可是我没有想到二次剩余,然后写了个感觉非常复杂度的线段树,还是 ...
随机推荐
- 【转】基于FPGA的Sobel边缘检测的实现
前面我们实现了使用PC端上位机串口发送图像数据到VGA显示,通过MATLAB处理的图像数据直接是灰度图像,后面我们在此基础上修改,从而实现,基于FPGA的动态图片的Sobel边缘检测.中值滤波.Can ...
- proxy_banner
- SpringBoot--整合Mybatis+druid
分为两部分,首先替换默认数据源为阿里德鲁伊并添加监控,其次是SpringBoot下使用Mybatis 替换数据源为德鲁伊 首先在配置文件里配置好数据库连接的基本信息,如username passwor ...
- python 包 安装 加速 pip anaconda
使用 -i 参数指定源,豆瓣的很快: pip install web.py -i http://pypi.douban.com/simple anaconda: ~/.condarc channels ...
- ASP.NET 一般处理程序 接收文件上传
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain&qu ...
- javascript创建一个基于对象的栈结构
上篇博客介绍了基于数组创建一个栈,这是用对象创建一个栈 s1.声明一个Stack类 class Stack { constructor() { this.count = 0; this.items = ...
- ECSHOP v3.0 数据库字典
商品相关表 商品分类表 category 此表用来维护商品分类信息 字段名 字段描述 字段类型 默认值 索引 cat_id 分类编号 smallint(5) unsigned 自增 PK cat_na ...
- 编译制作Linux 3.18内核rpm包(升级centos6.x虚拟机内核)
介绍 openstack平台需要使用各种Linux发行版模板镜像,其制作方法主要有两种,要么是基于各大Linux发行版ISO光盘手动制作,要么是使用官方提供的模板镜像再做修改 之前制作的opensta ...
- Linux主机之间传输文件的几种方法对比
1.scp传输 scp -r /data/file root@ip:/data/ scp -C /data/sda.img root@ip:/data/img/#-r: 支持目录#-C: 启用压缩传送 ...
- Java并发(九)【转载】不可不说的Java“锁”事
转载自 美团技术团队,原文链接 不可不说的Java“锁”事 前言 Java提供了种类丰富的锁,每种锁因其特性的不同,在适当的场景下能够展现出非常高的效率.本文旨在对锁相关源码(本文中的源码来自JDK ...