C. Sasha and Array

C. Sasha and Array

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 i=lrf(ai), 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(x1)+f(x2) 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 (1n100000, 1m100000) — the number of elements in the array and the number of queries respectively.

The next line contains n integers a1,a2,,an (1ai109).

Then follow m lines with queries descriptions. Each of them contains integers tpi, li, ri and may be xi (1tpi2, 1lirin, 1xi109). 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.

 

解题思路

  参考斐波那契前 n 项和,先回忆用矩阵乘法求出斐波那契第 n 项的做法。定义斐波那契的第 n 项为 fn,矩阵 Fn=[fn1fn],转移矩阵 A=[0111],则有 Fn×A=Fn+1,从而得到第 n+1fn+1。进一步有 Fn+1=Fn×A=Fn1×A2==F1×An,其中 F1=[01]。因此若要快速得到第 nfn,只需计算 F1×An1 即可,可以用快速幂在 O(logn) 内算出来。

  本题需要由于涉及到区间修改与区间查询,因此需要用线段树来维护区间关于 Fai 的和。具体来说如果线段树节点维护的区间是 [l,r],则维护每个 Fai 的和,即 F=i=lrFai,那么该区间 i=lrfai 的结果就是 F1,2

  再考虑修改操作,如果要对 ai 加上 x 来获得第 ai+x 项,相当于令 Fai 右乘 x1 次转移矩阵 A,即 Fai×Ax1。因此修改操作就相当于对区间 [l,r] 内的 Fai 都右乘上 Ax1,再求和。由于线段树维护的是矩阵和,矩阵乘法又具有分配律,因此只需令 FF×Ax1 即可。

  卡常提醒,一般来说矩阵乘法 C=A×B 的代码会这么写:

Matrix operator*(Matrix b) {
    Matrix c;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 2; k++) {
                c[i][j] = (c[i][j] + 1ll * a[i][k] * b[k][j]) % mod;
            }
        }
    }
    return c;
}

  但本题由于有大量的次幂运算和 pushup 操作,意味着矩阵乘法也会被多次执行,然后因为矩阵乘法中有大量取模运算而被卡常。优化的方法把最内层循环拆开直接计算,这样就可以减少一半的取模运算,然后就很玄学地过了:

Matrix operator*(Matrix b) {
    Matrix c;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            c[i][j] = ((LL)a[i][0] * b[0][j] + (LL)a[i][1] * b[1][j]) % mod;
        }
    }
    return c;
}

  AC 代码如下,时间复杂度为 O(nlogA+m(logn+logA))

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 1e5 + 10, mod = 1e9 + 7;

int a[N];
struct Matrix {
    array<array<int, 2>, 2> a;
    
    Matrix(array<array<int, 2>, 2> b = {0}) {
        a = b;
    }
    auto& operator[](int x) {
        return a[x];
    }
    Matrix operator+(Matrix b) {
        Matrix c;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                c[i][j] = (a[i][j] + b[i][j]) % mod;
            }
        }
        return c;
    }
    Matrix operator*(Matrix b) {
        Matrix c;
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                c[i][j] = ((LL)a[i][0] * b[0][j] + (LL)a[i][1] * b[1][j]) % mod;
            }
        }
        return c;
    }
};
struct Node {
    int l, r;
    LL add;
    Matrix f;
}tr[N * 4];

Matrix qmi(Matrix a, LL k) {
    Matrix ret({1, 0, 0, 1});
    while (k) {
        if (k & 1) ret = ret * a;
        a = a * a;
        k >>= 1;
    }
    return ret;
}

void build(int u, int l, int r) {
    tr[u] = {l, r};
    if (l == r) {
        tr[u].f = Matrix({0, 1}) * qmi(Matrix({0, 1, 1, 1}), a[l] - 1);
    }
    else {
        int mid = l + r >> 1;
        build(u << 1, l, mid);
        build(u << 1 | 1, mid + 1, r);
        tr[u].f = tr[u << 1].f + tr[u << 1 | 1].f;
    }
}

void pushdown(int u) {
    if (tr[u].add) {
        tr[u << 1].f = tr[u << 1].f * qmi(Matrix({0, 1, 1, 1}), tr[u].add);
        tr[u << 1].add += tr[u].add;
        tr[u << 1 | 1].f = tr[u << 1 | 1].f * qmi(Matrix({0, 1, 1, 1}), tr[u].add);
        tr[u << 1 | 1].add += tr[u].add;
        tr[u].add = 0;
    }
}

void modify(int u, int l, int r, int c) {
    if (tr[u].l >= l && tr[u].r <= r) {
        tr[u].f = tr[u].f * qmi(Matrix({0, 1, 1, 1}), c);
        tr[u].add += c;
    }
    else {
        pushdown(u);
        int mid = tr[u].l + tr[u].r >> 1;
        if (l <= mid) modify(u << 1, l, r, c);
        if (r >= mid + 1) modify(u << 1 | 1, l, r, c);
        tr[u].f = tr[u << 1].f + tr[u << 1 | 1].f;
    }
}

Matrix query(int u, int l, int r) {
    if (tr[u].l >=l && tr[u].r <= r) return tr[u].f;
    pushdown(u);
    int mid = tr[u].l + tr[u].r >> 1;
    Matrix ret;
    if (l <= mid) ret = query(u << 1, l, r);
    if (r >= mid + 1) ret = ret + query(u << 1 | 1, l, r);
    return ret;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    build(1, 1, n);
    while (m--) {
        int op, l, r, c;
        cin >> op >> l >> r;
        if (op == 1) {
            cin >> c;
            modify(1, l, r, c);
        }
        else {
            cout << query(1, l, r)[0][1] << '\n';
        }
    }
    
    return 0;
}

 

参考资料

  Codeforces Round #373 — Editorial:https://codeforces.com/blog/entry/47314

  CF718C 题解:https://www.luogu.com.cn/article/beooj8nq

posted @   onlyblues  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2022-02-27 数字重构
Web Analytics
点击右上角即可分享
微信分享提示