Description
Sasha has an array of integers \(a_1, a_2, ..., a_n\). You have to perform \(m\) queries. There might be queries of two types:
- 1 l r x — increase all integers on the segment from \(l\) to \(r\) by values \(x\);
- 2 l r — find\(\sum_{i=l}^rf(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 ≤ 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 \(a_1, a_2, ..., a_n (1 ≤ a_i ≤ 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 ≤ tp_i ≤ 2, 1 ≤l_i ≤ r_i ≤n, 1 ≤ x_i ≤ 109)\). 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\).
题意
给定数列\(a_1, a_2, ..., a_n\),\(m\)次询问,支持两种操作:
1.给区间\([l,r]\)加\(x\)
2.求\(\sum_{i=1}^rf(a_i) mod(10^9+7)\),其中\(f(x)\)满足
\(f(1) = f(2) = 1,\forall x > 2,f(x) = f(x - 1) + f(x - 2)\)(就是斐波那契数列)
\((1≤n,m≤10^5,1≤a_i,x≤10^9)\)
思路
鸽了,反正没人看,易得递推式子,把\(lazy\)变成一个矩阵
复杂度\(O(mlogn*2^3logx)\)
AC代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int,int>
#define endl '\n'
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
const double pi = acos(-1.0);
typedef long long ll;
int t, n, m, l, r;
struct Matrix
{
int a[5][5];
void init()
{
a[1][1] = a[2][2] = 1;
a[1][2] = a[2][1] = 0;
}
Matrix operator * (const Matrix& Ma_) const//重载运算符
{
Matrix res;
memset(res.a, 0, sizeof(res.a));
for (int i = 1; i <= 2; i++)
{
for (int j = 1; j <= 2; j++)
{
for (int k = 1; k <= 2; k++)
{
res.a[i][j] = (res.a[i][j] + a[i][k] * Ma_.a[k][j]) % mod;
}
}
}
return res;
}
Matrix operator + (const Matrix& Ma_) const
{
Matrix res;
memset(res.a, 0, sizeof(res.a));
for (int i = 1; i <= 2; i++)
{
for (int j = 1; j <= 2; j++)
{
res.a[i][j] = (a[i][j] + Ma_.a[i][j]) % mod;
}
}
return res;
}
};
Matrix sum[N << 2], lazy[N << 2];
Matrix quickpow(int base)//矩阵快速幂板子
{
Matrix res, trans;
res.init();
trans.a[1][1] = 1;
trans.a[1][2] = 1;
trans.a[2][1] = 1;
trans.a[2][2] = 0;
while (base > 0)
{
if (base & 1) res = res * trans;
trans = trans * trans;
base >>= 1;
}
return res;
}
void pushup(int rt)
{
sum[rt] = sum[rt * 2] + sum[rt * 2 + 1];
return;
}
void pushdown(int rt)
{
sum[rt * 2] = sum[rt * 2] * lazy[rt];
sum[rt * 2 + 1] = sum[rt * 2 + 1] * lazy[rt];
lazy[rt * 2] = lazy[rt * 2] * lazy[rt];
lazy[rt * 2 + 1] = lazy[rt * 2 + 1] * lazy[rt];
lazy[rt].init();
return;
}
void build(int l, int r, int rt)
{
sum[rt].init();
lazy[rt].init();
if (l == r)
{
int x;
cin >> x;
sum[rt] = quickpow(x - 1);
return;
}
int mid = (l + r) / 2;
build(l, mid, rt * 2);
build(mid + 1, r, rt * 2 + 1);
pushup(rt);
return;
}
void update(int L, int R, int l, int r, int rt, Matrix b)
{
if (L <= l && R >= r)
{
sum[rt] = sum[rt] * b;
lazy[rt] = lazy[rt] * b;
return;
}
pushdown(rt);
int mid = (l + r) / 2;
if (L <= mid) update(L, R, l, mid, rt * 2, b);
if (R >= mid + 1) update(L, R, mid + 1, r, rt * 2 + 1, b);
pushup(rt);
return;
}
int query(int L, int R, int l, int r, int rt)
{
if (L <= l && R >= r)
return sum[rt].a[1][1];
pushdown(rt);
int mid = (l + r) / 2;
int ans = 0;
if (L <= mid) ans = (ans + query(L, R, l, mid, rt * 2)) % mod;
if (R >= mid + 1) ans = (ans + query(L, R, mid + 1, r, rt * 2 + 1)) % mod;
return ans;
}
void solve() {
build(1, n, 1);
for (int i = 1; i <= m; i++)
{
int op, L, R;
cin >> op >> L >> R;
if (op == 1)
{
int x;
cin >> x;
Matrix temp = quickpow(x);
update(L, R, 1, n, 1, temp);
}
else
{
cout << query(L, R, 1, n, 1) << endl;
}
}
return;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
while (cin >> n >> m) {
solve();
}
return 0;
}