參考: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 Round #FF/#255 D DZY Loves Modification --贪心+优先队列

    题意:给你一个矩阵,每次选某一行或者某一列,得到的价值为那一行或列的和,然后该行每个元素减去p.问连续取k次能得到的最大总价值为多少. 解法: 如果p=0,即永远不减数,那么最优肯定是取每行或每列那个 ...

  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. 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. [CodeForces - 447E] E - DZY Loves Fibonacci Numbers

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

  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 Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d &a ...

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

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

  8. 『题解』Codeforces446C DZY Loves Fibonacci Numbers

    更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description In mathematical terms, the sequence \( ...

  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. sql: sybase与oracle中insert into select和select into的用法

    1. sybase与oracle中insert into select和select into的用法 http://wjlvivid.iteye.com/blog/1921679 Sybase 一.首 ...

  2. HDU ACM 1081 To The Max-&gt;最大子矩阵

    分析:利用求最大子段和的思想进行求解. 1.首先累加s[i][j].表示第j列中i从第1行加到第i行的和. 2.对每一列的i1到i2行的和进行计算(0<i1<i2<=n),得出t[k ...

  3. 简化ui文件转换写法

    在命令行敲一串长的命令.枯燥麻烦. #coding:utf-8 import sys import os import subprocess if len(sys.argv) == 2: #节省输入, ...

  4. OCA读书笔记(2) - 安装Oracle软件

    Objectives: •Describe your role as a database administrator (DBA) and explain typical tasks and tool ...

  5. 3-04. 一元多项式的乘法与加法运算(20)(ZJU_PAT 结构体)

    题目链接:http://pat.zju.edu.cn/contests/ds/3-04 设计函数分别求两个一元多项式的乘积与和. 输入格式说明: 输入分2行.每行分别先给出多项式非零项的个数.再以指数 ...

  6. HDU 2098 分拆素数和(素数)

    HDU 2098 分拆素数和(素数) http://acm.hdu.edu.cn/showproblem.php?pid=2098 题意: 给你一个偶数,问你这个偶数有多少种方式能由两个不同的素数构成 ...

  7. MFC--自定义消息

    在windows程序中,消息是一个重要的概念,最常见的消息一般都是以WM_开头,WM就是window message,窗口消息的缩写,通过处理标准的windows消息,我们可以改变窗口的外观,如使用W ...

  8. Webbrowser加载Flash后方向键失效问题(用到了OLE接口,没有被处理就转发,够复杂的)

    原文:http://blog.csdn.net/dropme/article/details/6253528 窗体上放一个ApplicationEvent控件,OnMessage事件中这么写 uses ...

  9. Python基础 2----Python 基础语法

    1 模块导入原理 1 模块是包含函数,类,变量的独立的Python文件 2 导入系统的模块 比如我在家目录下创建一个m.py的文件,我们导入了许多的系统模块,比如time,string等等.这边就类似 ...

  10. vs2013 IntelliSense: &quot;const char *&quot; 类型的实參与 &quot;LPCWSTR&quot; 类型的形參不兼容

    错误例如以下: 解决方法: project字符集选择多字符