【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法

C. 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 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 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 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 integers tpiliri and may be xi (1 ≤ tpi ≤ 2, 1 ≤ li ≤ ri ≤ n,1 ≤ 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 4
1 1 2 1 1
2 1 5
1 2 4 2
2 2 4
2 1 5

output

5
7
9

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.

Solution

题目大意:维护一个序列,支持两种操作:

1.区间[l,r]的权值+x

2.询问区间[l,r]的函数和,即$\sum _{x=l}^{r}fib(x)$这里的函数即斐波那契函数

一般求斐波那契函数的方法可以考虑矩阵乘法,这里也是这样的。

我们不用线段树维护权值,我们用线段树维护矩阵$fib^{a[l]-1}$。

矩阵的合并是可以相加的。

然后就可以了。

这道题很卡常数,我平常的习惯,矩阵是从1~n,而我这里要是从1开始就TLE了..所以应该从0~n-1

Code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define LL long long
inline int read()
{
    int x=0,f=1; char ch=getchar();
    while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
    while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
    return x*f; 
}
#define P 1000000007
#define MAXN 100010
int N,M,a[MAXN];
struct MatrixNode{LL a[2][2]; MatrixNode() {memset(a,0,sizeof(a));}}fib;
MatrixNode operator + (MatrixNode x,MatrixNode y)
{
    MatrixNode re;
    for (int i=0; i<=1; i++)
        for (int j=0; j<=1; j++)
            re.a[i][j]=(x.a[i][j]+y.a[i][j])%P;
    return re;
}
MatrixNode operator * (MatrixNode x,MatrixNode y)
{
    MatrixNode re;
    for (int k=0; k<=1; k++)
        for (int i=0; i<=1; i++)
            if (x.a[i][k])
                for (int j=0; j<=1; j++)
                    if (y.a[k][j])
                        (re.a[i][j]+=(x.a[i][k]*y.a[k][j])%P)%=P;
    return re;
}
MatrixNode operator ^ (MatrixNode x,int y)
{
    MatrixNode re;
    for (int i=0; i<=1; i++) re.a[i][i]=1;
    for (int i=y; i; i>>=1,x=x*x) if (i&1) re=re*x;
    return re;
}
namespace SegmentTree
{
    struct SegmentTreeNode{int l,r; MatrixNode tag,sum;}tree[MAXN<<2];
    #define ls now<<1
    #define rs now<<1|1
    inline void Update(int now) {tree[now].sum=tree[ls].sum+tree[rs].sum;}
    inline void PushDown(int now)
    {
        if (tree[now].l==tree[now].r) return;
        MatrixNode D=tree[now].tag;
        tree[ls].sum=tree[ls].sum*D; tree[ls].tag=tree[ls].tag*D;
        tree[rs].sum=tree[rs].sum*D; tree[rs].tag=tree[rs].tag*D;
        memset(tree[now].tag.a,0,sizeof(tree[now].tag.a));
        for (int i=0; i<=1; i++) tree[now].tag.a[i][i]=1;
    }
    inline void BuildTree(int now,int l,int r)
    {
        tree[now].l=l; tree[now].r=r;
        for (int i=0; i<=1; i++) tree[now].tag.a[i][i]=1;
        if (l==r) {tree[now].sum=fib^(a[l]-1); return;}
        int mid=(l+r)>>1;
        BuildTree(ls,l,mid); BuildTree(rs,mid+1,r);
        Update(now);
    }
    inline void Modify(int now,int L,int R,MatrixNode D)
    {
        int l=tree[now].l,r=tree[now].r;
        PushDown(now);
        if (L<=l && R>=r) {tree[now].tag=tree[now].tag*D; tree[now].sum=tree[now].sum*D; return;}
        int mid=(l+r)>>1;
        if (L<=mid) Modify(ls,L,R,D);
        if (R>mid) Modify(rs,L,R,D);
        Update(now);
    }
    inline MatrixNode Query(int now,int L,int R)
    {
        int l=tree[now].l,r=tree[now].r;
        PushDown(now);
        if (L<=l && R>=r) return tree[now].sum;
        int mid=(l+r)>>1; MatrixNode re;
        if (L<=mid) re=re+Query(ls,L,R);
        if (R>mid) re=re+Query(rs,L,R);
        return re;
    }
}
int main()
{
    N=read(),M=read();
    for (int i=1; i<=N; i++) a[i]=read();
    fib.a[0][0]=1; fib.a[0][1]=1; fib.a[1][0]=1; fib.a[1][1]=0;
    SegmentTree::BuildTree(1,1,N);
    while (M--)
        {
            int opt=read(),x,y,z;
            switch (opt)
                {
                    case 1 : x=read(),y=read(),z=read(); SegmentTree::Modify(1,x,y,fib^z); break;
                    case 2 : x=read(),y=read(); printf("%I64d\n",SegmentTree::Query(1,x,y).a[0][0]%P); break;
                }
        }
    return 0;
}
posted @ 2016-10-02 15:55  DaD3zZ  阅读(384)  评论(0编辑  收藏  举报