C. Sasha and Array
C. Sasha and Array
Sasha has an array of integers . You have to perform m queries. There might be queries of two types:
- — increase all integers on the segment from to by values ;
- — find , where is the -th Fibonacci number. As this number may be large, you only have to find it modulo .
In this problem we define Fibonacci numbers as follows: , , for all .
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 and (, ) — the number of elements in the array and the number of queries respectively.
The next line contains integers ().
Then follow lines with queries descriptions. Each of them contains integers , , and may be (, , ). Here corresponds to the queries of the first type and 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 .
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 is equal to , , , , .
The answer for the first query of the second type is .
After the query array a is equal to , , , , .
The answer for the second query of the second type is .
The answer for the third query of the second type is .
解题思路
参考斐波那契前 n 项和,先回忆用矩阵乘法求出斐波那契第 项的做法。定义斐波那契的第 项为 ,矩阵 ,转移矩阵 ,则有 ,从而得到第 项 。进一步有 ,其中 。因此若要快速得到第 项 ,只需计算 即可,可以用快速幂在 内算出来。
本题需要由于涉及到区间修改与区间查询,因此需要用线段树来维护区间关于 的和。具体来说如果线段树节点维护的区间是 ,则维护每个 的和,即 ,那么该区间 的结果就是 。
再考虑修改操作,如果要对 加上 来获得第 项,相当于令 右乘 次转移矩阵 ,即 。因此修改操作就相当于对区间 内的 都右乘上 ,再求和。由于线段树维护的是矩阵和,矩阵乘法又具有分配律,因此只需令 即可。
卡常提醒,一般来说矩阵乘法 的代码会这么写:
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 代码如下,时间复杂度为 :
#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
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/18038354
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
2022-02-27 数字重构