DZY Loves Fibonacci Numbers

Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status
Appoint description: 
System Crawler  (2014-07-14)

Description

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 arem queries, each query has one of the two types:

  1. Format of the query "1 lr". 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 lr". 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.

Sample Input

Input
4 4
1 2 3 4
1 1 4
2 1 4
1 2 4
2 1 3
Output
17
12

Hint

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.

解题思路:根据斐波那契数列的两个定义(任意区间适应,a为该区间的第一项,b为该区间的第二项):

(1)F(n)=b*fib[n-1]+a*fib[n-2] ;

(2)F[1]+F[2]+...+F[n]=F[n+2]-b;

 #include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; #define lson i<<1
#define rson i<<1|1
#define mid ((l+r)>>1)
typedef __int64 LL;
const int maxn=;
const int mod=1e9+;
int a[maxn],fib[maxn]; struct Node
{
int f1,f2;//区间第一项和第二项的值
int sum;
}segtree[maxn<<]; void init()//斐波那契数列预处理
{
fib[]=;fib[]=;
for(int i=;i<maxn;i++)
if((fib[i]=fib[i-]+fib[i-])>=mod)
fib[i]-=mod;
} int get_fib(int a,int b,int n)
{
if(n==) return a;
if(n==) return b;
return ((LL)b*fib[n-]+(LL)a*fib[n-])%mod;
} int get_sum(int a,int b,int n)
{
int sum=get_fib(a,b,n+)-b;
return (sum+mod)%mod;
}
void add_fib(int i,int l,int r,int a,int b)
{
segtree[i].f1=(segtree[i].f1+a)%mod;
segtree[i].f2=(segtree[i].f2+b)%mod;
segtree[i].sum=(segtree[i].sum+get_sum(a,b,r-l+))%mod;
} void pushdown(int i,int l,int r)
{
add_fib(lson,l,mid,segtree[i].f1,segtree[i].f2);
add_fib(rson,mid+,r,get_fib(segtree[i].f1,segtree[i].f2,mid+-l+)
,get_fib(segtree[i].f1,segtree[i].f2,mid-l+));
segtree[i].f1=segtree[i].f2=;
} void pushup(int i)
{
segtree[i].sum=(segtree[lson].sum+segtree[rson].sum)%mod;
} void build(int i,int l,int r)
{
if(l==r)
{
segtree[i].sum=a[l];
segtree[i].f1=segtree[i].f2=;
return ;
}
build(lson,l,mid);
build(rson,mid+,r);
pushup(i);
} void update(int i,int l,int r,int a,int b)
{
if(a<=l && r<=b)
{
add_fib(i,l,r,fib[l-a+],fib[l-a+]);
return ;
}
pushdown(i,l,r);
if(a<=mid) update(lson,l,mid,a,b);
if(b>mid) update(rson,mid+,r,a,b);
pushup(i);
} int query(int i,int l,int r,int a,int b)
{
if(a<=l && r<=b)
return segtree[i].sum;
pushdown(i,l,r);
int ans=;
if(a<=mid) ans=(ans+query(lson,l,mid,a,b))%mod;
if(mid<b) ans=(ans+query(rson,mid+,r,a,b))%mod;
return ans;
} int main()
{
init();
int i,n,m,op,l,r;
scanf("%d%d",&n,&m);
for(i=;i<=n;i++) scanf("%d",a+i);
build(,,n);
while(m--)
{
scanf("%d%d%d",&op,&l,&r);
if(op==) update(,,n,l,r);
if(op==) printf("%d\n",query(,,n,l,r));
}
return ;
}

codeforces 446C DZY Loves Fibonacci Numbers 数论+线段树成段更新的更多相关文章

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

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

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

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

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

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

  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 [线段树,数论]

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

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

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

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

  8. 【CF446C】DZY Loves Fibonacci Numbers (线段树 + 斐波那契数列)

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

  9. Codeforces Round #FF (Div. 2)__E. DZY Loves Fibonacci Numbers (CF447) 线段树

    http://codeforces.com/contest/447/problem/E 题意: 给定一个数组, m次操作, 1 l r 表示区间修改, 每次 a[i] +  Fibonacci[i-l ...

随机推荐

  1. vue跨域处理(vue项目中baseUrl设置问题)

    1.开发环境: 2.生产环境: 然后 const instance = axios.create({ baseURL: process.env.API })

  2. Bootstrap历练实例:默认的进度条

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  3. 微信iOS多设备多字体适配方案总结

    一.背景 2014下半年,微信iOS版先后适配iPad, iPhone6/6plus.随着这些大屏设备的登场,部分用户觉得微信的字体太小,但也有很多用户不喜欢太大的字体.为了满足不同用户的需求,我们做 ...

  4. NOIP模拟赛 经营与开发 小奇挖矿

    [题目描述] 4X概念体系,是指在PC战略游戏中一种相当普及和成熟的系统概念,得名自4个同样以“EX”为开头的英语单词. eXplore(探索) eXpand(拓张与发展) eXploit(经营与开发 ...

  5. 【莫队】bzoj4542: [Hnoi2016]大数

    挺有意思的,可以仔细体味一下的题:看白了就是莫队板子. Description 小 B 有一个很大的数 S,长度达到了 N 位:这个数可以看成是一个串,它可能有前导 0,例如00009312345.小 ...

  6. pandas交叉表和透视表及案例分析

    一.交叉表: 作用: 交叉表是一种用于计算分组频率的特殊透视图,对数据进行汇总 考察预测数据和正式数据的对比情况,一个作为行,一个作为列 案例: 医院预测病人病情: 真实病情如下数组(B:有病,M:没 ...

  7. 201621123080《Java程序设计》第9周学习总结

    作业09-集合与泛型 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1. List中指定元素的删除(题集题目) 1.1 实 ...

  8. vue 使用lib-flexable,px2rem 进行移动端适配 但是引入的第三方UI组件 vux 的样式缩小,解决方案

    最近在写移动端项目,就想用lib-flexable,px2rem来进行适配,把px转换成rem但是也用到了第三方UI组件库vux,把这个引入发现一个问题就是vux的组件都缩小了,在网上找不到答案,最后 ...

  9. ipvsadm分发MySQL读请求

    在MySQL的部署场景中,经常使用HAproxy和ipvs来作为读请求转发的网关.ipvs的好处在于本身不需要daemon的方式来运行,而是直接作为kernel的服务来提供:当ipvs和应用程序服务器 ...

  10. 如何解决js跨域问题

    Js跨域问题是web开发人员最常碰到的一个问题之一.所谓js跨域问题,是指在一个域下的页面中通过js访问另一个不同域下的数据对象,出于安全性考 虑,几乎所有浏览器都不允许这种跨域访问,这就导致在一些a ...