我这种maintain写法好zz。考试时获得了40pts的RE好成绩

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 n integers: a1, a2, ..., an. Moreover, there are mqueries, 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 Fi - l + 1 to each element ai, where l ≤ i ≤ r.
  2. 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.


题目分析

注意到形如Fib的数列$a_{n+2}=a_{n+1}+a_n$有相当好的性质。

  1. $a_n=F_{n−2}a_1+F_{n−1}a_2$
  2. $\sum_{i=1}^na_i=a_{n+2}−a_2$

第一条可以用数学归纳法证明;第二条就是将$2\sum_{i=1}^na_i$展开,得到$\sum_{i=1}^na_i+a_{n+2}-a_2$.

回到这一道题上,利用了这两条性质,那么对于每一个修改的区间只需要保留区间前两项增加的Fib值就可以记录下这个操作。所以现在就可以用线段树来维护这一系列询问了。

 #include<bits/stdc++.h>
#define MO 1000000009
typedef long long ll;
const int maxn = ; struct node
{
ll tag1,tag2,sum;
}f[maxn<<];
int n,m;
ll sum[maxn],fib[maxn]; int read()
{
char ch = getchar();
int num = , fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = -;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
return num*fl;
}
void maintain(int rt, int lens)
{
f[rt].tag1 %= MO, f[rt].tag2 %= MO;
f[rt].sum = f[rt<<].sum+f[rt<<|].sum;
f[rt].sum = (f[rt].sum+f[rt].tag1*fib[lens]+f[rt].tag2*fib[lens+]-f[rt].tag2)%MO;
}
void pushdown(int rt, int l, int r)
{
if (!f[rt].tag1&&!f[rt].tag2) return;
int mid = (l+r)>>, ls = rt<<, rs = rt<<|;
f[ls].tag1 += f[rt].tag1, f[ls].tag2 += f[rt].tag2;
f[rs].tag1 += f[rt].tag1*fib[mid-l]+f[rt].tag2*fib[mid-l+];
f[rs].tag2 += f[rt].tag1*fib[mid-l+]+f[rt].tag2*fib[mid-l+];
f[rt].tag1 = f[rt].tag2 = ;
maintain(ls, mid-l+);
maintain(rs, r-mid);
}
void modify(int rt, int L, int R, int l, int r)
{
if (L <= l&&r <= R){
f[rt].tag1 += fib[l-L+];
f[rt].tag2 += fib[l-L+];
maintain(rt, r-l+);
return;
}
int mid = (l+r)>>;
pushdown(rt, l, r);
if (L <= mid) modify(rt<<, L, R, l, mid);
if (R > mid) modify(rt<<|, L, R, mid+, r);
maintain(rt, r-l+);
}
ll query(int rt, int L, int R, int l, int r)
{
if (L <= l&&r <= R) return f[rt].sum;
pushdown(rt, l, r);
int mid = (l+r)>>;
ll ret = ;
if (L <= mid) ret += query(rt<<, L, R, l, mid);
if (R > mid) ret += query(rt<<|, L, R, mid+, r);
return ret%MO;
}
int main()
{
n = read(), m = read(), fib[] = fib[] = ;
for (int i=; i<=n; i++) sum[i] = (sum[i-]+read())%MO;
for (int i=; i<=; i++) fib[i] = (fib[i-]+fib[i-])%MO;
for (int i=; i<=m; i++)
{
int opt = read(), l = read(), r = read();
if (opt==) modify(, l, r, , n);
else printf("%lld\n",(query(, l, r, , n)+(sum[r]-sum[l-])%MO+MO)%MO);
}
return ;
}

END

【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers的更多相关文章

  1. cf446C DZY Loves Fibonacci Numbers

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

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

  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. ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)

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

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

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

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

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

  8. 「CF446C」 DZY Loves Fibonacci Numbers

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

  9. Codeforces446C - DZY Loves Fibonacci Numbers

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

随机推荐

  1. Mysql相关函数使用和总结(liet、right、substring、substring_index)

    一.字段截取 1.从左开始截取字符串 用法:left(str,length),即:leift(被截取字符串,截取长度) 列子:select left(‘www.baidu.com’,8) 结果:www ...

  2. jar包冲突问题

    这两天在启动一个新项目的时候,项目一直启动不了,报StackOverFlow; java.util.concurrent.ExecutionException: java.lang.StackOver ...

  3. Delphi调用C# 编写dll动态库

    Delphi调用C# 编写dll动态库 编写C#dll的方法都一样,首先在vs2005中创建一个“类库”项目WZPayDll, using System.Runtime.InteropServices ...

  4. Struts2拦截器再认识

    拦截器(Interceptor)是 Struts 2 的核心组成部分. Struts2 很多功能都是构建在拦截器基础之上的,例如文件的上传和下载.国际化.数据类型转换和数据校验等等. Struts2 ...

  5. Codeforces 161E(搜索)

    要点 标签是dp但搜索一发就能过了. 因为是对称矩阵所以试填一下就是一个外层都填满了,因此搜索的深度其实不超过5. 显然要预处理有哪些素数.在这个过程中可以顺便再处理出一个\(vector:re[le ...

  6. II play with GG(思维规律)

    时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K 64bit IO Format: %lld 题目描述 IG won the S champion ...

  7. 1e9个兵临城下(容斥原理)

    链接:https://ac.nowcoder.com/acm/contest/321/A 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  8. 解决Linux下SSH等终端乱码问题

    1.vi /etc/sysconfig/i18n Centos5.5原来内容是: //LANG="en_US.UTF-8" //SYSFONT="latarcyrheb- ...

  9. 右侧导航栏(动态添加数据到list)

    页面样式 <style> .scroll { position: fixed; right: 5%; top: 5em; background: #ccc; display: none; ...

  10. MariaDB 实现主从复制

    實驗目的: MariaDB為MySQL的一個分支,其完全開源.無版權之虞且操作上與 MySQL 一脈相承,實際應用中非常廣泛,軟件本身很小,安裝容易,使用簡單. 但其也有缺點,指令行方式操作,無原生G ...