Codeforces 446C —— DZY Loves Fibonacci Numbers(线段树)
题目:DZY Loves Fibonacci Numbers
题意比較简单,不解释了。
尽管官方的题解也是用线段树,但还利用了二次剩余。
可是我没有想到二次剩余,然后写了个感觉非常复杂度的线段树,还是mark一下吧。
我是这样考虑这个问题的,首先准备三个数组F,G,K,功能后面解释。
然后对它们有这样一个计算:
F[0] = G[0] = 0;
F[1] = 1; G[1] = 0;
K[0] = 1;
K[1] = 0;
for(int i=2; i<N; i++){
F[i] = (F[i-1]+F[i-2])%mod;
G[i] = (G[i-1]+F[i-1])%mod;
K[i] = (K[i-1]+K[i-2])%mod;
}
对于一个斐波那契数列,依据递推式我们能够知道即使我们要从中间開始。仅仅要知道连续两项(隔一项也能够。这里不考虑)。就能够顺利推出后面的值。
那么如果我们知道如今连续两项是v1,v2。那么从它们開始的第n项vn就是:K[n-1]*v1 + K[n]*v2(1)。
同一时候以v1,v2開始的连续n项的和是:F[n]*v1 + G[n]*v2(2)。
上面两个式子都是能够利用斐波那契数列本身的递推式推导得到的。
那么如今对于题目,如果我们要更新的是[L,R]区间,一開始传进去的是v1,v2,
假设如今须要改动子节点了,求出结点的中间值M。对于左边的区间我们知道v1和 v2照旧,关键是右边的须要又一次计算。
右边的第一项在原来的序列里面应该是属于第M+2-L项,记为x,所以能够利用上面(1)求出右边開始的两项。
当我们到达某个结点。它相应的区间和我们要改动的区间一样时,能够利用(2)直接将和加在原来的答案上面。
懒惰标记也非常好做。用f1和f2同一时候维护v1和v2的增量。
假如有两个序列1。1,2。3和2,3,5,8都加到了某个区间上去。
那么懒惰标记维护之后就是f1=1+2=3,,f2=1+3=4,利用递推能够算出后面两项就是7和11,这也符合原先两个序列的叠加。
完毕上面的工作,剩下的查询就是非经常规的求和了。
最后在CF上面跑了1122MS,还算能够吧。
#include<cstdio>
#include<cstring>
const int mod = 1000000009;
const int N = 300010;
typedef long long LL;
#define lson o<<1
#define rson (o<<1)|1
int a[N], l[N<<2], r[N<<2];
LL s[N<<2], f1[N<<2], f2[N<<2];
bool f[N<<2];
LL F[N], G[N], K[N];
inline void in(int &x){
char c=getchar();
x = 0;
while(c<48 || c>57) c=getchar();
while(c>=48 && c<=57){
x = x*10+c-48;
c = getchar();
}
}
void maintain(int o){
s[o] = (s[lson] + s[rson])%mod;
}
void build(int o, int ll, int rr){
l[o] = ll; r[o]=rr;
f[o] = 0;
if(ll<rr){
int m = (ll+rr)>>1;
build(lson, ll, m);
build(rson, m+1, rr);
maintain(o);
}
else{
s[o] = a[ll];
}
}
void update(int o, int ll, int rr, LL v1, LL v2);
void pushdown(int o){
if(f[o]){
int m = (l[o]+r[o])>>1;
update(lson, l[o], m, f1[o], f2[o]);
int x = m + 1 - l[o];
LL t1 = (K[x]*f1[o]%mod + K[x+1]*f2[o]%mod )%mod;
LL t2 = (K[x+1]*f1[o]%mod + K[x+2]*f2[o]%mod )%mod;
update(rson, m+1, r[o], t1, t2);
f[o] = 0;
}
}
void update(int o, int ll, int rr, LL v1, LL v2){
if(l[o]==ll && r[o]==rr){
int len = rr-ll+1;
s[o] = (s[o] + F[len]*v1%mod + G[len]*v2%mod)%mod;
if(!f[o]){
f1[o] = v1;
f2[o] = v2;
f[o] = 1;
}
else{
f1[o] = (f1[o] + v1)%mod;
f2[o] = (f2[o] + v2)%mod;
}
return;
}
pushdown(o);
int m = (l[o]+r[o])>>1;
if(rr<=m) update(lson, ll, rr, v1, v2);
else if(ll>m) update(rson, ll, rr, v1, v2);
else{
update(lson, ll, m, v1, v2);
int x = m + 1 - ll;
LL t1 = (K[x]*v1%mod + K[x+1]*v2%mod)%mod;
LL t2 = (K[x+1]*v1%mod + K[x+2]*v2%mod)%mod;
update(rson, m+1, rr, t1, t2);
}
maintain(o);
}
LL query(int o, int ll, int rr){
if(l[o]==ll && r[o]==rr) return s[o];
pushdown(o);
int m = (l[o]+r[o])>>1;
LL tmp=0;
if(rr<=m) tmp = query(lson, ll, rr);
else if(ll>m) tmp = query(rson, ll, rr);
else{
tmp = (query(lson, ll, m)+query(rson, m+1, rr))%mod;
}
if(tmp<0){
tmp = (tmp%mod + mod)%mod;
}
maintain(o);
return tmp;
}
int main(){
F[0] = G[0] = 0;
F[1] = 1; G[1] = 0;
K[0] = 1;
K[1] = 0;
for(int i=2; i<N; i++){
F[i] = (F[i-1]+F[i-2])%mod;
G[i] = (G[i-1]+F[i-1])%mod;
K[i] = (K[i-1]+K[i-2])%mod;
} int n, m;
in(n); in(m);
for(int i=1; i<=n; i++) in(a[i]);
build(1, 1, n);
int op, x, y;
while(m--){
in(op); in(x); in(y);
if(op&1){
update(1, x, y, 1, 1);
}
else{
printf("%I64d\n", query(1, x, y));
}
}
return 0;
}
Codeforces 446C —— 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 [线段树,数论]
洛谷 Codeforces 思路 这题知道结论就是水题,不知道就是神仙题-- 斐波那契数有这样一个性质:\(f_{n+m}=f_{n+1}f_m+f_{n}f_{m-1}\). 至于怎么证明嘛-- 即 ...
- 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(数学 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 Time Limit:4000MS Memory Limit:262144KB 64bit IO Format:%I64d &a ...
- Codeforces 446C - DZY Loves Fibonacci Numbers(斐波那契数列+线段树)
Codeforces 题目传送门 & 洛谷题目传送门 你可能会疑惑我为什么要写 *2400 的题的题解 首先一个很明显的想法是,看到斐波那契数列和 \(10^9+9\) 就想到通项公式,\(F ...
- 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]$ 其中 ...
- Codeforces446C DZY Loves Fibonacci Numbers(线段树 or 分块?)
第一次看到段更斐波那契数列的,整个人都不会好了.事后看了题解才明白了一些. 首先利用二次剩余的知识,以及一些数列递推式子有下面的 至于怎么解出x^2==5(mod 10^9+9),我就不知道了,但是要 ...
随机推荐
- (3)左右值再探与decltype
Decltype 类型指示符 “引用从来都作为其所指对象的同义词出现,只有用在decltype处是一个例外” 理解: Decltype和auto区别: 1. auto是从表达式类型推断出要定义 ...
- C#.NET,技巧篇(DataGridView线程操作)
这个系列的文章,主要是平时做C#.NET(Framework 3.5)开发的时候,积累的经验和技巧.我们平时总有这样的体会,遇到一个特别难解决的问题,网上寻它千百度也没能搜索到有用的信息.这时你肯定会 ...
- JVM 参数含义
JVM参数的含义 实例见实例分析 参数名称 含义 默认值 -Xms 初始堆大小 物理内存的1/64(<1GB) 默认(MinHeapFreeRatio参数可以调整)空余堆内存小于40%时,J ...
- 梦想Android版CAD控件2018.10.12更新
下载地址: http://www.mxdraw.com/ndetail_10106.html 1. 增加读写对象扩展字典功能 2. 修改样条线显示错误 3. 修改shx文字显示错误 4. 增加向量运算 ...
- pringboot开启找回Run Dashboard
代码中加入 <option name="configurationTypes"> <set> <option value="SpringBo ...
- canvas练手项目(三)——Canvas中的Text文本
Canvas中的Text文本也是一个知识点~,我们需要掌握一下几个基本的Text操作方法 首先是重要参数textAlign和textBaseline: textAlign left center ri ...
- Coin Toss(uva 10328,动态规划递推,限制条件,至少转至多,高精度)
有n张牌,求出至少有k张牌连续是正面的排列的种数.(1=<k<=n<=100) Toss is an important part of any event. When everyt ...
- 散列--P1047 校门外的树
题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,-,L,都种有 ...
- 移动端placeholder不能垂直居中解决方案
1.问题描述 问题如图:手机端placeholder文字偏上,垂直方向不居中,input光标显示偏上解决IE下不支持placeholder属性 2.解决方案 css .phoneNumber inpu ...
- web视频播放插件:Video For Everybody
相比其它的web视频播放插件(video.js , jwplayer等)来说,Video For Everybody(极力推荐)是一款更好的视频播放插件,无需任何下载,支持html5以及flash播放 ...