[CodeForces - 447E] E - DZY Loves Fibonacci Numbers
E DZY Loves Fibonacci Numbers
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
F1 = 1; F2 = 1; Fn = Fn - 1 + Fn - 2 (n > 2).
DZY loves Fibonacci numbers very much. Today DZY gives you an array consisting of nintegers: a1, a2, ..., an. Moreover, there are m queries, each query has one of the two types:
- Format of the query "1 l r". In reply to the query, you need to add Fi - l + 1 to each element ai, where l ≤ i ≤ r.
- Format of the query "2 l r". In reply to the query you should output the value of
modulo 1000000009 (109 + 9).
Help DZY reply to all the queries.
Input
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 300000). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — initial array a.
Then, m lines follow. A single line describes a single query in the format given in the statement. It is guaranteed that for each query inequality 1 ≤ l ≤ r ≤ n holds.
Output
For each query of the second type, print the value of the sum on a single line.
Example
4 41 2 3 41 1 42 1 41 2 42 1 3
1712
Note
After the first query, a = [2, 3, 5, 7].
For the second query, sum = 2 + 3 + 5 + 7 = 17.
After the third query, a = [2, 4, 6, 9].
For the fourth query, sum = 2 + 4 + 6 = 12.
题目的大意就是,定义了fib数列前两项F[1]=1,F[2]=1。给出整数n,m,再给出n个数和m项操作。
每个操作包含三个数op,l,r,若op=1,就在[l,r]的区间相应加上F[1,r-l+1](每个数对应着加),若op=2,就将[l,r]中所有数统计和并输出。
首先,我们要知道,fib数列有个特殊的性质_两个fib数列相加,还是fib数列,只是前两项的值变动了而已(从而导致后面的数变动).
那么,根据这个性质,就可以通过累计的方法实现_用线段树进行维护.
首先,我们需要知道一些性质:
设fib[1]=a,fib[2]=b,则fib[n]=fib[n-1]*b+fib[n-2]*a.(可推)
fib[1]+fib[2]+fib[3]+......+fib[n]=fib[n+2]-fib[2].(可推)
通过这两个性质,我们可以巧妙的记录线段树上每一个节点的前两个(fib[1],fib[2])的值,然后计算出这个节点上[L,R],所有数的和.
怎么来?
我们对于每一个线段树上的节点,都记录两个值,ta,tb,分别表示这个区间前两项的值,在更新时,
void upt(int now,int fir,int sec,int len){
T[now].ta=(T[now].ta+fir)%TT,T[now].tb=(T[now].tb+sec)%TT;
T[now].key=((]-(long long)sec)%TT;
}
其中前两句是将标记累计,最后以句就是顺带计算出这个节点的值.
同时,在pushdown函数中,也要对此进行更新.
void push_down(int now){
if (!T[now].ta&&!T[now].tb) return;
int L=T[now].L,R=T[now].R;
upt(now<<,T[now].ta,T[now].tb,mid-L+);
,a,b;
a=((]+(long long)T[now].tb*fib[len])%TT;
b=((])%TT;
upt(now<<|,a,b,R-mid);
T[now].ta=T[now].tb=;
}
其中,对于某个节点的子节点,有两段.一段[L,mid],一段[mid+1,R].
那么更新[L,mid]时,它的前缀和当前节点是一样的,所以不需改动信息,直接下传.
更新[mid+1,R]时,需要重新计算出这段区间前两项的值(这可以用上面说的性质算),然后才能下传.
最后下传完了记得把标记清空.
不过不知道为什么,我的线段树开4,5倍空间会炸,开10倍才不炸,很玄学......
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mid ((L+R)>>1)
using namespace std;
,maxn=;
int n,m,a[maxn],fib[maxn];
];
int read(){
; char ch=getchar();
') ch=getchar();
+ch-',ch=getchar();
return x;
}
void make(int now,int L,int R){
T[now].L=L,T[now].R=R;
if (L==R){T[now].key=a[L]; return;}
make(now<<,L,mid),make(now<<|,mid+,R);
T[now].key=(T[now<<].key+T[now<<|].key)%TT;
}
void upt(int now,int fir,int sec,int len){
T[now].ta=(T[now].ta+fir)%TT,T[now].tb=(T[now].tb+sec)%TT;
T[now].key=((]-(long long)sec)%TT;
}
void push_down(int now){
if (!T[now].ta&&!T[now].tb) return;
int L=T[now].L,R=T[now].R;
upt(now<<,T[now].ta,T[now].tb,mid-L+);
,a,b;
a=((]+(long long)T[now].tb*fib[len])%TT;
b=((])%TT;
upt(now<<|,a,b,R-mid);
T[now].ta=T[now].tb=;
}
void alter(int now,int aimL,int aimR){
int L=T[now].L,R=T[now].R;
if (L>aimR||R<aimL) return;
],fib[L-aimL+],R-L+); push_down(now); return;}
push_down(now);
alter(now<<,aimL,aimR),alter(now<<|,aimL,aimR);
T[now].key=(T[now<<].key+T[now<<|].key)%TT;
}
int seek(int now,int aimL,int aimR){
int L=T[now].L,R=T[now].R;
;
if (L>=aimL&&R<=aimR) return T[now].key;
push_down(now);
,aimL,aimR)+seek(now<<|,aimL,aimR))%TT;
}
int main(){
n=read(),m=read(),memset(T,,sizeof T);
; i<=n; i++) a[i]=read();
make(,,n);
fib[]=,fib[]=fib[]=;
; i<=n+; i++) fib[i]=(fib[i-]+fib[i-])%TT;
; i<=m; i++){
int tp=read(),L=read(),R=read();
) alter(,L,R);
,L,R));
}
;
}
[CodeForces - 447E] E - DZY Loves Fibonacci Numbers的更多相关文章
- 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 Round #FF 446 C. DZY Loves Fibonacci Numbers
參考:http://www.cnblogs.com/chanme/p/3843859.html 然后我看到在别人的AC的方法里还有这么一种神方法,他预先设定了一个阈值K,当当前的更新操作数j<K ...
- 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 数论+线段树成段更新
DZY Loves Fibonacci Numbers Time Limit:4000MS Memory Limit:262144KB 64bit IO Format:%I64d &a ...
- Codeforces 446C —— DZY Loves Fibonacci Numbers(线段树)
题目:DZY Loves Fibonacci Numbers 题意比較简单,不解释了. 尽管官方的题解也是用线段树,但还利用了二次剩余. 可是我没有想到二次剩余,然后写了个感觉非常复杂度的线段树,还是 ...
- 『题解』Codeforces446C DZY Loves Fibonacci Numbers
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description In mathematical terms, the sequence \( ...
- cf446C DZY Loves Fibonacci Numbers
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- Codeforces446C - DZY Loves Fibonacci Numbers
Portal Description 给出一个\(n(n\leq3\times10^5)\)个数的序列,进行\(m(m\leq3\times10^5)\)次操作,操作有两种: 给区间\([L,R]\) ...
随机推荐
- BOM - 浏览器API
1,javascript 组成部分: 1.ECMAscript(核心标准): 定义了基本的语法,比如:if for 数组 字符串 ... 2.BOM : 浏览器对象模型(Browser ...
- Node内核基本自带模块fs 文件的读写
在node的内核中存在一些内置的模块 这些是最基本的服务端所必要的 1:node全局环境:global类似于浏览器端的window 2:文件读取模块:fs fs模块同时提供了异步和同步的方法. 'us ...
- 【BZOJ】3143: [Hnoi2013]游走
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3143 显然如果一条边期望被走过的次数越多,我们就应该给它的编号越小. 所以问题变为如何求每 ...
- 配置多个版本的jdk
引言: 由于公司有多个项目都需要我开发,而各个项目所依赖的jdk版本又不同,因此需要配置多个jdk; 配置方法: 什么也不说了,直接上图: 然后在其他需要制定Tomcat的地方直接引用JAVA_HOM ...
- [osgearth][原]仿照谷歌,修改oe漫游器中focal(视角切换)功能
oe中的视角加速感觉好奇怪,就仿照谷歌方式去改了. 先看看oe原来的漫游器改变视角的接口: void CameraManipulator::setViewpoint(const Viewpoint&a ...
- tips 移入悬浮功能
前景: 页面部分区域需要移入悬浮效果,当然默认的 title 也是可以的,最多只是格格不入,但是却是最为靠谱的.. 思路: 基于 jq 实例扩展 .使用立即执行函数保持功能独立. 自定义类实现功能封装 ...
- 学习笔记16—Matlab 基础集
1.常用相关 [r, p] = corr(X,Y), [r, p] = partialcorr(X,Y, Z) , 其中Z是协变量. 2.TD_age = importdata('F:\BrainAg ...
- Qt的Radio Button(单选按钮)
1 在UI界面中加入控件 2 对QRadioButton控件进行分组 QRadioButton的分组有多重方法,如采用组合框.QWidge等,下面介绍采用QButtonGroup方法来实现分组,好处是 ...
- Unity---资源管理中不同资源的路径获取方式
1.首先需要先了解两个知识点: Unity内置的文件路径获取方式.windows的Directory.GetFiles文件获取方式: 1>Unity内置的文件路径获取方式,一下是官方解释:h ...
- jenkins之从0到1利用Git和Ant插件打war包并自动部署到tomcat(第三话):创建一个自由风格的项目(非maven),实现自动打war包
上一节把git和ant安装在虚拟机,并在jenkins上做了相关配置,接下来就可以真正开始构建一个项目了 1.新建一个自由风格的项目,因为是用ant打包,所以不要选择构建maven项目 2.配置源码管 ...