有两个操作:

  • 将 $[l,r]$所有数 + $x$
  • 求 $\sum_{i=l}^{r}fib(i)$
$n=m=10^5$  
直接求不好求,改成矩阵乘法的形式: 
$a_{i}=M^x\times fib_{1}$
直接用线段树维护 $M^x$ 即可.
因为矩阵乘法是满足结合律的: $A*B+A*C=A*(B+C)$
#include <cstdio>
#include <algorithm>
#include <cstring>
#define lson (now << 1)
#define rson (now << 1 | 1)
#define ll long long
#define setIO(s) freopen(s".in", "r" , stdin)
using namespace std;
const int N = 200003;
const ll mod = 1000000007;
struct Matrix
{
int n, m;
ll a[4][4];
ll * operator[] (int x) { return a[x]; }
inline void re()
{
for(int i = 0; i < 3 ; ++i)
for(int j = 0; j < 3; ++j) a[i][j] = 0;
}
inline void I()
{
re();
for(int i = 0; i < 3 ; ++i) a[i][i] = 1ll;
}
Matrix friend operator * (Matrix a, Matrix b)
{
Matrix c;
c.re();
int i , j , k;
for(i = 0; i < a.n ; ++i)
{
for(j = 0; j < b.m ; ++j)
for(k = 0; k < a.m ; ++k)
{
c[i][j] = (c[i][j] + (a[i][k] * b[k][j]) % mod) % mod;
}
}
c.n = a.n , c.m = b.m;
return c;
}
Matrix friend operator + (Matrix a, Matrix b)
{
Matrix c;
c.n = 2, c.m = 1;
for(int i = 0; i < c.n ; ++i)
{
for(int j = 0; j < c.m; ++j) c[i][j] = (a[i][j] + b[i][j]) % mod;
}
return c;
}
}A[N], M, fib1, v;
Matrix operator ^ (Matrix a, int k)
{
Matrix tmp;
tmp.n = tmp.m = 2;
for(tmp.I(); k ; a = a * a, k >>= 1) if(k & 1) tmp = tmp * a;
return tmp;
}
inline void init()
{
M.re(), fib1.re();
M.n = M.m = 2;
M[0][0] = 0, M[0][1] = 1, M[1][0] = 1, M[1][1] = 1;
fib1.n = 2, fib1.m = 1;
fib1[0][0] = 0, fib1[1][0] = 1;
}
int n , m ;
struct Node
{
Matrix sum, lazy;
int tag;
}t[N << 2];
inline void pushup(int l, int r, int now)
{
int mid = (l + r) >> 1;
t[now].sum = t[now << 1].sum;
if(r > mid) t[now].sum = t[now].sum + t[now << 1 | 1].sum;
}
inline void mark(int now, Matrix f)
{
t[now].sum = f * t[now].sum ;
t[now].lazy = t[now].lazy * f;
t[now].tag = 1;
}
inline void pushdown(int l, int r, int now)
{
int mid = (l + r) >> 1;
if(t[now].tag)
{
mark(lson, t[now].lazy);
if(r > mid) mark(rson, t[now].lazy);
t[now].lazy.I(), t[now].tag = 0;
}
}
void build(int l, int r, int now)
{
t[now].lazy.n = t[now].lazy.m = 2;
t[now].lazy.I();
t[now].tag = 0;
if(l == r)
{
t[now].sum = A[l];
return ;
}
int mid = (l + r) >> 1;
if(mid >= l) build(l, mid, lson);
if(r > mid) build(mid + 1, r, rson);
pushup(l, r, now);
}
void update(int l, int r, int now, int L, int R)
{
if(l >= L && r <= R)
{
mark(now , v);
return ;
}
pushdown(l, r, now);
int mid = (l + r) >> 1;
if(L <= mid) update(l, mid, lson, L, R);
if(R > mid) update(mid + 1, r, rson, L, R);
pushup(l, r, now);
}
ll query(int l, int r, int now, int L, int R)
{
if(l >= L && r <= R) return t[now].sum[1][0];
pushdown(l, r, now);
int mid = (l + r) >> 1;
ll g = 0;
if(L <= mid) g += query(l, mid, lson, L, R);
if(R > mid) g += query(mid + 1, r, rson, L, R);
return (g % mod);
}
int main()
{
// setIO("input");
init();
int i , j, x;
scanf("%d%d", &n, &m);
for(i = 1; i <= n ; ++i)
{
scanf("%d", &x), A[i] = (M ^ (x - 1)) * fib1;
}
build(1, n, 1);
for(int cas = 1, op , l, r, x; cas <= m ; ++cas)
{
scanf("%d", &op);
if(op == 1)
{
scanf("%d%d%d", &l, &r, &x);
if(x == 0) continue;
v = M ^ x;
update(1, n, 1, l, r);
}
if(op == 2)
{
scanf("%d%d", &l, &r);
printf("%lld\n", query(1, n, 1, l, r));
}
}
return 0;
}

  

CF718C Sasha and Array 线段树 + 矩阵乘法的更多相关文章

  1. 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法

    C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...

  2. CF718C Sasha and Array 线段树+矩阵加速

    正解:线段树 解题报告: 传送门! 首先这种斐波拉契,又到了1e9的范围,又是求和什么的,自然而然要想到矩阵加速昂 然后这里主要是考虑修改操作,ai+=x如果放到矩阵加速中是什么意思呢QAQ? 那不就 ...

  3. CF718C Sasha and Array [线段树+矩阵]

    我们考虑线性代数上面的矩阵知识 啊呸,是基础数学 斐波那契的矩阵就不讲了 定义矩阵 \(f_x\) 是第 \(x\) 项的斐波那契矩阵 因为 \(f_i * f_j = f_{i+j}\) 然后又因为 ...

  4. CF719E. Sasha and Array [线段树维护矩阵]

    CF719E. Sasha and Array 题意: 对长度为 n 的数列进行 m 次操作, 操作为: a[l..r] 每一项都加一个常数 C, 其中 0 ≤ C ≤ 10^9 求 F[a[l]]+ ...

  5. Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵

    E. Sasha and Array 题目连接: http://codeforces.com/contest/719/problem/E Description Sasha has an array ...

  6. codeforces 719E E. Sasha and Array(线段树)

    题目链接: E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input sta ...

  7. hdu 5068(线段树+矩阵乘法)

    矩阵乘法来进行所有路径的运算, 线段树来查询修改. 关键还是矩阵乘法的结合律. Harry And Math Teacher Time Limit: 5000/3000 MS (Java/Others ...

  8. 【对不同形式矩阵的总结】WC 2009 最短路径问题(线段树+矩阵乘法)

    题意 ​ 题目链接:https://www.luogu.org/problem/P4150 ​ 一个 \(6\times n\) 的网格图,每个格点有一个初始权值.有两种操作: 修改一个格子的权值 求 ...

  9. MAZE(2019年牛客多校第二场E题+线段树+矩阵乘法)

    题目链接 传送门 题意 在一张\(n\times m\)的矩阵里面,你每次可以往左右和下三个方向移动(不能回到上一次所在的格子),\(1\)表示这个位置是墙,\(0\)为空地. 现在有\(q\)次操作 ...

随机推荐

  1. 【图像处理】【计算机视觉】findContours的使用

    原文地址:findContours函数参数说明及相关函数作者:鸳都学童 findContours函数,这个函数的原型为: void findContours(InputOutputArray imag ...

  2. 禁止layer.msg()回调函数时抖动

    layer.msg(resp.msg, { shift: -1, time: 2000 }, function () {                                window.l ...

  3. Solr 4.4.0安装

    软件包准备 JDK http://download.oracle.com/otn/java/jdk/6u45-b06/jdk-6u45-linux-x64.bin Solr https://archi ...

  4. MySQL-线上数据迁移实战记录

    1. 迁移背景和限制条件 随着功能的迭代或者数据表中数据量的增加,将现有数据进行迁移已是工作中经常遇到的事情.通常我们在平时迁移数据数据的时候,只需要用mysqldump.mysqlimport指令就 ...

  5. APT高持续渗透攻击-后门篇

    APT是指高级持续性威胁, 利用先进的攻击手段对特定目标进行长期持续性网络攻击的攻击形式,APT攻击的原理相对于其他攻击形式更为高级和先进,其高级性主要体现在APT在发动攻击之前需要对攻击对象的业务流 ...

  6. jackson json序列化 首字母大写 第二个字母需小写

    有这样一个类: @Setter @Getter @JsonNaming(value = PropertyNamingStrategy.UpperCamelCaseStrategy.class) pub ...

  7. [转载]Oracle之单引号与双引号

    一.单引号 1.引用一个字符串常量,也就是界定一个字符串的开始和结束 select * from t_sys_user where id='15'; --查询id为15的字符 select * fro ...

  8. 十二、LaTex中数学公式多行排版

  9. Win7安装Visual Studio 2019闪退问题

    最近在Win7 系统上安装最新版的VS2019发现 每次在这个画面之后就闪退了,即便换了台电脑也是一样的情况,于是我意识到,这应该是系统本身的问题 经过调查发现是只需要安装两个更新就可以了 这两个更新 ...

  10. ipcs - 提供基于 ipc (Inter-process communication)结构的信息

    总览 ipcrm [ shm | msg | sem ] id 描述 ipcrm 将删除指定的资源--指定 id.