CodeForces - 719E Sasha and Array

本文介绍了一个结合矩阵快速幂和线段树技术解决特定数组查询与更新问题的方法。该问题涉及对数组进行批量增加操作及求解指定区间内斐波那契数列之和的查询,并通过模运算确保数值大小可控。

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

传送门

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 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 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|1
const 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值