Portal

Description

给出一个\(n(n\leq3\times10^5)\)个数的序列,进行\(m(m\leq3\times10^5)\)次操作,操作有两种:

  • 给区间\([L,R]\)加上一个斐波那契数列,即\(\{a_L,a_{L+1},...,a_R\} \rightarrow \{a_L+F_1,a_{L+1}+F_2,...,a_R+F_{R-L+1}\}\)
  • 询问区间\([L,R]\)的和,对\(10^9+9\)取模。

    斐波那契数列:\(F_1=1,F_2=2\)且满足\(F_{n+2}=F_n+F_{n+1}\)。

Solution

容易知道,满足\(a_{n+2}=a_n+a_{n+1}\)的数列具有以下性质:

  • 若\(c_n=a_n+b_n\),则\(c_1=a_1+b_1,c_2=a_2+b_2,c_{n+2}=c_n+c_{n+1}\)。
  • 有通项公式\(a_n=F_{n-2}a_1+F_{n-1}a_2\)。
  • 有前缀和公式\(\sum_{i=1}^n a_i=a_{n+2}-a_2\)。

    下面依次进行证明。

    证明1

    \(c_{n+2}=a_{n+2}+b_{n+2}=(a_n+a_{n+1})+(b_n+b_{n+1})=(a_n+b_n)+(a_{n+1}+b_{n+1})=c_n+c_{n+1}\)

    证明2

    若当\(n\leq k\)时,\(a_n=F_{n-2}a_1+F_{n-1}a_2\),则

    \(a_{k+1}=a_{k-1}+a_k=(F_{k-3}a_1+F_{k-2}a_2)+(F_{k-2}a_1+F_{k-1}a_2)=F_{k-1}a_1+F_ka_2\)

    因为\(a_1=F_{-1}a_1+F_0\)(可以认为\(F_0=F_2-F_1=0,F_{-1}=F_1-F_0=1\)),\(a_2=F_0a_1+F_1a_2\)

    所以\(\forall n\in\mathbb{Z},a_n=F_{n-2}a_1+F_{n-1}a_2\)。

    证明3

    \(\begin{align} 2\Sigma_{i=1}^n a_i &= (a_1+a_2+...+a_n)+(a_1+a_2+...+a_n) \\ &=a_1+(a_1+a_2)+(a_2+a_3)+...+(a_{n-1}+a_n)+a_n \\ &=a_1+a_n+(a_3+a_4...+a_{n+1}) \\ &=(a_1+a_2+...+a_n)-a_2+a_n+a_{n+1} \\ &=\Sigma_{i=1}^n a_i+a_{n+2}-a_2 \\ \Sigma_{i=1}^n a_i &=a_{n+2}-a_2 \end{align}\)

    我们现在就可以用线段树维护这个序列了。线段树中的每个节点记录三个值\(f_1,f_2,sum\),表示该区间被加上了一个以\(f_1,f_2\)开头的数列,区间和为\(sum\)。

    通过性质1,我们知道\(f_1,f_2\)可以叠加;

    通过性质2,我们可以\(O(1)\)地将\(f_1,f_2\)下放;

    通过性质3,我们可以\(O(1)\)地更新\(sum\)。

    时间复杂度\(O(nlogn)\)。

Code

//DZY Loves Fibonacci Numbers
#include <cstdio>
inline char gc()
{
    static char now[1<<16],*S,*T;
    if(S==T) {T=(S=now)+fread(now,1,1<<16,stdin); if(S==T) return EOF;}
    return *S++;
}
inline int read()
{
    int x=0,f=1; char ch=gc();
    while(ch<'0'||'9'<ch) {if(ch=='-') f=-1; ch=gc();}
    while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
typedef long long lint;
int const N=3e5+10;
lint const P=1e9+9;
int n,m; lint F[N],a[N];
#define s sg[s0]
struct seg{lint f1,f2,sum;} sg[N*20];
void update(int s0,int len)
{
    s.f1%=P,s.f2%=P;
    s.sum=sg[s0<<1].sum+sg[s0<<1|1].sum+(s.f1*F[len]+s.f2*F[len+1]-s.f2),s.sum%=P;
}
void pushdown(int s0,int L0,int R0)
{
    if(s.f1==0&&s.f2==0) return;
    int mid=L0+R0>>1,Ls0=s0<<1,Rs0=Ls0|1;
    sg[Ls0].f1+=s.f1; sg[Rs0].f1+=s.f1*F[mid-L0]+s.f2*F[mid-L0+1];
    sg[Ls0].f2+=s.f2; sg[Rs0].f2+=s.f1*F[mid-L0+1]+s.f2*F[mid-L0+2];
    s.f1=s.f2=0;
    update(Ls0,mid-L0+1); update(Rs0,R0-mid);
}
void ins(int s0,int L0,int R0,int L,int R)
{
    if(L<=L0&&R0<=R) {s.f1+=F[L0-L+1],s.f2+=F[L0-L+2],update(s0,R0-L0+1); return;}
    pushdown(s0,L0,R0);
    int mid=L0+R0>>1;
    if(L<=mid) ins(s0<<1,L0,mid,L,R);
    if(mid<R) ins(s0<<1|1,mid+1,R0,L,R);
    update(s0,R0-L0+1);
}
lint query(int s0,int L0,int R0,int L,int R)
{
    if(L<=L0&&R0<=R) return s.sum;
    pushdown(s0,L0,R0);
    lint res=0; int mid=L0+R0>>1;
    if(L<=mid) res+=query(s0<<1,L0,mid,L,R);
    if(mid<R) res+=query(s0<<1|1,mid+1,R0,L,R);
    return res%P;
}
lint sumA[N];
int main()
{
    n=read(),m=read();
    F[1]=F[2]=1; for(int i=3;i<=n+1;i++) F[i]=(F[i-2]+F[i-1])%P;
    for(int i=1;i<=n;i++) a[i]=read(),sumA[i]=sumA[i-1]+a[i];
    int rt=1;
    for(int i=1;i<=m;i++)
    {
        int opt=read(),L=read(),R=read();
        if(opt==1) ins(rt,1,n,L,R);
        else printf("%lld\n",(query(rt,1,n,L,R)+sumA[R]-sumA[L-1])%P);
    }
    return 0;
}

P.S.

CF怎么是个题就要开long long!?

不知道为什么要求对\(10^9+9\)取模,而不是\(10^9+7\)。

Codeforces446C - DZY Loves Fibonacci Numbers的更多相关文章

  1. 『题解』Codeforces446C DZY Loves Fibonacci Numbers

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

  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. cf446C DZY Loves Fibonacci Numbers

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

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

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

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

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

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

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

  9. 【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers

    我这种maintain写法好zz.考试时获得了40pts的RE好成绩 In mathematical terms, the sequence Fn of Fibonacci numbers is de ...

随机推荐

  1. BFC(块级格式上下文)

    BFC的生成 满足下列css声明之一的元素便会生成BFC 根元素 float的值不为none overflow的值不为visible display的值为inline-block.table-cell ...

  2. Tomcat--startup.bat文件

    Tomcat--startup.bat文件 如何启动tomcat,如何关闭tomcat等常规操作,我们应该都很清楚了,但是实际中我们经常会遇到一些恶心的情景,比如说正在我们撸码撸的很高兴的时候,ecl ...

  3. 权限问题导致zabbix无法监控mysql

    说说一个困扰自已两天的问题. 首先是用常规的方法安装上了mysql数据库.做了主从. 在监控从库的时候,发现所有的监控数据库的监控项都获取不到key值 . zabbix server端也不报错.获取到 ...

  4. Jetson TX2刷机教程(原创)

    Jetson TX2刷机教程 一,硬件准备 1台host主机(linux系统,最好是ubuntu64位) 1台Jetson TX2的平台 二,软件包 JetPack(Jetson SDK) 下载地址: ...

  5. 小白的.Net Core 2.0 ConsoleApp入门(keng)指南(一)

    一.准备工作 准备工作很简单,甚至可以不用Visual Studio,一只.NET CORE和Runtime即可(你有考虑过世界第一IDE的感受吗) 下载:https://www.microsoft. ...

  6. Java字节码基础[转]

    原文链接:http://it.deepinmind.com/jvm/2014/05/24/mastering-java-bytecode.html Java是一门设计为运行于虚拟机之上的编程语言,因此 ...

  7. HDU 6181 Two Paths

    这是一道次短路的题 但是本题有两个坑 注意边权的范围,一定要在所有与距离有关的地方开 long long 本题所求的并不是次短路,而是与最短路不同的最短的路径,如果最短路不止一条,那么就输出最短路的长 ...

  8. 洛谷 [P2024] 食物链

    并查集 这是一道比较特殊的并查集,开一个三倍的数组, 1-n保存同类,n-n×2保存猎物,n2~n3保存天敌: #include <iostream> #include <cstdi ...

  9. BZOJ 3884: 上帝与集合的正确用法 [欧拉降幂]

    PoPoQQQ大爷太神了 只要用欧拉定理递归下去就好了.... 然而还是有些细节没考虑好: $(P,2) \neq 1$时分解$P=2^k*q$的形式,然后变成$2^k(2^{(2^{2^{...}} ...

  10. Windows Azure Platform Introduction (14) 申请海外的Windows Azure账户

    <Windows Azure Platform 系列文章目录> 本文的最后更新时间为:2017-12-27 本文介绍国内用户,注册和使用海外Azure账户. 前提: 1.需要一个有效的Wi ...