CF719E. Sasha and Array

题意:

对长度为 n 的数列进行 m 次操作, 操作为:

  1. a[l..r] 每一项都加一个常数 C, 其中 0 ≤ C ≤ 10^9
  2. 求 F[a[l]]+F[a[l+1]]+...F[a[r]] mod 1e9+7 的余数

矩阵快速幂求斐波那契

矩阵满足乘法分配律和结合律!

所以可以每个节点维护矩阵/矩阵和,区间加相当于区间乘矩阵

注意:不要把快速幂写在里面,复杂度平添一个log。把\(B^C\)算出来之后传进去就好了

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
#define lc x<<1
#define rc x<<1|1
#define mid ((l+r)>>1)
#define lson lc, l, mid
#define rson rc, mid+1, r
const int N = 1e5+5, P = 1e9+7; int n, m;
struct Matrix {
ll a[2][2];
ll* operator [](int x) {return a[x];}
Matrix(int p=0) {
if(!p) a[0][0] = a[0][1] = a[1][0] = a[1][1] = 0;
else a[0][0] = a[1][1] = 1, a[0][1] = a[1][0] = 0;
}
} B; Matrix operator + (Matrix a, Matrix b) {
Matrix c;
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
c[i][j] = (a[i][j] + b[i][j]) %P;
return c;
} Matrix operator * (Matrix a, Matrix b) {
Matrix c;
for(int i=0; i<2; i++)
for(int j=0; j<2; j++) {
ll &x = c[i][j];
for(int k=0; k<2; k++)
x = (x + a[i][k] * b[k][j] %P) %P;
}
return c;
} Matrix operator ^ (Matrix a, int b) {
Matrix ans(1);
ans[0][0] = ans[1][1] = 1;
for(; b; b>>=1, a=a*a)
if(b & 1) ans = ans*a;
return ans;
} struct meow {
Matrix f;
Matrix v;
int c;
meow() {f[0][0] = 1; v[0][0] = v[1][1] = 1;}
} t[N<<2]; void paint(int x, int l, int r, int d, Matrix &v) {
t[x].c += d;
t[x].v = v * t[x].v;
t[x].f = v * t[x].f;
}
void push_down(int x, int l, int r) {
if(t[x].c) {
paint(lson, t[x].c, t[x].v);
paint(rson, t[x].c, t[x].v);
t[x].c = 0;
t[x].v = Matrix(1);
}
}
void merge(int x) {
t[x].f = t[lc].f + t[rc].f;
} void build(int x, int l, int r) {
if(l == r) {
cin >> t[x].c;
if(t[x].c > 1) t[x].f = (B ^ (t[x].c - 1)) * t[x].f;
} else {
build(lson);
build(rson);
merge(x);
}
} void Add(int x, int l, int r, int ql, int qr, int d, Matrix &v) {
if(ql <= l && r <= qr) paint(x, l, r, d, v);
else {
push_down(x, l, r);
if(ql <= mid) Add(lson, ql, qr, d, v);
if(mid < qr) Add(rson, ql, qr, d,v );
merge(x);
}
} ll Que(int x, int l, int r, int ql, int qr) {
if(ql <= l && r <= qr) return t[x].f[0][0];
else {
push_down(x, l, r);
ll ans = 0;
if(ql <= mid) ans = (ans + Que(lson, ql, qr)) %P;
if(mid < qr) ans = (ans + Que(rson, ql, qr)) %P;
return ans;
}
} int main() {
//freopen("in", "r", stdin);
ios::sync_with_stdio(false); cin.tie(); cout.tie(); B[0][0] = B[0][1] = B[1][0] = 1;
cin >> n >> m;
build(1, 1, n); for(int i=1; i<=m; i++) {
int tp, l, r, x;
cin >> tp >> l >> r;
if(tp == 1) {
cin >> x;
Matrix t = B^x;
Add(1, 1, n, l, r, x, t);
} else cout << Que(1, 1, n, l, r) << '\n';
}

CF719E. Sasha and Array [线段树维护矩阵]的更多相关文章

  1. 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 ...

  2. 线段树维护矩阵【CF718C】 Sasha and Array

    Description 有一个长为\(n\)的数列\(a_{1},a_{2}...a_{n}\),你需要对这个数列维护如下两种操作: \(1\space l \space r\space x\) 表示 ...

  3. CF718C Sasha and Array(线段树维护矩阵)

    题解 (不会矩阵加速的先去学矩阵加速) 反正我想不到线段树维护矩阵.我太菜了. 我们在线段树上维护一个区间的斐波那契的列矩阵的和. 然后询问时提取每个符合题意列矩阵的答案项(不是列矩阵存了两项吗,一个 ...

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

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

  5. Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵

    Problem Description Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries ...

  6. hdu 5068 线段树维护矩阵乘积

    http://acm.hdu.edu.cn/showproblem.php?pid=5068 题意给的略不清晰 m个询问:从i层去j层的方法数(求连段乘积)或者修改从x层y门和x+1层z门的状态反转( ...

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

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

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

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

  9. CF718C Sasha and Array 线段树 + 矩阵乘法

    有两个操作: 将 $[l,r]$所有数 + $x$ 求 $\sum_{i=l}^{r}fib(i)$ $n=m=10^5$   直接求不好求,改成矩阵乘法的形式:  $a_{i}=M^x\times ...

随机推荐

  1. 一个网站SQL注入的案例

    网站的页面提交参数做了md5转换,而且参数会带入两个SQL语句中执行. 注入是肯定存在的,但是SQLMAP怎么都跑不出来(可能原因是其中有个SQL语句总是报错). 尝试手工,发现 order by 报 ...

  2. Java IO与网络编程笔记

    <!doctype html>Java IO&NIO figure:first-child { margin-top: -20px; } #write ol, #write ul ...

  3. Selenium-ActionChainsApi--鼠标连贯操作

    ActionChains UI自动化测试过程中,经常遇到那种,需要鼠标悬浮后,要操作的元素才会出现的这种场景,那么我们就要模拟鼠标悬浮到某一个位置,做一系列的连贯操作,Selenium给我们提供了Ac ...

  4. Win8 64位安装Oracle 11g时错

    Win8 64位 安装Oracle时会出现[INS-13001] 环境不满足最低要求 异常原因 11.2.0.1 比Win8 早发行,所以 兼容列表不可能兼容 Win 8. 解决方法一 以管理员身份 ...

  5. 为什么使用SLF4J?

      每个Java开发人员都知道日志记录对Java应用的重要性,尤其是对服务端应用,而且其中许多人都已经熟悉了各种记录日志的库,比如java.util.logging,Apache的log4j,logb ...

  6. 开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc (转)

      [原文地址]http://www.cnblogs.com/liqingwen/p/5898368.html 序 本打算过几天简单介绍下组件 Spire.XLS,突然发现园友率先发布了一篇,既然 x ...

  7. lua简单入门

    一.安装windows下的lua环境,luaforwindows 下载最新的5.3版本,下载地址: https://sourceforge.net/projects/luabinaries/files ...

  8. 微信小程序开发学习(二)

    一些官方API 总结了一些官方API,便于之后有用时针对性查找(发现官方给了好多好用的API)官方API文档 基础 wx.canIUse:判断小程序的API,回调,参数,组件等是否在当前版本可用,返回 ...

  9. Linux从入门到入门

    一. 前言 首先,在你的Windows系统上要想有linux系统,那就必须先安装一款软件,这里提供的是14.15的,还有ISO镜像:VMware-workstation 安装VMware:略 新建虚拟 ...

  10. UiAutomator2.0 - 获取同行控件

    目录 问题:UI测试时,在同一个界面出现相同的属性的控件(如图),对于这种控件的获取很是无奈.如果直接通过控件id去查找的话总是会返回界面该类型的第一个控件. 解决: 1.UiObject2 中已经给 ...