CF718C Sasha and Array 线段树 + 矩阵乘法
- 将 $[l,r]$所有数 + $x$
- 求 $\sum_{i=l}^{r}fib(i)$
$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 线段树 + 矩阵乘法的更多相关文章
- 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法
C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...
- CF718C Sasha and Array 线段树+矩阵加速
正解:线段树 解题报告: 传送门! 首先这种斐波拉契,又到了1e9的范围,又是求和什么的,自然而然要想到矩阵加速昂 然后这里主要是考虑修改操作,ai+=x如果放到矩阵加速中是什么意思呢QAQ? 那不就 ...
- CF718C Sasha and Array [线段树+矩阵]
我们考虑线性代数上面的矩阵知识 啊呸,是基础数学 斐波那契的矩阵就不讲了 定义矩阵 \(f_x\) 是第 \(x\) 项的斐波那契矩阵 因为 \(f_i * f_j = f_{i+j}\) 然后又因为 ...
- CF719E. Sasha and Array [线段树维护矩阵]
CF719E. Sasha and Array 题意: 对长度为 n 的数列进行 m 次操作, 操作为: a[l..r] 每一项都加一个常数 C, 其中 0 ≤ C ≤ 10^9 求 F[a[l]]+ ...
- 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 ...
- codeforces 719E E. Sasha and Array(线段树)
题目链接: E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input sta ...
- hdu 5068(线段树+矩阵乘法)
矩阵乘法来进行所有路径的运算, 线段树来查询修改. 关键还是矩阵乘法的结合律. Harry And Math Teacher Time Limit: 5000/3000 MS (Java/Others ...
- 【对不同形式矩阵的总结】WC 2009 最短路径问题(线段树+矩阵乘法)
题意 题目链接:https://www.luogu.org/problem/P4150 一个 \(6\times n\) 的网格图,每个格点有一个初始权值.有两种操作: 修改一个格子的权值 求 ...
- MAZE(2019年牛客多校第二场E题+线段树+矩阵乘法)
题目链接 传送门 题意 在一张\(n\times m\)的矩阵里面,你每次可以往左右和下三个方向移动(不能回到上一次所在的格子),\(1\)表示这个位置是墙,\(0\)为空地. 现在有\(q\)次操作 ...
随机推荐
- go的变量定义
package main //理解包的概念 import "fmt" var ( aa = 1 bb = "kkk" ss = true) func varia ...
- Java -cp命令的使用
服务器跑程序,用到了一些Linux命令,做个简单笔记. Linux(Mac)下 java -cp .:jar包路径 主类的全限定名称 全限定名有绝对路径的意思,比如一个文件file的存放路径, ...
- L2-001. 紧急救援(迪杰斯特拉算法)
L2-001. 紧急救援 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国 ...
- [JavaScript] es6规则总结
let 和 const let 是块级作用域 let 声明的变量只在其所在的作用域内生效 <script> { var today = "周3"; let yester ...
- Linux 问题集
解决E: Encountered a section with no Package: header错误 我的ubuntu机器上出现下面这个错误. Reading package lists... E ...
- Fliter设置字符编码,解决中文问题
class EncodingFilter implements FileFilter{ private String encoding; @Override public boolean accept ...
- numpy.random.uniform(记住文档网址)
http://docs.scipy.org/doc/numpy/reference/generated/numpy.random.uniform.html#numpy.random.uniform h ...
- Scrapy-redis分布式+Scrapy-redis实战
[学习目标] Scrapy-redis分布式的运行流程 Scheduler与Scrapy自带的Scheduler有什么区别 Duplication Filter作用 源码自带三种spider的使用 6 ...
- 009-通过jmx监控tomcat
前言想理解怎么监控tomcat,必需识下图(图片源出网络) zabbix-Web前端界面,它通过数据库里数据展示.和其它组件不直接关联zabbix-server运行在10051端口,Zabbix-se ...
- Binary Numbers AND Sum CodeForces - 1066E (前缀和)
You are given two huge binary integer numbers aa and bb of lengths nn and mmrespectively. You will r ...