C. Sasha and Array

C. Sasha and Array

Sasha has an array of integers $a_1, a_2, \ldots, a_n$. 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 $\sum_{i=l}^{r}{f(a_i)}$, where $f(x)$ is the $x$-th Fibonacci number. As this number may be large, you only have to find it modulo $10^9 + 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 \leq n \leq 100 000$, $1 \leq m \leq 100 000$) — the number of elements in the array and the number of queries respectively.

The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$).

Then follow $m$ lines with queries descriptions. Each of them contains integers $tp_i$, $l_i$, $r_i$ and may be $x_i$ ($1 \leq tp_i \leq 2$, $1 \leq l_i \leq r_i \leq n$, $1 \leq x_i \leq 10^9$). Here $tp_i = 1$ corresponds to the queries of the first type and $tp_i$ 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 $10^9 + 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$ 项为 $f_n$,矩阵 $F_n = \begin{bmatrix} f_{n-1} & f_{n} \end{bmatrix}$,转移矩阵 $A = \begin{bmatrix} 0 & 1 \\ 1 & 1 \end{bmatrix}$,则有 $F_n \times A = F_{n+1}$,从而得到第 $n+1$ 项 $f_{n+1}$。进一步有 $F_{n+1} = F_{n} \times A = F_{n-1}\times A^2 = \cdots = F_{1} \times A^n$,其中 $F_1 = \begin{bmatrix} 0 & 1 \end{bmatrix}$。因此若要快速得到第 $n$ 项 $f_n$,只需计算 $F_1 \times A^{n-1}$ 即可,可以用快速幂在 $O(\log{n})$ 内算出来。

  本题需要由于涉及到区间修改与区间查询,因此需要用线段树来维护区间关于 $F_{a_i}$ 的和。具体来说如果线段树节点维护的区间是 $[l,r]$,则维护每个 $F_{a_i}$ 的和,即 $F = \sum\limits_{i=l}^{r}{F_{a_i}}$,那么该区间 $\sum\limits_{i=l}^{r}{f_{a_i}}$ 的结果就是 $F_{1,2}$。

  再考虑修改操作,如果要对 $a_i$ 加上 $x$ 来获得第 $a_i + x$ 项,相当于令 $F_{a_i}$ 右乘 $x-1$ 次转移矩阵 $A$,即 $F_{a_i} \times A^{x-1}$。因此修改操作就相当于对区间 $[l,r]$ 内的 $F_{a_i}$ 都右乘上 $A^{x-1}$,再求和。由于线段树维护的是矩阵和,矩阵乘法又具有分配律,因此只需令 $F \gets F \times A^{x-1}$ 即可。

  卡常提醒,一般来说矩阵乘法 $C = A \times 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(n \log{A} + m(\log{n} + \log{A}))$:

#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 @ 2024-02-27 21:10  onlyblues  阅读(8)  评论(0编辑  收藏  举报
Web Analytics