题目链接:http://codeforces.com/contest/719/problem/E

题意:操作1将[l, r] + x;

操作2求f[l] + ... + f[r];

题解:注意矩阵可以是a*(b + c) = a*b + a*c ,还有update的时候传入矩阵

 //#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e5 + ;
LL a[N], mod = 1e9 + ;
struct data {
LL mat[][];
data() {
mat[][] = mat[][] = mat[][] = ;
mat[][] = ;
}
void init() {
mat[][] = mat[][] = ;
mat[][] = mat[][] = ;
}
}op;
struct SegTree {
int l, r;
data lazy;
data val;
}T[N << ]; data operator +(const data &a, const data &b) {
data ans;
for(int i = ; i <= ; ++i) {
for(int j = ; j <= ; ++j) {
ans.mat[i][j] = (a.mat[i][j] + b.mat[i][j]) % mod;
}
}
return ans;
} data operator *(const data &a, const data &b) {
data ans;
for(int i = ; i <= ; ++i) {
for(int j = ; j <= ; ++j) {
ans.mat[i][j] = ;
for(int k = ; k <= ; ++k) {
ans.mat[i][j] = (ans.mat[i][j] + a.mat[i][k] * b.mat[k][j] % mod) % mod;
}
}
}
return ans;
} data operator ^(data a, LL n) {
data ans;
for(int i = ; i <= ; ++i) {
for(int j = ; j <= ; ++j) {
ans.mat[i][j] = (i == j);
}
}
while(n) {
if(n & )
ans = ans * a;
a = a * a;
n >>= ;
}
return ans;
} bool cmp(const data &a) {
for(int i = ; i <= ; ++i) {
for(int j = ; j <= ; ++j) {
if(a.mat[i][j] != op.mat[i][j])
return false;
}
}
return true;
} inline void pushdown(int p) {
if(!cmp(T[p].lazy)) {
T[p << ].lazy = T[p].lazy * T[p << ].lazy;
T[(p << )|].lazy = T[p].lazy * T[(p << )|].lazy;
T[p << ].val = T[p << ].val * T[p].lazy;
T[(p << )|].val = T[(p << )|].val * T[p].lazy;
T[p].lazy.init();
}
} void build(int p, int l, int r) {
int mid = (l + r) >> ;
T[p].l = l, T[p].r = r, T[p].lazy.init();
if(l == r) {
scanf("%lld", a + l);
T[p].val = T[p].val ^ (a[l] - );
return ;
}
build(p << , l, mid);
build((p << )|, mid + , r);
T[p].val = T[p << ].val + T[(p << )|].val;
} void update(int p, int l, int r, data add) {
int mid = (T[p].l + T[p].r) >> ;
if(T[p].l == l && T[p].r == r) {
T[p].lazy = add * T[p].lazy;
T[p].val = T[p].val * add;
return ;
}
pushdown(p);
if(r <= mid) {
update(p << , l, r, add);
} else if(l > mid) {
update((p << )|, l, r, add);
} else {
update(p << , l, mid, add);
update((p << )|, mid + , r, add);
}
T[p].val = T[p << ].val + T[(p << )|].val;
} LL query(int p, int l, int r) {
int mid = (T[p].l + T[p].r) >> ;
if(T[p].l == l && T[p].r == r) {
return T[p].val.mat[][];
}
pushdown(p);
if(r <= mid) {
return query(p << , l, r);
} else if(l > mid) {
return query((p << )|, l, r);
} else {
return (query(p << , l, mid) + query((p << )|, mid + , r)) % mod;
}
} int main()
{
op.init();
int n, m;
scanf("%d %d", &n, &m);
build(, , n);
int c, l, r;
LL add;
while(m--) {
scanf("%d %d %d", &c, &l, &r);
if(c == ) {
scanf("%lld", &add);
data temp;
update(, l, r, temp ^ add);
} else {
printf("%lld\n", query(, l, r));
}
}
return ;
}

Codeforces 719 E. Sasha and Array (线段树+矩阵运算)的更多相关文章

  1. codeforces 719E E. Sasha and Array(线段树)

    题目链接: E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input sta ...

  2. [Codeforces 266E]More Queries to Array...(线段树+二项式定理)

    [Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...

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

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

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

  5. CF719E. Sasha and Array [线段树维护矩阵]

    CF719E. Sasha and Array 题意: 对长度为 n 的数列进行 m 次操作, 操作为: a[l..r] 每一项都加一个常数 C, 其中 0 ≤ C ≤ 10^9 求 F[a[l]]+ ...

  6. CF718C Sasha and Array 线段树+矩阵加速

    正解:线段树 解题报告: 传送门! 首先这种斐波拉契,又到了1e9的范围,又是求和什么的,自然而然要想到矩阵加速昂 然后这里主要是考虑修改操作,ai+=x如果放到矩阵加速中是什么意思呢QAQ? 那不就 ...

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

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

  8. CF718C Sasha and Array [线段树+矩阵]

    我们考虑线性代数上面的矩阵知识 啊呸,是基础数学 斐波那契的矩阵就不讲了 定义矩阵 \(f_x\) 是第 \(x\) 项的斐波那契矩阵 因为 \(f_i * f_j = f_{i+j}\) 然后又因为 ...

  9. [Codeforces 280D]k-Maximum Subsequence Sum(线段树)

    [Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...

随机推荐

  1. KVC和KVO的区别

    kvc和kvo 1.kvc Key-Value Coding (KVC) KVC,即是指 NSKeyValueCoding,一个非正式的 Protocol,提供一种机制来间接访问对象的属性.KVO 就 ...

  2. Localizing Astah – Chinese version(simplified) is now available!

    Thanks to Abbey, now GUI in Astah Community can be shown in Chinese. As Abbey created Chinese one, a ...

  3. Dom文档模型

    文档对象模型     通过 JavaScript,您可以重构整个 HTML 文档.您可以添加.移除.改变或重排页面上的项目.要改变页面的某个东西,JavaScript 就需要获得对 HTML 文档中所 ...

  4. watch 命令实时命令执行监控

    watch 命令   watch -d -n 1 'df; ls -FlAt /path' 在使用这条命令时你需要替换其中的 /path 部分,watch 是实时监控工具,-d 参数会高亮 显示变化的 ...

  5. 关于ios越狱开发的那些事

    也许吧,每每接触某些新东西的时候,都有点犯晕吧,这不是应该要的. 第一次接触ios越狱开发,也是这样吧.这篇主要是从无到有的说一下ios越狱的开发,网上很多的教程大部门都比较旧了吧,放在新设备上总是出 ...

  6. tcp的精髓:滑动窗口

    TCP协议作为一个可靠的面向流的传输协议,其可靠性和流量控制由滑动窗口协议保证,而拥塞控制则由控制窗口结合一系列的控制算法实现.一.滑动窗口协议 关于这部分自己不晓得怎么叙述才好,因为理解的部分更多, ...

  7. 在Android中使用并发来提高速度和性能

    Android框架提供了很实用的异步处理类.然而它们中的大多数在一个单一的后台线程中排队.当你需要多个线程时你是怎么做的? 众所周知,UI更新发生在UI线程(也称为主线程).在主线程中的任何操作都会阻 ...

  8. ylb:多表的连接与练习(第三方关联表的应用)

    ylbtech-SQL Server:SQL Server-多表的连接与练习(第三方关联表的应用) SQL Server 多表的连接与练习(第三方关联表的应用). 1,多表的连接与练习(第三方关联表的 ...

  9. 带删除小图标的EditText

    import android.content.Context; import android.graphics.Rect; import android.graphics.drawable.Drawa ...

  10. 苹果将通过新Apple TV打造电视游戏平台 欲发力家庭游戏(转)

    据<纽约时报>报道,9月10日凌晨1时举行的苹果发布会上将会公布新版Apple TV设备,还会推出TV版App Store.新设备以游戏作为主要卖点,图形性能将大幅提升. 苹果2015年秋 ...