线段树(待续)(区间每个数加上不同的斐波那契额数)Codeforces Round #FF (Div. 2)E

E. DZY Loves Fibonacci Numbers

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 m queries, 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.


有区间更新和区间查询,直觉上就往线段树靠,关键在于区间更新,因为每个点加的值不一样,所以为了能够区间更新,要提取出每个点更新的共同部分。

假如i-l+1=x,那么我们可以找到这样一个矩阵等式:


显然对于一个区间操作,l是一样的,所有左边的第一部分是一样,而右边肯定也可以直接求的,就是区间和。
所以我们把左边第一部分的逆矩阵放到右边。就变成了:


这样我们就可以知道一个区间需要增加多少了。。。。。。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

typedef long long ll;
#define rep(i,s,t) for(int i=s;i<t;i++)
#define red(i,s,t) for(int i=s-1;i>=t;i--)
#define ree(i,now) for(int i=head[now];i!=-1;i=edge[i].next)
#define clr(a,v) memset(a,v,sizeof a)
#define L t<<1
#define R t<<1|1
#define MID int mid=(l+r)>>1;
#define max(a,b) (a<b?b:a)
#define min(a,b) (a<b?a:b)
#define SQR(a) ((a)*(a))

inline int input(){
    int ret=0;bool isN=0;char c=getchar();
    while(c<'0' || c>'9'){
        if(c=='-') isN=1;
        c=getchar();
    }
    while(c>='0' && c<='9'){
        ret=ret*10+c-'0';
        c=getchar();
    }
    return isN?-ret:ret;
}

inline void output(ll x){
    if(x<0){
        putchar('-');x=-x;
    }
    int len=0,data[20];
    while(x){
        data[len++]=x%10;x/=10;
    }
    if(!len)    data[len++]=0;
    while(len--)
        putchar(data[len]+48);
    putchar('\n');
}


const int MAXN=300005;
const ll mod=1000000009;
struct Ma{
    ll a[2][2];
}D,E,Z;
inline Ma mul(Ma x,Ma y){
    Ma ans;
    rep(i,0,2) rep(j,0,2){
        ans.a[i][j]=0;
        rep(k,0,2){
            ans.a[i][j]+=x.a[i][k]*y.a[k][j]%mod;
        }
        ans.a[i][j]%=mod;
    }
    return ans;
}
inline void add(Ma &x,Ma y){
    rep(i,0,2) rep(j,0,2){
        x.a[i][j]=(x.a[i][j]+y.a[i][j])%mod;
    }
}

inline Ma Pow(Ma a,ll b){
    Ma d=E,t=a;
    while(b){
        if(b&1) d=mul(d,t);
        b>>=1;
        t=mul(t,t);
    }
    return d;
}
inline ll Pow(ll a,ll b){
    ll d=1,t=a%mod;//记得取模,否则逆元处要超出范围。
    while(b){
        if(b&1) d=d*t%mod;
        b>>=1;
        t=t*t%mod;
    }
    return d;
}
inline bool Inver(Ma a,Ma &ans){
    /*
     | a b |
     | c d |
     的逆矩阵
     (1/(ad-bc)) * | d -b |
                   |-c a  |  
    */
    /*
    0,0 0,1
    1,0 1,1
    */
    ll x=a.a[0][0]*a.a[1][1]-a.a[0][1]*a.a[1][0];
    if(x==0) return 0;
    if(x>0){
        x=Pow(x,mod-2);
        ans.a[0][0]=x*a.a[1][1]%mod;
        ans.a[1][1]=x*a.a[0][0]%mod;
        ans.a[0][1]=x*(-a.a[0][1])%mod;
        ans.a[1][0]=x*(-a.a[1][0])%mod;
    }
    else{
        x=Pow(-x,mod-2);
        ans.a[0][0]=x*(-a.a[1][1])%mod;
        ans.a[1][1]=x*(-a.a[0][0])%mod;
        ans.a[0][1]=x*a.a[0][1]%mod;
        ans.a[1][0]=x*a.a[1][0]%mod;
    }
    rep(i,0,2) rep(j,0,2) ans.a[i][j]=(ans.a[i][j]+mod)%mod;
    return 1;
}

ll F[MAXN],Fsum[MAXN];
inline void init(){
    rep(i,0,2) rep(j,0,2) {Z.a[i][j]=0,E.a[i][j]=(i==j),D.a[i][j]=1;}
    D.a[1][1]=0;
    F[0]=0;
    F[1]=F[2]=1;
    rep(i,3,MAXN){
        F[i]=(F[i-1]+F[i-2])%mod;
    }
    Fsum[0]=0;
    rep(i,1,MAXN){
        Fsum[i]=(Fsum[i-1]+F[i])%mod;
    }
}

int n,m,op,x,y;
int a[MAXN];
ll data[MAXN<<2];
Ma lazy[MAXN<<2];
int lch[MAXN<<2],rch[MAXN<<2];

inline ll get(int l,int r,Ma v){
    return ((Fsum[r]-Fsum[l-1]+mod)%mod*v.a[1][1]%mod+(Fsum[r+1]-Fsum[l]+mod)%mod*v.a[1][0]%mod)%mod;
}

inline void push_up(int t){
    data[t]=(data[L]+data[R])%mod;
}

inline bool cmp(Ma x,Ma y){
    rep(i,0,2) rep(j,0,2) if(x.a[i][j]!=y.a[i][j]) return 0;
    return 1;
}

inline void push_down(int t){
    if(cmp(lazy[t],Z)==0){
        add(lazy[L],lazy[t]);
        data[L]+=get(lch[L],rch[L],lazy[t]);
        data[L]%=mod;

        add(lazy[R],lazy[t]);
        data[R]+=get(lch[R],rch[R],lazy[t]);
        data[R]%=mod;

        lazy[t]=Z;
    }
}

inline void build(int t,int l,int r){
    lazy[t]=Z;data[t]=0;
    lch[t]=l;
    rch[t]=r;
    if(l==r){
        data[t]=a[l];
    }
    else{
        MID;
        build(L,l,mid);
        build(R,mid+1,r);
        push_up(t);
    }
}

inline void add(int t,int x,int y,Ma v){
    int l=lch[t],r=rch[t];
    if(l>=x && r<=y){
        add(lazy[t],v);
        data[t]+=get(l,r,v);
        data[t]%=mod;
    }
    else{
        push_down(t);
        MID;
        if(y<=mid) add(L,x,y,v);
        else if(x>mid) add(R,x,y,v);
        else{
            add(L,x,mid,v);
            add(R,mid+1,y,v);
        }
        push_up(t);
    }
}


inline ll query(int t,int x,int y){
    int l=lch[t],r=rch[t];
    if(l>=x && r<=y) return data[t];
    push_down(t);
    MID;
    if(y<=mid) return query(L,x,y);
    else if(x>mid) return query(R,x,y);
    else return (query(L,x,mid)+query(R,mid+1,y))%mod;
}

int main(){
    init();
    n=input(),m=input();
    rep(i,1,n+1) a[i]=input();
    build(1,1,n);
    rep(i,0,m){
        op=input(),x=input(),y=input();
        if(op==2) output(query(1,x,y));
        else{
            Ma ma=Pow(D,x-1),b;
            if(Inver(ma,b))
                add(1,x,y,b);
        }
    }
    return 0;
}




展开阅读全文

没有更多推荐了,返回首页