题面简洁明了,一看就懂

做了这个题之后,才知道怎么用线段树维护递推式。递推式的递推过程可以看作两个矩阵相乘,假设矩阵A是初始值矩阵,矩阵B是变换矩阵,求第n项相当于把矩阵B乘了n - 1次。

那么我们线段树中每个点维护把矩阵B乘了多少次,懒标记下放的时候用快速幂维护sum。

#include <bits/stdc++.h>
#define LL long long
#define ls(x) (x << 1)
#define rs(x) ((x << 1) | 1)
using namespace std;
const LL mod = 1000000007;
const int maxn = 100010;
struct Matrix {
static const int len = 2;
LL x[len][len]; void init() {
memset(x, 0, sizeof(x));
for (int i = 0; i < len; i++)
x[i][i] = 1;
} void zero() {
memset(x, 0, sizeof(x));
} Matrix operator * (const Matrix& m) const {
Matrix ans;
ans.zero();
for (int i = 0; i < len; i++)
for (int j = 0; j < len; j++)
for (int k = 0; k < len; k++)
ans.x[i][j] = (ans.x[i][j] + x[i][k] * m.x[k][j]) % mod;
return ans;
} Matrix operator + (const Matrix& m) const {
Matrix ans;
ans.zero();
for (int i = 0; i < len; i++)
for (int j = 0; j < len; j++)
ans.x[i][j] = (x[i][j] + m.x[i][j]) % mod;
return ans;
} Matrix operator ^ (int b) const {
Matrix ans, a;
ans.init();
memcpy(a.x, x, sizeof(x));
for (; b; b >>= 1) {
if(b & 1) ans = ans * a;
a = a * a;
}
return ans;
}
}; Matrix mul , tmp, trans ;
int a[maxn];
struct SegementTree {
int lz;
Matrix sum, flag;
}; SegementTree tr[maxn * 4]; void maintain(int o) {
tr[o].sum = tr[ls(o)].sum + tr[rs(o)].sum;
} void pushdown(int o) {
if(tr[o].lz) {
tr[ls(o)].sum = tr[ls(o)].sum * tr[o].flag;
tr[rs(o)].sum = tr[rs(o)].sum * tr[o].flag;
tr[ls(o)].flag = tr[ls(o)].flag * tr[o].flag;
tr[rs(o)].flag = tr[rs(o)].flag * tr[o].flag;
tr[o].lz = 0;
tr[ls(o)].lz = 1;
tr[rs(o)].lz = 1;
tr[o].flag.init();
}
} void build(int o, int l, int r) {
tr[o].sum.zero();
tr[o].lz = 0;
tr[o].flag.init();
if(l == r) {
tr[o].sum = trans * ( mul ^ (a[l] - 1));
return;
}
int mid = (l + r) >> 1;
build(ls(o), l, mid);
build(rs(o), mid + 1, r);
maintain(o);
} void update(int o, int l, int r, int ql, int qr, Matrix now) {
if(l >= ql && r <= qr) {
tr[o].sum = tr[o].sum * now;
tr[o].flag = tr[o].flag * now;
tr[o].lz = 1;
return;
}
pushdown(o);
int mid = (l + r) >> 1;
if(ql <= mid) update(ls(o), l, mid, ql, qr, now);
if(qr > mid) update(rs(o), mid + 1, r, ql, qr, now);
maintain(o);
} LL query(int o, int l, int r, int ql, int qr) {
if(l >= ql && r <= qr) {
return tr[o].sum.x[0][1];
}
pushdown(o);
int mid = (l + r) >> 1;
LL ans = 0;
if(ql <= mid) ans = (ans + query(ls(o), l, mid, ql, qr)) % mod;
if(qr > mid) ans = (ans + query(rs(o), mid + 1, r, ql, qr)) % mod;
return ans;
} int main() {
int n, m, op, l, r;
LL x;
trans.zero();
trans.x[0][1] = 1;
mul.x[0][1] = mul.x[1][0] = mul.x[1][1] = 1;
mul.x[0][0] = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
build(1, 1, n);
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &op, &l, &r);
if(op == 1) {
scanf("%lld", &x);
tmp = (mul ^ x);
update(1, 1, n, l, r, tmp);
} else {
printf("%lld\n", query(1, 1, n, l, r));
}
}
}

  

Codeforces 719E (线段树教做人系列) 线段树维护矩阵的更多相关文章

  1. Codeforces 1136E Nastya Hasn't Written a Legend (线段树教做人系列)

    题意:有一个数组a和一个数组k,数组a一直保持一个性质:a[i + 1] >= a[i] + k[i].有两种操作:1,给某个元素加上x,但是加上之后要保持数组a的性质.比如a[i]加上x之后, ...

  2. 线段树教做人系列(2)HDU 4867 XOR

    题意:给你一个数组a,长度为.有两种操作.一种是改变数组的某个元素的值,一种是满足某种条件的数组b有多少种.条件是:b[i] <= a[i],并且b[1]^b[2]...^b[n] = k的数组 ...

  3. 线段树教做人系列(1)HDU4967 Handling the Past

    题意:给你n组操作,分别为压栈,出栈,询问栈顶元素.每一组操作有一个时间戳,每次询问栈顶的元素的操作询问的是在他之前出现的操作,而且时间戳小于它的情况.题目中不会出现栈为空而且出栈的情况. 例如: p ...

  4. 线段树教做人系列(3) HDU 4913

    题意及思路看这篇博客就行了,讲得很详细. 下面是我自己的理解: 如果只有2,没有3的话,做法就很简单了,只需要对数组排个序,然后从小到大枚举最大的那个数.那么它对答案的贡献为(假设这个数排序后的位置是 ...

  5. Codeforces 750E New Year and Old Subsequence - 线段树 - 动态规划

    A string t is called nice if a string "2017" occurs in t as a subsequence but a string &qu ...

  6. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  7. Codeforces 1063F - String Journey(后缀数组+线段树+dp)

    Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小 ...

  8. Codeforces 1368H - Breadboard Capacity(最小割+线段树维护矩阵乘法)

    Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 首先看到这种从某一种颜色 ...

  9. Codeforces 750E - New Year and Old Subsequence(线段树维护矩阵乘法,板子题)

    Codeforces 题目传送门 & 洛谷题目传送门 u1s1 我做这道 *2600 的动力是 wjz 出了道这个套路的题,而我连起码的思路都没有,wtcl/kk 首先考虑怎样对某个固定的串计 ...

随机推荐

  1. 《Advanced Bash-scripting Guide》学习(十四):HERE Document和cat <<EOF

    本文所选的例子来自于<Advanced Bash-scripting Gudie>一书,译者 杨春敏 黄毅 #here document cat <<EOF \z EOF ca ...

  2. MySQL 大数据量修改表结构问题

    前言: 在系统正常运作一定时间后,随着市场.产品汪的需求不断变更,比较大的一些表结构面临不得不增加字段的方式来扩充满足业务需求:  而 MySQL 在体量上了千万.亿级别数据的时候,Alter Tab ...

  3. java重置Timer执行频率

    public class BallUtil { public static Timer fisTimer ; public static void fisStartBall(){ long first ...

  4. 第三次ScrumMeeting

    每个人的工作(有Issue的内容和链接):昨天已完成的工作,今天计划完成的工作:工作中遇到的困难. --by 张华杰 团队成员 昨日完成任务 明日要完成的任务 易子沐 搭建主页框架 issue14 前 ...

  5. SMB/CIFS协议简介

    1. 简介:(ServerMessage Block)服务消息块通信协议是微软(Microsoft)和英特尔(Intel)在1987年制定的协议,主要是作为Microsoft网络的通讯协议.SMB从I ...

  6. C#中DEV控件,XtraTabPage得小方法

    DEV控件设计窗体程序,XtraTabPage用到的小方法,欢迎大家评论,分享技术! //DEV中的选项卡 private bool TabCtlPageExist(string pageName) ...

  7. GC(Garbage Collection)垃圾回收机制

    1.在垃圾回收器中,程序员没有执行权,只有通知它的权利. 2.程序员可以通过System.gc().通知GC运行,但是Java规范并不能保证立刻运行. 3.finalize()方法,是java提供给程 ...

  8. 【LeetCode】029. Divide Two Integers

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...

  9. python函数-filter()

    filter(func, seq)  filter()函数是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断, ...

  10. Qt中int转换成QString

    (1) QString QString::number ( long n, int base = 10 ) [static] examle: long a = 48; QString s = QStr ...