Codeforces Round #373 (Div. 2) E. Sasha and Array

来源:互联网 发布:linux 统计登录次数 编辑:程序博客网 时间:2024/05/01 05:56
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 performm queries. There might be queries of two types:

  1. 1 l r x — increase all integers on the segment froml to r by valuesx;
  2. 2 l r — find , wheref(x) is the x-th Fibonacci number. As this number may be large, you only have to find it modulo109 + 7.

In this problem we define Fibonacci numbers as follows: f(1) = 1, f(2) = 1, f(x) = f(x - 1) + f(x - 2) for allx > 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 andm (1 ≤ n ≤ 100 000,1 ≤ 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 integerstpi,li,ri and may bexi (1 ≤ tpi ≤ 2,1 ≤ li ≤ ri ≤ n,1 ≤ xi ≤ 109). Heretpi = 1 corresponds to the queries of the first type andtpi 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 1, 1, 2, 1, 1.

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 1, 3, 4, 3, 1.

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个数,两个操作,1是区间增加x,2是查询区间fib(a[i])的和

【解题方法】参考题解http://www.cnblogs.com/qscqesze/p/5902547.html

【AC 代码】

////Filename E. Sasha and Array//Created by just_sort 2016/9/27 19:12//Copyright (c) 2016 just_sort.All Rights Reserved//#include <set>#include <map>#include <queue>#include <stack>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;struct FastIO{    static const int S = 1310720;    int wpos;    char wbuf[S];    FastIO() : wpos(0) {}    inline int xchar()    {        static char buf[S];        static int len = 0, pos = 0;        if (pos == len)            pos = 0, len = fread(buf, 1, S, stdin);        if (pos == len) return -1;        return buf[pos ++];    }    inline int xuint()    {        int c = xchar(), x = 0;        while (c <= 32) c = xchar();        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';        return x;    }    inline int xint()    {        int s = 1, c = xchar(), x = 0;        while (c <= 32) c = xchar();        if (c == '-') s = -1, c = xchar();        for (; '0' <= c && c <= '9'; c = xchar()) x = x * 10 + c - '0';        return x * s;    }    inline void xstring(char *s)    {        int c = xchar();        while (c <= 32) c = xchar();        for (; c > 32; c = xchar()) * s++ = c;        *s = 0;    }    inline void wchar(int x)    {        if (wpos == S) fwrite(wbuf, 1, S, stdout), wpos = 0;        wbuf[wpos ++] = x;    }    inline void wchar(ll x)    {        if (x < 0) wchar('-'), x = -x;        char s[60];        int n = 0;        while (x || !n) s[n ++] = '0' + x % 10, x /= 10;        while (n--) wchar(s[n]);    }    inline void wint(int x)    {        if (x < 0) wchar('-'), x = -x;        char s[24];        int n = 0;        while (x || !n) s[n ++] = '0' + x % 10, x /= 10;        while (n--) wchar(s[n]);    }    inline void wstring(const char *s)    {        while (*s) wchar(*s++);    }    ~FastIO()    {        if (wpos) fwrite(wbuf, 1, wpos, stdout), wpos = 0;    }} io;const int mod = 1e9+7;const int maxn = 1e5+7;struct node{    ll a[2][2];    void init(){        memset(a,0,sizeof(a));    }    void one(){        init();        a[0][0] = a[1][1] = 1;    }};node add(node A,node B){    node C;    C.init();    for(int i = 0; i < 2; i++){        for(int j = 0; j < 2; j++){            C.a[i][j] = (A.a[i][j] + B.a[i][j]) % mod;        }    }    return C;}node mul(node A,node B){    node C;    C.init();    for(int i = 0; i < 2; i++){        for(int j = 0; j < 2; j++){            for(int k = 0; k < 2; k++){                C.a[i][j] = (C.a[i][j] + A.a[i][k]*B.a[k][j]) % mod;            }        }    }    return C;}node pow(int p){    node A;    A.a[0][0] = 0; A.a[0][1] = 1;    A.a[1][0] = 1; A.a[1][1] = 1;    node res;    res.one();    while(p){        if(p%2) res = mul(res,A);        A = mul(A,A);        p >>= 1;    }    return res;}int a[maxn];struct SegmentTree{    int l,r,flag;    node sum,lazy;    void update(node v)    {        sum = mul(sum,v);        lazy = mul(lazy,v);        flag = 1;    }}Tree[maxn<<2];void pushup(int rt){    Tree[rt].sum = add(Tree[rt*2].sum,Tree[rt*2+1].sum);}void pushdown(int rt){    if(Tree[rt].flag)    {        Tree[rt*2].update(Tree[rt].lazy);        Tree[rt*2+1].update(Tree[rt].lazy);        Tree[rt].flag = 0;        Tree[rt].lazy.one();    }}void Build(int l,int r,int rt){    Tree[rt].l = l,Tree[rt].r = r,Tree[rt].sum.init(),Tree[rt].lazy.one(),Tree[rt].flag = 0;    if(l == r){        Tree[rt].sum = pow(a[l]);        return ;    }    int mid = (l+r)/2;    Build(l,mid,rt*2);    Build(mid+1,r,rt*2+1);    pushup(rt);}void update(int L,int R,node v,int rt){    if(L <= Tree[rt].l && Tree[rt].r <= R){        Tree[rt].update(v);        return ;    }    else{        pushdown(rt);        int mid = (Tree[rt].l + Tree[rt].r)/2;        if(L <= mid) update(L,R,v,rt*2);        if(mid < R)  update(L,R,v,rt*2+1);        pushup(rt);    }}node queryans(int L,int R,int rt){    if(L <= Tree[rt].l && Tree[rt].r <= R){        return Tree[rt].sum;    }    else{        pushdown(rt);        int mid = (Tree[rt].l + Tree[rt].r)/2;        node res;        res.init();        if(L <= mid) res = add(res,queryans(L,R,rt*2));        if(mid < R)  res = add(res,queryans(L,R,rt*2+1));        pushup(rt);        return res;    }}int n,q;int main(){    n = io.xint();    q = io.xuint();    for(int i = 1; i <= n; i++) a[i] = io.xint();    Build(1,n,1);    for(int i = 1; i <= q; i++){        int op;        op = io.xint();        if(op == 2){            int l,r;            l = io.xint();            r = io.xint();            printf("%I64d\n",queryans(l,r,1).a[1][0]);        }        else{            int l,r,v;            l = io.xint();            r = io.xint();            v = io.xint();            update(l,r,pow(v),1);        }    }    return 0;}


0 0
原创粉丝点击