Sasha and Array (线段树+矩阵快速幂)

本文介绍了一种结合段式树和矩阵快速幂技术的高效算法,用于处理大规模数组上的复杂查询操作,如增加指定区间的数值以及计算斐波那契数列的特定项。通过构建段式树并利用矩阵快速幂进行优化,该算法能够在短时间内响应大量查询,展现出卓越的性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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) = 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 tpi, li, ri 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.

 

 

这题代码量有点大啊 O__O

#include<bits/stdc++.h>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1//人懒没办法^_^
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;//注意取模
const int maxn = 1e5 +5 ;
struct Mat
{
    ll x[2][2];
    void init()//每个矩阵都先定义为单位矩阵
    {
        x[0][0]=x[1][1]=1;
        x[1][0]=x[0][1]=0;
    }
    Mat operator * (const Mat& m2)const//重载*号为矩阵相乘
    {
        Mat m;
        m.x[0][0]=m.x[0][1]=m.x[1][0]=m.x[1][1]=0;
        for(int k=0;k<2;k++)
            for(int i=0;i<2;i++)
                for(int j=0;j<2;j++)
                    m.x[i][j]=(m.x[i][j]+x[i][k]*m2.x[k][j])%mod;
        return m;
    }
    Mat operator + (const Mat& m2)const//重载+号为矩阵相加
    {
        Mat m;
        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++)
                m.x[i][j]=(x[i][j]+m2.x[i][j])%mod;
        return m;
    }
};
Mat sum[maxn*5],lazy[maxn*5];//数组要开大啊!!
Mat pow(ll n)
{
    Mat m,ret;
    m.x[0][0]=m.x[0][1]=m.x[1][0]=1;
    m.x[1][1]=0;
    ret.init();
    while(n)
    {
        if(n&1) ret=ret*m;
        m=m*m;
        n>>=1;
    }
    return ret;
}
void build(int l,int r,int rt)
{
    sum[rt].init();
    lazy[rt].init();
    if(l==r)
    {
        ll x;
        cin>>x;
        sum[rt]=pow(x-1);
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void PushDown(int rt)//下放
{
    sum[rt<<1]=sum[rt<<1]*lazy[rt];
    sum[rt<<1|1]=sum[rt<<1|1]*lazy[rt];
    lazy[rt<<1]=lazy[rt<<1]*lazy[rt];
    lazy[rt<<1|1]=lazy[rt<<1|1]*lazy[rt];
    lazy[rt].init();
}

void update(int L,int R,Mat c,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        sum[rt]=sum[rt]*c;
        lazy[rt]=lazy[rt]*c;
        return;
    }
    PushDown(rt);//更新时也要下放
    int mid=(l+r)>>1;
    if(L<=mid)
        update(L,R,c,lson);
    if(R>mid)
        update(L,R,c,rson);
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

ll query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
    {
        return sum[rt].x[0][0];
    }
    PushDown(rt);
    int mid=(l+r)>>1;
    ll ret=0;
    if(L<=mid)
        ret=(ret+query(L,R,lson))%mod;//注意取模
    if(R>mid)
        ret=(ret+query(L,R,rson))%mod;
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    return ret;
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        build(1,n,1);
        int op,a,b;
        ll x;
        while(m--)
        {
            cin>>op>>a>>b;
            if(op==1)
            {
                cin>>x;
                Mat m=pow(x);
                update(a,b,m,1,n,1);
            }
            else
            {
                cout<<query(a,b,1,n,1)<<endl;
            }
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值