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\)次操作 ...
随机推荐
- magento下载地址
https://download.magentochina.org/magento/2/ https://www.magentochina.org/blog/download-install-mage ...
- vue--生命周期演示
vue实例的生命周期:https://cn.vuejs.org/v2/guide/instance.html#%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA-Vue-%E5% ...
- Servlet概念及与Jsp的区别
一.Servlet概念 Servlet是在服务器上运行的小程序.一个Servlet就是一个Java类,并且可以通过”请求-响应”编程模型来访问这个驻留在服务器内存里的Servlet程序 二.Servl ...
- CF 1178E Archaeology 题解
题面 这道题竟然是E?还是洛谷中的黑题? wow~!! 于是就做了一下: 然后一下就A了:(这并不代表想的容易,而是写的容易) 这道题就是骗人的!! 什么manacher,什么回文自动机,去靠一边站着 ...
- mysql自增主键字段重排
不带外键模式的 mysql 自增主键字段重排 1.备份表结构 create table table_bak like table_name; 2.备份表数据 insert into table_bak ...
- linux VMware 提供的网络连接有 5 种详细讲述
VMware 提供的网络连接有 5 种,分别是"桥接模式"."NAT 模式"."仅主机模式"."自定义"和"L ...
- wex5 如何使用蓝牙 ble
使用蓝牙插件 需要在js中添加 require("cordova!cordova-plugin-ble-central"); ble插件具体文档: http://docs.we ...
- vue的v-model指令原理分析
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 3-关于ES的几个小疑问和解答
1.ES如何实现分布式 2.ES如何实现高实时 3.ES如何实现高扩展 4.ES7.x版本为何废弃type 5.搜索原理--知乎es
- this 到底指向谁
this 的指向,是在调用函数时根据执行上下文所动态确定的. 在函数体中,简单调用该函数时(非显式/隐式绑定下),严格模式下 this 绑定到 undefined,否则绑定到全局对象 window/g ...