Description

看题戳我 给你一个序列,要求支持区间加斐波那契数列和区间求和。\(~n \leq 3 \times 10 ^ 5, ~fib_1 = fib_2 = 1~\).

Solution

​ 先来考虑一段斐波那契数列如何快速求和,根据性质有

\[\begin {align}
fib_n &= fib_{n - 1} + fib_{n - 2} \\
&= fib_ {n - 2} + fib_{n - 3} + fib_{n - 2} \\
&= fib_{n - 3} + fib_{n - 4} + fib_{n - 3} + fib_{n - 2} \\
&= \dots \\
&= fib_2 + \sum_{i = 1}^{n - 2} {fib_i}
\end {align}
\]

​ 可以发现这里有个\(~\sum_{i = 1} ^ {n - 2} {fib_i}\),转换一下就是\(~\sum_{i = 1} ^ {n}fib_i = fib_{n + 2} - fib_2\).而两个斐波那契数列对应项加起来之后还是一个类斐波那契数列,记为\(~S_i\),设这个类斐波那契数列的起始项\(S_1 = a, S_2 = b\),显然有\(~S_i = a \times fib_{i - 2} + b \times fib_{i - 1}\).那么对于一段类斐波那契数列的求和,我们只要记起始的两项和这段数列的长度即可。现在可以用简单的线段树区间加来维护了,\(~PushDown~\)操作有一点细节,注意要分开算区间的前两项。具体看代码。。

Code

#include<bits/stdc++.h>
#define For(i, j, k) for(int i = j; i <= k; ++i)
#define Forr(i, j, k) for(int i = j; i >= k; --i)
using namespace std; inline int read() {
int x = 0, p = 1; char c = getchar();
for(; !isdigit(c); c = getchar()) if(c == '-') p = -1;
for(; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x *= p;
} inline void File() {
freopen("cf446c.in", "r", stdin);
freopen("cf446c.out", "w", stdout);
} const int N = 3e5 + 10, mod = 1e9 + 9;
int n, m, fib[N]; inline int add(int a, int b) { return (a += b) >= mod ? a - mod : a; } namespace SGT {
#define lc (rt << 1)
#define rc (rt << 1 | 1)
#define mid (l + r >> 1)
#define lson lc, l, mid
#define rson rc, mid + 1, r int tr[N << 2], t1[N << 2], t2[N << 2]; inline void pushup(int rt) { tr[rt] = (tr[lc] + tr[rc]) % mod; } inline int S(int a, int b, int x) {
return x == 1 ? a : (x == 2 ? b : (1ll * a * fib[x - 2] + 1ll * b * fib[x - 1]) % mod);
} inline int sum(int a, int b, int x) {
return x == 1 ? a : (x == 2 ? add(a, b) : (S(a, b, x + 2) - b + mod) % mod);
} inline void pushdown(int rt, int l, int r) {
if (t1[rt]) {
t1[lc] = add(t1[lc], t1[rt]), t2[lc] = add(t2[lc], t2[rt]);
tr[lc] = add(tr[lc], sum(t1[rt], t2[rt], mid - l + 1));
int T1 = S(t1[rt], t2[rt], mid - l + 2), T2 = S(t1[rt], t2[rt], mid - l + 3);
t1[rc] = add(t1[rc], T1), t2[rc] = add(t2[rc], T2);
tr[rc] = add(tr[rc], sum(T1, T2, r - mid));
t1[rt] = t2[rt] = 0;
}
} inline void build(int rt, int l, int r) {
if (l == r) tr[rt] = read();
else build(lson), build(rson), pushup(rt);
} inline void update(int rt, int l, int r, int L, int R) {
if (L <= l && r <= R) {
tr[rt] = add(tr[rt], sum(fib[l - L + 1], fib[l - L + 2], r - l + 1));
t1[rt] = add(t1[rt], fib[l - L + 1]); t2[rt] = add(t2[rt], fib[l - L + 2]);
return ;
}
pushdown(rt, l, r);
if (L <= mid) update(lson, L, R);
if (R > mid) update(rson, L, R);
pushup(rt);
} inline int query(int rt, int l, int r, int L, int R) {
if (L <= l && r <= R) return tr[rt];
pushdown(rt, l, r); int res = 0;
if (L <= mid) res = add(res, query(lson, L, R));
if (R > mid) res = add(res, query(rson, L, R));
return pushup(rt), res;
} #undef lc
#undef rc
#undef mid
#undef lson
#undef rson
} int main() {
File();
n = read(), m = read();
fib[1] = fib[2] = 1;
For(i, 3, n + 5) fib[i] = (fib[i - 1] + fib[i - 2]) % mod; using namespace SGT;
build(1, 1, n);
while (m --) {
int opt = read(), l = read(), r = read();
opt == 1 ? update(1, 1, n, l, r), 1 : printf("%d\n", query(1, 1, n, l, r)), 1;
} return 0;
}

【CF446C】DZY Loves Fibonacci Numbers (线段树 + 斐波那契数列)的更多相关文章

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

  2. [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)

    [Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...

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

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

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

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

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

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

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

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

  8. 【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers

    我这种maintain写法好zz.考试时获得了40pts的RE好成绩 In mathematical terms, the sequence Fn of Fibonacci numbers is de ...

  9. cf446C DZY Loves Fibonacci Numbers

    C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...

随机推荐

  1. CentOS 7.2 yum安装LAMP环境

    https://www.linuxidc.com/Linux/2016-11/136766.htm 详见以上链接,用yum安装方便省事. 尤其注意,mysql数据要设置远程连接.

  2. java中的代码块是什么意思,怎么用

    代码块是一种常见的代码形式.他用大括号“{}”将多行代码封装在一起,形成一个独立的代码区,这就构成了代码块.代码块的格式如下:   方法/步骤     普通代码块:是最常见的代码块,在方法里用一对“{ ...

  3. Linux sed使用方法

    目录 sed处理流程 测试数据 sed命令格式 sed命令行格式 行定位 定位1行 定位区间行(多行) 定位某一行之外的行 定位有跨度的行 操作命令 -a (新增行) -i(插入行) -c(替代行) ...

  4. 【学习总结】Git学习-参考廖雪峰老师教程九-使用码云

    学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...

  5. 网站数据分析&初始来源

    数据分析:如何追踪访客初始来源_搜索学院_百度搜索资源平台 https://ziyuan.baidu.com/college/articleinfo?id=260 网站数据分析:如何追踪访客初始来源 ...

  6. Tomcat v7.0 java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

    十二月 , :: 下午 org.apache.coyote.http11.AbstractHttp11Processor process 信息: Error parsing HTTP request ...

  7. Linux Centos 迁移Mysql 数据位置

    Linux Centos 迁移Mysql 数据位置 由于业务量增加导致安装在系统盘(20G)磁盘空间被占满了, 现在进行数据库的迁移. Mysql 是通过 yum 安装的. Centos6.5Mysq ...

  8. mysql sql执行计划

    查看Mysql执行计划 使用navicat查看mysql执行计划: 打开profile分析工具: 查看是否生效:show variable like ‘%profil%’; 查看进程:show pro ...

  9. hadoop分布式系统架构详解

    hadoop 简单来说就是用 java写的分布式 ,处理大数据的框架,主要思想是 “分组合并” 思想. 分组:比如 有一个大型数据,那么他就会将这个数据按照算法分成多份,每份存储在 从属主机上,并且在 ...

  10. Java中 VO、 PO、DO、DTO、 BO、 QO、DAO、POJO的概念(转)

    PO(persistant object) 持久对象 在 o/r 映射的时候出现的概念,如果没有 o/r 映射,没有这个概念存在了.通常对应数据模型 ( 数据库 ), 本身还有部分业务逻辑的处理.可以 ...