假如F[1] = a, F[2] = B, F[n] = F[n - 1] + F[n - 2]。

写成矩阵表示形式可以很快发现F[n] = f[n - 1] * b + f[n - 2] * a。 f[n] 是斐波那契数列

也就是我们如果知道一段区间的前两个数增加了多少,可以很快计算出这段区间的第k个数增加了多少

通过简单的公式叠加也能求和

F[n]  = f[n - 1] * b + f[n - 2] * a

F[n - 1] = f[n - 2] * b + f[n - 3] * a

.....

F[3] = f[2] * b + f[1] * a

F[2] = 1 * b    +        0

F[1] =    0       +        a

令G[n] = 0 + 1 + f[2] + f[3] + .... + f[n - 1]

K[n]  = 1 + 0 + f[1] + f[2] + .... f[n - 2] ,那么F[n] = G[n] * b + K[n] * a

线段树结点维护a,b两个延迟标记,分别表示第一个数和第二个数增加了多少

注意在PushDown和update的时候还要通过F[n]  = f[n - 1] * b + f[n - 2] * a计算出右子节点的前两个数应该增加的值

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream> using namespace std;
typedef long long LL; const int maxn = 3e5 + 10;
const int mod = 1e9 + 9;
#define lson idx << 1, l, mid
#define rson idx << 1 | 1, mid + 1, r LL f[maxn], F[maxn], G[maxn];
void init() {
f[1] = G[1] = 1;
F[0] = 1;
F[1] = 1;
for(int i = 2; i < maxn; i++) {
f[i] = (f[i - 1] + f[i - 2]) % mod;
G[i] = (G[i - 1] + f[i]) % mod;
F[i] = (F[i - 1] + f[i - 1]) % mod;
}
// F[i] = 1 + 0 + f[1] + f[2] + f[3] + ....f[i - 1]
// G[i] = 0 + f[1] + f[2] + f[3] + ....f[i]
}
LL sum[maxn << 2];
LL a[maxn << 2], b[maxn << 2];
void PushUp(int idx) {
sum[idx] = (sum[idx << 1] + sum[idx << 1 | 1]) % mod;
}
void build(int idx, int l, int r) {
a[idx] = b[idx] = 0;
if(l == r) {
scanf("%I64d", &sum[idx]);
} else {
int mid = (r + l) >> 1;
build(lson);
build(rson);
PushUp(idx);
}
} void maintain(int idx, int l,int r, int v1, int v2) {
int len = r - l + 1;
a[idx] = (a[idx] + v1) % mod;
b[idx] = (b[idx] + v2) % mod;
sum[idx] = (sum[idx] + G[len - 1] * v2 % mod + F[len - 1] * v1 % mod) % mod;
}
void PushDown(int idx, int l, int r) {
if(a[idx] != 0 || b[idx] != 0) {
int mid = (r + l) >> 1;
maintain(lson, a[idx], b[idx]);
int len = mid - l + 1;
int v1 = (f[len] * b[idx] + f[len - 1] * a[idx]) % mod;
int v2 = (f[len + 1] * b[idx] + f[len] * a[idx]) % mod;
maintain(rson, v1, v2);
a[idx] = b[idx] = 0;
}
}
void update(int idx, int l, int r, int tl, int tr, int v1, int v2) {
if(tl <= l && tr >= r) {
maintain(idx, l, r, v1, v2);
} else {
PushDown(idx, l, r);
int mid = (r + l) >> 1;
if(tl <= mid) update(lson, tl, tr, v1, v2);
if(tr > mid) {
LL h1 = v1, h2 = v2;
if(tl <= mid) {
int len = mid - max(tl,l) + 1;
h1 = (f[len] * v2 + f[len - 1] * v1) % mod;
h2 = (f[len + 1] * v2 + f[len] * v1) % mod;
}
update(rson, tl, tr, h1, h2);
}
PushUp(idx);
}
}
LL query(int idx, int l, int r, int tl, int tr) {
if(tl <= l && tr >= r) return sum[idx];
else {
PushDown(idx, l, r);
int mid = (r + l) >> 1;
LL ans = 0;
if(tl <= mid) ans = (ans + query(lson, tl, tr)) % mod;
if(tr > mid) ans = (ans + query(rson, tl, tr)) % mod;
return ans;
}
}
int main() {
init();
int n, m;
scanf("%d%d", &n, &m);
build(1, 1, n);
for(int i = 1; i <= m; i++) {
int op, l, r;
scanf("%d%d%d", &op, &l, &r);
if(op == 1) update(1, 1, n, l, r, 1, 1);
else {
LL ans = query(1, 1, n, l, r);
printf("%I64d\n", ans);
}
}
return 0;
}

codeforces 446C DZY Loves Fibonacci Numbers 线段树的更多相关文章

  1. ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)

    Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...

  2. Codeforces 446C DZY Loves Fibonacci Numbers [线段树,数论]

    洛谷 Codeforces 思路 这题知道结论就是水题,不知道就是神仙题-- 斐波那契数有这样一个性质:\(f_{n+m}=f_{n+1}f_m+f_{n}f_{m-1}\). 至于怎么证明嘛-- 即 ...

  3. codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)

    In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...

  4. 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 ...

  5. Codeforces 446C —— DZY Loves Fibonacci Numbers(线段树)

    题目:DZY Loves Fibonacci Numbers 题意比較简单,不解释了. 尽管官方的题解也是用线段树,但还利用了二次剩余. 可是我没有想到二次剩余,然后写了个感觉非常复杂度的线段树,还是 ...

  6. codeforces 446C DZY Loves Fibonacci Numbers 数论+线段树成段更新

    DZY Loves Fibonacci Numbers Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d &a ...

  7. Codeforces 446C - DZY Loves Fibonacci Numbers(斐波那契数列+线段树)

    Codeforces 题目传送门 & 洛谷题目传送门 你可能会疑惑我为什么要写 *2400 的题的题解 首先一个很明显的想法是,看到斐波那契数列和 \(10^9+9\) 就想到通项公式,\(F ...

  8. 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]$ 其中 ...

  9. Codeforces446C DZY Loves Fibonacci Numbers(线段树 or 分块?)

    第一次看到段更斐波那契数列的,整个人都不会好了.事后看了题解才明白了一些. 首先利用二次剩余的知识,以及一些数列递推式子有下面的 至于怎么解出x^2==5(mod 10^9+9),我就不知道了,但是要 ...

随机推荐

  1. MVC中Control和View之间数据传递的方式

    1:ViewBag和ViewData 具体区别不做讨论,本处只演示ViewData的具体示例: Controler代码:ViewData["Employee"] = emp; Vi ...

  2. 6、SQL Server 数据查询

    一.使用SELECT检索数据 数据查询是SQL语言的中心内容,SELECT 语句的作用是让数据库服务器根据客户要求检索出所需要的信息资料,并按照规定的格式进行整理,返回给客户端. SELECT 语句的 ...

  3. Spark Streaming消费Kafka Direct方式数据零丢失实现

    使用场景 Spark Streaming实时消费kafka数据的时候,程序停止或者Kafka节点挂掉会导致数据丢失,Spark Streaming也没有设置CheckPoint(据说比较鸡肋,虽然可以 ...

  4. SpringMVC无法获取请求中的参数的问题的调查与解决(2)

    由于Request的getInputSteam()一旦获取一次后,就再也无法获取了 在实际项目中导致下面的问题: 1,多个拦截器,Filter都需要从InputStream中拿数据的情况无法处理: 2 ...

  5. ASP.NET Misconfiguration: Debug Information

    Abstract: Debugging messages help attackers learn about the system and plan a form of attack. Explan ...

  6. Eclipse取消设置项目默认空间

    分享一个小经验:       如果,在启动Eclipse时选中了Use this as the default and do not ask again 下次要启动时不会再次显示修改工作空间的选择, ...

  7. QML引擎的演进,第一部分

    原文链接:Lars Knoll – Evolution of the QML engine, part 1 QML作为一项技术对于Qt的成功变得越来越重要.它允许创建流畅的动画界面,与现今的市场预期相 ...

  8. 机器学习中的范数规则化之(一)L0、L1与L2范数

    L1正则会产生稀疏解,让很多无用的特征的系数变为0,只留下一些有用的特征 L2正则不让某些特征的系数变为0,即不产生稀疏解,只让他们接近于0.即L2正则倾向于让权重w变小.见第二篇的推导. 所以,样本 ...

  9. dma_ops

    kernel中的dma缓存区管理层操作统一实现在  struct dma_map_ops 中: dma缓存区分配函数的具体实现参考: http://www.aichengxu.com/view/599 ...

  10. mac系统,git上刚刚checkout出来的文件,一检查,发现已经被修改过了,怎么破???

    如下图中所示: 事实上,checkout之后什么都还没做,这些文件为何就被修改? 检查一下别的电脑上所存放的同一套源码,原来出问题的文件都是同名文件,只不过是有大小写区分而已!!! linux系统可以 ...