更好的阅读体验

Portal

Portal1: Codeforces

Portal2: Luogu

Description

In mathematical terms, the sequence \(F_n\) of Fibonacci numbers is defined by the recurrence relation

\[F_1 = 1; F_2 = 1; F_n = F_n - 1 + F_n - 2 (n > 2)
\]

DZY loves Fibonacci numbers very much. Today DZY gives you an array consisting of \(n\) integers: \(a1, a2, \cdots , an\). Moreover, there are \(m\) queries, each query has one of the two types:

  1. Format of the query "1 l r". In reply to the query, you need to add \(F_i - l + 1\) to each element ai, where \(l \le i \le r\).

  2. Format of the query "2 l r". In reply to the query you should output the value of modulo \(1000000009 (10^9 + 9)\).

Help DZY reply to all the queries.

Input

The first line of the input contains two integers \(n\) and \(m (1 \le n, m \le 300000)\). The second line contains \(n\) integers \(a_1, a_2, \cdots , a_n (1 \le ai \le 10^9)\) — 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 \le l \le r \le n\) holds.

Output

For each query of the second type, print the value of the sum on a single line.

Sample Input

4 4
1 2 3 4
1 1 4
2 1 4
1 2 4
2 1 3

Sample Output

17
12

Sample Explain

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\).

Description in Chinese

题目让我们求给你一个序列,支持区间加Fibonacci数列前r - l + 1项和查询区间和。

Solution

一些约定:把斐波那契数列的前两个数\(F_1 = 1, F_2 = 1\)换成另两个数,仍满足\(F_n = F_{n - 1} + F_{n - 2}(n > 2)\)的数列称为广义斐波那契数列。

Fibonacci数列有一些性质:

性质\(1\). \(F_n = (\sum^{n - 2}_{i = 1}{F_i}) + F_2(n > 2)\);

证明如下:

首先将前几项Fibonacci数列展开。

F(1) = 1
F(2) = 1
F(3) = F(1) + F(2)
F(4) = F(2) + F(3) = F(2) + F(1) + F(2)
F(5) = F(3) + F(4) = F(3) + F(2) + F(1) + F(2)
F(6) = F(4) + F(5) = F(4) + F(3) + F(2) + F(1) + F(2)
......

在\(F_n = F_{n - 1} + F_{n - 2}\)中,我们可以把\(F_{n - 1}\)按式子展开,可得\(F_n = \sum^{n - 3}_{i = 1} + F_2 + F_{n - 2}\),即\(F_n = (\sum^{n - 2}_{i = 1}{F_i}) + F_2(n > 2)\),跟原式一模一样,故原式正确性得证。

性质\(2\). 一个广义斐波那契数列数列\(f_i\), 当\(f_1 = x, f_2 = y\)时,则有\(f_n = x \times f_{n - 1} + y \times f_{n - 2}\)

证明如下:

这个性质与性质1类似,证明方法也与性质1类似,列举几个:

f(1) = x
f(2) = y
f(3) = f(1) + f(2) = x × F(1)
f(4) = f(2) + f(3) = x × F(1) + y × F(2)
f(5) = f(3) + f(4) = x × F(2) + y × F(3)
f(6) = f(4) + f(5) = x × F(3) + y × F(4)
......

把上述规律推广到代数式:

\[f_n = f_{n - 1} + f_{n - 2} \\\\ \quad = x \times f_{n - 2} + y \times f_{n - 3} + x \times f_{n - 3} + y \times f_{n - 4} \\\\ \quad = x \times (f_{n - 2} + f_{n - 3}) + y \times (f_{n - 3} + f_{n - 4}) \\\\ \quad = x \times f_{n - 1} + y \times f_{n - 2}
\]

证毕。

性质\(3\): 任意两段不同的广义斐波那契数列段相加(逐项相加),所得的数列任然是广义斐波那契数列。

这个性质易证。


这题我们维护一棵线段树,线段树需要维护\(L\)至\(R\)区间的广义斐波那契数列的第一项,第二项与区间的和。

下传标记时,我们可以在左区间加广义斐波那契数列的前两项,在右区间可以求出总和再加上总和就行了,时间复杂\(\text{O(n log n)}\)。

Code

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std; typedef long long LL;
const int MAXN = 300005, MAXM = 1200005, mod = 1e9 + 9;
struct node {
int c1, c2, sum;
} tree[MAXM];
int n, m, opt, x, y, a[MAXN], f[MAXN];
inline int add(int x, int y) {//两项相加并取模
int ret = x + y;
if (ret < 0) return ret += mod; else return ret % mod;
}
inline int calc1(int x, int y, int len) {//计算斐波那契
if (len == 1) return x; else
if (len == 2) return y; else return ((LL)x * f[len - 2] + (LL)y * f[len - 1]) % mod;
}
inline int calc2(int x, int y, int len) {//计算总和
if (len == 1) return x; else
if (len == 2) return add(x, y); else return add(calc1(x, y, len + 2), -y);
}
inline void pushup(int rt) {
tree[rt].sum = add(tree[rt << 1].sum, tree[rt << 1 | 1].sum);
}
inline void pushdown(int rt, int l, int r) {//下传标记
if (tree[rt].c1) {
int mid = l + r >> 1;
tree[rt << 1].c1 = add(tree[rt << 1].c1, tree[rt].c1);
tree[rt << 1].c2 = add(tree[rt << 1].c2, tree[rt].c2);
tree[rt << 1].sum = add(tree[rt << 1].sum, calc2(tree[rt].c1, tree[rt].c2, mid - l + 1));
int x = calc1(tree[rt].c1, tree[rt].c2, mid - l + 2), y = calc1(tree[rt].c1, tree[rt].c2, mid - l + 3);
tree[rt << 1 | 1].c1 = add(tree[rt << 1 | 1].c1, x);
tree[rt << 1 | 1].c2 = add(tree[rt << 1 | 1].c2, y);
tree[rt << 1 | 1].sum = add(tree[rt << 1 | 1].sum, calc2(x, y, r - mid));
tree[rt].c1 = 0; tree[rt].c2 = 0;
}
}
inline void update(int rt, int l, int r, int ansl, int ansr) {//线段树区间更新
if (ansl <= l && r <= ansr) {
tree[rt].c1 = add(tree[rt].c1, f[l - ansl + 1]);
tree[rt].c2 = add(tree[rt].c2, f[l - ansl + 2]);
tree[rt].sum = add(tree[rt].sum, calc2(f[l - ansl + 1], f[l - ansl + 2], r - l + 1));
return ;
}
pushdown(rt, l, r);
int mid = l + r >> 1;
if (ansl <= mid) update(rt << 1, l, mid, ansl, ansr);
if (ansr > mid) update(rt << 1 | 1, mid + 1, r, ansl, ansr);
pushup(rt);
}
inline int query(int rt, int l, int r, int ansl, int ansr) {//线段树区间查询
int ret = 0;
if (ansl <= l && r <= ansr) {
ret = tree[rt].sum;
return ret;
}
pushdown(rt, l, r);
int mid = l + r >> 1;
if (ansl <= mid) ret = add(ret, query(rt << 1, l, mid, ansl, ansr));
if (ansr > mid) ret = add(ret, query(rt << 1 | 1, mid + 1, r, ansl, ansr));
return ret;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
a[i] = add(a[i - 1], x);
}
f[1] = 1; f[2] = 1;
for (int i = 3; i <= n + 2; i++)
f[i] = add(f[i - 1], f[i - 2]);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &opt, &x, &y);
if (opt == 1) update(1, 1, n, x, y); else printf("%d\n", add(query(1, 1, n, x, y), a[y] - a[x - 1]));
}
return 0;
}

『题解』Codeforces446C DZY Loves Fibonacci Numbers的更多相关文章

  1. Codeforces446C - DZY Loves Fibonacci Numbers

    Portal Description 给出一个\(n(n\leq3\times10^5)\)个数的序列,进行\(m(m\leq3\times10^5)\)次操作,操作有两种: 给区间\([L,R]\) ...

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

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

  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. 「CF446C」 DZY Loves Fibonacci Numbers

    「CF446C」 DZY Loves Fibonacci Numbers 这里提供一种优美的根号分治做法. 首先,我们考虑一种不太一样的暴力.对于一个区间加斐波那契数的操作 \([a,b]\),以及一 ...

  7. cf446C DZY Loves Fibonacci Numbers

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

  8. Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers

    參考:http://www.cnblogs.com/chanme/p/3843859.html 然后我看到在别人的AC的方法里还有这么一种神方法,他预先设定了一个阈值K,当当前的更新操作数j<K ...

  9. [CodeForces - 447E] E - DZY Loves Fibonacci Numbers

    E  DZY Loves Fibonacci Numbers In mathematical terms, the sequence Fn of Fibonacci numbers is define ...

随机推荐

  1. MySQL 和 Navicat Premium 下载及安装全过程

    前言: 我对 “MySQL社区版” 的理解是:它只是一个后台服务,它的管理需要用到其他的数据库管理软件,这里我用的是 Navicat Premium,这个软件可以同时为多个数据库提供管理,比如MySQ ...

  2. JS中的排序算法-冒泡排序解析

    冒泡排序算法 例子:10,8,9,6,4,20,5  从小到大排序 第一轮  1)10>8  交换数据 得到:8,10,9,6,4,20,5 2)10>9  交换数据 得到:8,9,10, ...

  3. Springboot2.x + ShardingSphere 实现分库分表

    之前一篇文章中我们讲了基于Mysql8的读写分离(文末有链接),这次来说说分库分表的实现过程. 概念解析 垂直分片 按照业务拆分的方式称为垂直分片,又称为纵向拆分,它的核心理念是专库专用. 在拆分之前 ...

  4. Anrlr4 生成C++版本的语法解析器

    一. 写在前面 我最早是在2005年,首次在实际开发中实现语法解析器,当时调研了Yacc&Lex,觉得风格不是太好,关键当时yacc对多线程也支持的不太好,接着就又学习了Bison&F ...

  5. 解决:Specifying a namespace in include() without providing an app_name和XXX is not a registered namespace问题

    python3 Django 环境下,如果你遇到namespace没有注册以及在根目录下urls.py中的include方法的第二个参数namespace添加之后就出错的问题. 出错问题: 'Spec ...

  6. 算法学习之剑指offer(九)

    一 题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). public class Solution ...

  7. web安全之XSS基础-常见编码科普

    0x01常用编码 html实体编码(10进制与16进制): 如把尖括号编码[ < ]  -----> html十进制: <  html十六进制:< javascript的八进制 ...

  8. Android 使用appcompat_v7包以及源码注意事项

    最近学习和实践Android,无数次被AS和gradle惨痛的折磨,于是决定坚守Eclipse阵地..真是无奈之举,AS和gradle对我而言就像win10一样不讨喜. 当然Eclipse中开发and ...

  9. grunt前端自动构建工具初级使用

    好啦,今天来和大家谈谈grunt 害怕与社会脱轨,所以自己研究了一下,简单说说我梳理完的grunt 首先要知道为什么使用grunt 1.grunt可以检测js.css文件内部是否有错误 2. grun ...

  10. 关于在vue-cli脚手架中使用CDN引入element-ui不成功的坑

    在前端开发过程中,为了减少最后打包出来的体积,我们会用到cdn引入一些比较大的库来解决. 常见我们引入的element-ui库,在最近使用cdn引入时,无论如何都引入不成功,其他的如Vue.vue-r ...