codeforces 719E (线段树 + 矩阵快速幂)

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) = 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

Copy

5 4
1 1 2 1 1
2 1 5
1 2 4 2
2 2 4
2 1 5

output

Copy

5
7
9

 

写了我5个半小时,心态崩了,后来发现少打了个lazy标记,

思路:这么大的斐波那契肯定不能递推存,一定要矩阵快速幂加速,线段树每个节点存一个矩阵,父节点就是两个子节点的矩阵相加,区间+x,等于把当前矩阵向下推x次,矩阵快速幂在log(x)时间内求出

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define lc rt << 1
#define rc rt << 1 | 1
typedef long long LL;
const LL mod = 1e9 + 7;
const int maxn = 2e5 + 50;

struct mat
{
    LL mm[2][2];
} unit;
mat b = {1, 1,
         1, 0};

mat tree[maxn << 2];
mat a[maxn];
mat lazy[maxn << 2];
LL mul(LL a, LL bb){
	a %= mod;
	bb %= mod;
	LL ans = 0;
	while(bb){
		if(bb & 1){
			ans = (ans + a) % mod;
		}
		bb >>= 1;
		a = (a + a) % mod;
	}
	return ans;
}
mat Mul(mat &A, mat &B){
    mat ret = {0};
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++){
            for(int k = 0; k < 2; k++){
            	ret.mm[i][j] += A.mm[i][k] * B.mm[k][j];
            	ret.mm[i][j] %= mod;
            }
        }
    }
    return ret;
}

mat add(mat &A, mat &B){
	mat ret = {0};
	for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++){
            ret.mm[i][j] = (A.mm[i][j] + B.mm[i][j]) % mod;
        }
    }
    return ret;
}
void init_unit(){
    for(int i = 0; i < 2; i++){
        unit.mm[i][i] = 1;
    }
    return ;
}
mat pow_mat(mat c, LL n){
    mat ret = unit;
    while(n){
        if(n & 1LL){
            ret = Mul(ret, c);
        }
        c = Mul(c, c);
        n >>= 1;
    }
    return ret;
}
void PushUp(int rt){
    tree[rt] = add(tree[lc], tree[rc]);
}
 
void PushDown(int rt){
	tree[lc] = Mul(tree[lc], lazy[rt]);
	tree[rc] = Mul(tree[rc], lazy[rt]);
	lazy[lc] = Mul(lazy[lc], lazy[rt]);
	lazy[rc] = Mul(lazy[rc], lazy[rt]);
	lazy[rt] = unit;
}
 
void Build(int le, int ri, int rt){
	lazy[rt] = unit;
    if(le == ri){
    	LL x;
        scanf("%I64d", &x);
        mat ret;
        if(x == 1){
        	ret = unit;
        } else if(x == 2){
        	ret = b;
        } else {
        	ret = pow_mat(b, x - 1);
        }
        tree[rt] = ret;
        lazy[rt] = unit;
    	return;
    }
    int m = (le + ri) >> 1;
    Build(le, m, lc);
    Build(m + 1, ri, rc);
    PushUp(rt);
}
 
int isequal(mat &A, mat &B){
	int flag = 1;
	for(int i = 0; i < 2; i++){
		for(int j = 0; j < 2; j++){
			if(A.mm[i][j] != B.mm[i][j]){
				flag = 0;
				break;
			}
		}
	}
	return flag;
}
void Update(int L, int R, mat &x, int le, int ri, int rt){
    if(L <= le && ri <= R){
        tree[rt] = Mul(tree[rt], x);
        lazy[rt] = Mul(lazy[rt], x);
        return ;
    }
    if(!isequal(lazy[rt], unit)){
        PushDown(rt);
    }
    int m = (le + ri) >> 1;
    if(L <= m){
        Update(L, R, x, le, m, lc);
    }
    if(R > m){
        Update(L, R, x, m + 1, ri, rc);
    }
    PushUp(rt);
}
 
LL Query(int L, int R, int le, int ri, int rt){
    if(L <= le && ri <= R){
        return tree[rt].mm[0][0];
    }
    if(!isequal(lazy[rt], unit)){
        PushDown(rt);
    }
    int m = (le + ri) >> 1;
    LL ans = 0;
    if(L <= m){
        ans += Query(L, R, le, m, lc);
        ans %= mod;
    }
    if(R > m){
        ans += Query(L, R, m + 1, ri, rc);
        ans %= mod;
    }
    return ans;
}
 
int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    init_unit();
    Build(1, n, 1);
    while(m--){
        int op, L, R;
        scanf("%d%d%d", &op, &L, &R);
        if(op == 1){
            LL x;
            scanf("%I64d", &x);
            mat ret = pow_mat(b, x);
            Update(L, R, ret, 1, n, 1);
        } else{
            LL ans = Query(L, R, 1, n, 1);
            printf("%I64d\n", ans % mod);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值