參考:http://www.cnblogs.com/chanme/p/3843859.html

然后我看到在别人的AC的方法里还有这么一种神方法,他预先设定了一个阈值K,当当前的更新操作数j<K的时候,它就用一个类似于树状数组段更的方法,用一个 d数组去存内容,譬如它要在区间 [3,6]上加一段fibonacci

原来:

id 0 1 2 3 4 5 6 7 8 9 10

d  0 0 0 0 0 0 0 0 0 0 0

更新:

id 0 1 2 3 4 5 6  7  8  9 10

d  0 0 0 1 0 0 0 -5  -3 0 0

我们能够发现,当利用 d[i]=d[i]+d[i-1]+d[i-2] i由小到大更新后就会得到

id 0 1 2 3 4 5 6  7  8  9 10

d  0 0 0 1 1 2 3  0  0  0  0

所以对于[L,R]上加一段操作,我们能够d[L]+=1; d[R+1]-=f[R-L+2],d[R+2]=f[R-L+1];

所以假如我更新了m次,我每次更新的复杂度是O(1),我把全部数求出来一次的复杂度是O(n)

然后他的算法是这种,j<K的时候更新的时候依照上述方法更新,询问的话则是将询问区间和更新的区间求交,把交的和加上去。

当j==K的时候,利用d还原出新的a,把当前的区间清0.

C. DZY Loves Fibonacci Numbers
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Sample test(s)
input
4 4
1 2 3 4
1 1 4
2 1 4
1 2 4
2 1 3

output
17
12

Note

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.

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>using namespace std;const int maxn=330000;
const long long int mod=1000000009LL;
typedef long long int LL;int n,m,k;
LL a[maxn],sum[maxn],fib[maxn],gib[maxn];
LL d[maxn]; int L[1100],R[1100];void init()
{fib[1]=1LL,fib[2]=1LL;gib[1]=1LL,gib[2]=2LL;for(int i=3;i<=n+50;i++){fib[i]=(fib[i-1]+fib[i-2])%mod;gib[i]=(gib[i-1]+fib[i])%mod;}
}int main()
{scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){scanf("%I64d",a+i);sum[i]=sum[i-1]+a[i];}init();while(m--){int c,l,r;scanf("%d%d%d",&c,&l,&r);if(c==1){L[k]=l,R[k]=r; k++;d[l]=(d[l]+1LL)%mod;d[r+1]=(d[r+1]-fib[r-l+2]+mod)%mod;d[r+2]=(d[r+2]-fib[r-l+1]+mod)%mod;if(k>=1000){for(int i=1;i<=n;i++){if(i>=2)d[i]=((d[i]+d[i-1])%mod+d[i-2])%mod;a[i]=(a[i]+d[i])%mod;sum[i]=sum[i-1]+a[i];}memset(d,0,sizeof(d));  k=0;}}else if(c==2){LL ret=0;for(int i=0;i<k;i++){int _left=max(l,L[i]);int _right=min(r,R[i]);if(_left>_right) continue;int s=_left-L[i]+1,e=_right-L[i]+1;ret=((ret+gib[e])%mod-gib[s-1]+mod)%mod;}ret=((ret+sum[r])%mod-sum[l-1]+mod)%mod;printf("%I64d\n",(ret+mod)%mod);}}return 0;
}

Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers相关推荐

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

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

  2. Codeforces 446C. DZY Loves Fibonacci Numbers【斐波那契+线段树】

    C. DZY Loves Fibonacci Numbers [题目描述] 传送门 [题解] 我们可以知道斐波那契数列有两个性质: ∑i=1nFi=Fn+2−F2\sum_{i=1}^{n} F_i= ...

  3. Codeforces 446C. DZY Loves Fibonacci Numbers (Fibonacci + 线段树)

    Description In mathematical terms, the sequence F n of Fibonacci numbers is defined by the recurrenc ...

  4. C. DZY Loves Fibonacci Numbers(线段树fibonacci)

    C. DZY Loves Fibonacci Numbers(线段树&fibonacci) 考虑fibonaccifibonaccifibonacci的几个性质: 两个的广义斐波那契数列和仍是 ...

  5. Codeforces446C - DZY Loves Fibonacci Numbers

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

  6. [CF446C]DZY Loves Fibonacci Numbers

    Description: 给出一个数列,每次可以选取一个区间,按顺序加上第i个Fibonacci Numbers(斐波那契数)进行更新,也可以查询某一个区间的总和. Hint: \(n \le 3*1 ...

  7. [Codeforces Round #254 div1] C.DZY Loves Colors 【线段树】

    题目链接:CF Round #254 div1 C 题目分析 这道题目是要实现区间赋值的操作,同时还要根据区间中原先的值修改区间上的属性权值. 如果直接使用普通的线段树区间赋值的方法,当一个节点表示的 ...

  8. Codeforces Round FF(Div. 2)

    layout: post title: Codeforces Round FF(Div. 2) author: "luowentaoaa" catalog: true tags: ...

  9. Codeforces Round #FF (Div. 2) A.DYZ Loves Hash

    水题.题目大意为输入一个p,n,再输入n组数据,每组数据对p取余,当和前面相同时,发现冲突,记下第几组,找到最后一组没有找到和前面一样的输出-1. *可以采用一个标记数组,就变得很简单了. A. DZ ...

最新文章

  1. 查看apache、linux、kernel、nginx等版本
  2. pytorch 索引
  3. recyclerView + GridLayoutManager 实现任意网格布局+拖拽排序
  4. 一文吃透PHP和HTML的嵌套写法
  5. Detection of Extraterrestrial KMP匹配 重复k次子串 好题
  6. Something about WinCE6.0 R3
  7. mysql timestamp add_「timestampdiff」MySQL中TIMESTAMPDIFF和TIMESTAMPADD函数的用法 - seo实验室...
  8. java 高并发im_java高并发(四)并发编程与线程安全
  9. 【数据科学系统学习】机器学习算法 # 西瓜书学习记录 [9] 决策树
  10. 推荐的Python电子书资源
  11. html文件上传协议,模拟HTML表单上传文件(RFC 1867)
  12. 《小石潭记》古文鉴赏
  13. error C2679: binary ‘<<‘ : no operator defined which takes a right-hand operand of type ‘class s
  14. [汇编] 汇编语言实现简易文本编辑器(光标移动、上卷和退格删除)
  15. 网易魔兽怀旧服服务器型号,魔兽世界所有怀旧服服务器名称已出,你觉得哪些名称更好听呢?...
  16. 省市县三级列表(jsp实现)
  17. 国内火狐浏览器不让使用有关广告插件解决方法
  18. Unity VideoPlayer播放切换视频卡上一帧问题
  19. 运动控制的轴到底是什么
  20. 富文本在TextView中显示图片

热门文章

  1. jdbc mysql查询显示图片_在实现JDBC时如何显示存储引擎-MySQL CONNECTION查询?
  2. Docker初学2:Docker的安装
  3. 随机森林 java_机器学习weka,java api调用随机森林及保存模型
  4. 05-自己创建mapmodel自定义迁移方式
  5. 【React Native】react-navigation导航使用方法
  6. iOS端Socket(二)ProtocolBuffer使用
  7. 用OpenGLES实现yuv420p视频播放界面
  8. TableStore: 海量结构化数据分层存储方案
  9. 云平台屡次停摆,核心系统事故频发?您的运维系统该升级了!
  10. the folder is already a source folder.