CodeForces

来源:互联网 发布:法国旅游 知乎 编辑:程序博客网 时间:2024/06/02 19:34

传送门

E. Sasha and Array
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types:

  1. 1 l r x — increase all integers on the segment from l to r by values x;
  2. 2 l r — find , where f(x) is the x-th Fibonacci number. As this number may be large, you only have to find it modulo 109 + 7.

In this problem we define Fibonacci numbers as follows: f(1) = 1f(2) = 1f(x) = f(x - 1) + f(x - 2) for all x > 2.

Sasha is a very talented boy and he managed to perform all queries in five seconds. Will you be able to write the program that performs as well as Sasha?

Input

The first line of the input contains two integers n and m (1 ≤ n ≤ 100 0001 ≤ m ≤ 100 000) — the number of elements in the array and the number of queries respectively.

The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Then follow m lines with queries descriptions. Each of them contains integers tpiliri and may be xi (1 ≤ tpi ≤ 21 ≤ li ≤ ri ≤ n1 ≤ xi ≤ 109). Here tpi = 1 corresponds to the queries of the first type and tpi corresponds to the queries of the second type.

It's guaranteed that the input will contains at least one query of the second type.

Output

For each query of the second type print the answer modulo 109 + 7.

Examples
input
5 41 1 2 1 12 1 51 2 4 22 2 42 1 5
output
579
Note

Initially, array a is equal to 11211.

The answer for the first query of the second type is f(1) + f(1) + f(2) + f(1) + f(1) = 1 + 1 + 1 + 1 + 1 = 5.

After the query 1 2 4 2 array a is equal to 13431.

The answer for the second query of the second type is f(3) + f(4) + f(3) = 2 + 3 + 2 = 7.

The answer for the third query of the second type is f(1) + f(3) + f(4) + f(3) + f(1) = 1 + 2 + 3 + 2 + 1 = 9.


题目大意:给定n个数字,m次操作,其中m次操作分为两种

1表示成段更新,把给定区间的数字全部加上x。

2表示查询,求sigma f(i),l<=i<=r,f是斐波那契数列,最后的结果对1e9+7取模

其实就是一个矩阵快速幂加线段树的成段更新

#include<stdio.h>#include<iostream>#include<string>#include<string.h>#include<algorithm>#include<set>#include<queue>#include<vector>#include<math.h>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int mod = 1e9 + 7;const int maxn = 100005;typedef long long ll;int n,m;struct Matrix{    ll mat[2][2];    void init1(){//0矩阵        mat[0][0] = mat[0][1] = mat[1][0] = mat[1][1] = 0;    }    void init2(){//单位矩阵        mat[0][0] = mat[1][1] = 1;        mat[0][1] = mat[1][0] = 0;    }    void init3(){        mat[0][0] = mat[0][1] = mat[1][0] = 1;        mat[1][1] = 0;    }    Matrix operator+(const Matrix& m2)const{        Matrix m;        for(int i=0;i<2;i++)            for(int j=0;j<2;j++)                m.mat[i][j]=(mat[i][j]+m2.mat[i][j])%mod;        return m;    }};Matrix multiply(Matrix x,Matrix y){    Matrix ans;    ans.init1();    for(int i = 0;i<2;i++){        for(int p=0;p<2;p++){            for(int j=0;j<2;j++){                ans.mat[i][j] = (ans.mat[i][j] + (x.mat[i][p]*y.mat[p][j])%mod)%mod;            }        }    }    return ans;}Matrix fast_pow(ll n){    Matrix E , ma;    ma.init3();    E.init2();    while(n){        if(n%2==1)            E = multiply(E,ma);        ma = multiply(ma,ma);        n>>=1;    }    return E;}Matrix sum[maxn<<2] , add[maxn<<2];void PushUp(int rt){     sum[rt] = sum[rt<<1] + sum[rt<<1|1];}void build(int l,int r,int rt) {    sum[rt].init2(); add[rt].init2();    if (l == r) {        ll x;        scanf("%lld",&x);        sum[rt] = fast_pow(x-1);        return ;    }    int m = (l + r) >> 1;    build(lson);    build(rson);    PushUp(rt);}void PushDown(int rt) {    sum[rt<<1] =multiply(sum[rt<<1] , add[rt]);    sum[rt<<1|1] =multiply(sum[rt<<1|1] , add[rt]);    add[rt<<1] =multiply(add[rt<<1] , add[rt]);    add[rt<<1|1] =multiply(add[rt<<1|1] , add[rt]);    add[rt].init2();}void update(int L,int R,Matrix c,int l,int r,int rt) {    if (L <= l && r <= R) {        add[rt] = multiply(add[rt] , c) ;        sum[rt] = multiply(sum[rt] , c) ;        return ;    }    PushDown(rt);    int m = (l + r) >> 1;    if (L <= m) update(L , R , c , lson);    if (m < R) update(L , R , c , rson);    PushUp(rt);}ll query(int L,int R,int l,int r,int rt) {    if (L <= l && r <= R) {        return sum[rt].mat[0][0];    }    PushDown(rt );    int m = (l + r) >> 1;    ll ret = 0;    if (L <= m) ret = (ret + query(L , R , lson))%mod;    if (m < R)  ret = (ret + query(L , R , rson))%mod;    return ret;}int main(){    while(~scanf("%d%d",&n, &m)){        build(1 , n , 1);        for(int i = 0 ; i < m ; i ++){            int sign , l , r , x;            scanf("%d" , &sign);            if(sign == 1){                scanf("%d%d%d",&l , &r , &x);                Matrix mm = fast_pow(x);                update(l , r , mm ,1 , n ,1);            }            else{                scanf("%d%d", &l , &r);                printf("%lld\n" , query(l , r , 1 , n , 1));            }        }    }    return 0;}