Codeforces 719E (线段树教做人系列) 线段树维护矩阵
题面简洁明了,一看就懂
做了这个题之后,才知道怎么用线段树维护递推式。递推式的递推过程可以看作两个矩阵相乘,假设矩阵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 (线段树教做人系列) 线段树维护矩阵的更多相关文章
- 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)HDU 4867 XOR
题意:给你一个数组a,长度为.有两种操作.一种是改变数组的某个元素的值,一种是满足某种条件的数组b有多少种.条件是:b[i] <= a[i],并且b[1]^b[2]...^b[n] = k的数组 ...
- 线段树教做人系列(1)HDU4967 Handling the Past
题意:给你n组操作,分别为压栈,出栈,询问栈顶元素.每一组操作有一个时间戳,每次询问栈顶的元素的操作询问的是在他之前出现的操作,而且时间戳小于它的情况.题目中不会出现栈为空而且出栈的情况. 例如: p ...
- 线段树教做人系列(3) HDU 4913
题意及思路看这篇博客就行了,讲得很详细. 下面是我自己的理解: 如果只有2,没有3的话,做法就很简单了,只需要对数组排个序,然后从小到大枚举最大的那个数.那么它对答案的贡献为(假设这个数排序后的位置是 ...
- 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 ...
- 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 ...
- Codeforces 1063F - String Journey(后缀数组+线段树+dp)
Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小 ...
- Codeforces 1368H - Breadboard Capacity(最小割+线段树维护矩阵乘法)
Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 首先看到这种从某一种颜色 ...
- Codeforces 750E - New Year and Old Subsequence(线段树维护矩阵乘法,板子题)
Codeforces 题目传送门 & 洛谷题目传送门 u1s1 我做这道 *2600 的动力是 wjz 出了道这个套路的题,而我连起码的思路都没有,wtcl/kk 首先考虑怎样对某个固定的串计 ...
随机推荐
- hdu4217splay
题意:有1到n的数组,每次删除第k小的值,并求和 题解:splay基本操作,删除+合并 坑点:由于不会c++指针操作,sb的只删除了头指针导致一直mle #include<bits/stdc++ ...
- Tomcat部署项目后有括号的处理方法
常见的问题,收录整理了一下,方便查找. 如下3个地方都修改为一致即可解决. 1,右键项目名 --> properties --> 输入web project settings --> ...
- 关于nginx访问 静态文件 403 的错误
例如 ngixn的配置的静态文件访问 如下: location /static { root /var/app/lxxxx/web; } 1.检查所有的文件有无读权限 chmod 644 -R 2.检 ...
- IDEA使用操作文档
IDEA中怎么设置黑色或白色背景? http://jingyan.baidu.com/article/4e5b3e19330df191911e246b.html 一. IntelliJ IDEA 的 ...
- 在Arcmap中加载互联网地图资源的4种方法(转载)
前一段时间想在Arcmap中打开互联网地图中的地图数据,如影像数据.基础地图数据等,经过简单研究目前总结了四种方法,整理下与大家分享,有些内容可能理解有误,希望大家多多指教.4种方法如下: a) ...
- Redis底层探秘(三):字典
字典,又称为符号表(symbol table).关联数组(associative array)或映射(map),是一种用于保存键值对的抽象数据结构. 字典经常作为一种数据结构内置在很多高级编程语言里面 ...
- Broken Keyboard(模拟数组或者双重链表的运用)
这题我是大写的服气,辛辛苦苦搞了个双重链表结果还一直不对,不对就算了,书上源代码打进去还是不对,我能怎么办我也很无奈.不过这题还是让我对双重链表更加了解和运用了!还是可以的! You’re typin ...
- [UOJ171][WC2016]挑战NPC
uoj luogu bzoj sol 你可以列一个表格. 一个框子里放球的数量 0 1 2 3 对"半空框子"数量的贡献 1 1 0 0 把一个框子拆三个点.两两之间连边. 会发现 ...
- docker容器的服务发现:consul
官网:https://www.consul.io 官网文档:https://www.consul.io/docs简介 consul是一个服务发现的组件,在docker世界中他比较流行,主要是consu ...
- poj 2154 Color——带优化的置换
题目:http://poj.org/problem?id=2154 置换的第二道题! 需要优化!式子是ans=∑n^gcd(i,n)/n (i∈1~n),可以枚举gcd=g,则有phi( n/g )个 ...