题意:

  一栋楼有n层,每一层有2个门,每层的两个门和下一层之间的两个门之间各有一条路(共4条)。

  有两种操作:

  0 x y : 输出第x层到第y层的路径数量。

  1 x y z : 改变第x层 的 y门 到第x+1层的 z门的通断情况。

思路:

  门之间的路径数可以用矩阵来表示,经过的中间层可以用矩阵乘积表示。 所以用线段树维护矩阵乘积即可。

代码:

  

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <cctype>
#include <time.h> using namespace std; typedef __int64 ll; const int INF = <<;
const int MAXN = 5e4+;
const ll MOD = 1e9+; struct Matrix {
ll a[][]; Matrix() {} Matrix(int x) {
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
a[i][j] = i==j ? x : ;
} Matrix operator * (const Matrix &x) {
Matrix res;
for (int i = ; i < ; i ++) {
for (int j = ; j < ; j++) {
res.a[i][j] = ;
for (int k = ; k < ; k++) {
res.a[i][j] = (res.a[i][j]+a[i][k]*x.a[k][j])%MOD;
}
}
}
return res;
} inline ll sum() {
return (a[][] + a[][] + a[][] + a[][])%MOD;
} inline void update(int i, int j) {
a[i][j] ^= ;
} inline void init() {
a[][] = a[][] = a[][] = a[][] = ;
}
void output() {
puts("Matrix: ");
for (int i = ; i < ; i ++) {
for (int j = ; j < ; j++)
printf("%I64d ", a[i][j]);
puts("");
}
}
}; Matrix a[MAXN<<];
int id[MAXN]; #define LS l, m, p<<1
#define RS m+1, r, p<<1|1 inline void pushUp(int p) {
a[p] = a[p<<]*a[p<<|];
} void build(int l, int r, int p) {
if (l==r) {
a[p].init();
id[l] = p;
return ;
}
int m = (l+r) >> ;
build(LS);
build(RS);
pushUp(p);
} void update(int p, int i, int j) {
p = id[p];
a[p].update(i, j);
p >>= ;
while (p>) {
pushUp(p);
p >>= ;
}
} Matrix query(int L, int R, int l, int r, int p) {
if (L<=l&&r<=R)
return a[p]; int m = (l+r)>>; Matrix res();
if (L<=m) res = res * query(L, R, LS);
if (m <R) res = res * query(L, R, RS); return res;
} int main() {
#ifdef Phantom01
freopen("1003.txt", "r", stdin);
#endif //Phantom01 int n, m;
int op, x, y, z;
while (scanf("%d%d", &n, &m)!=EOF) {
n--;
build(, n, );
while (m--) {
scanf("%d", &op);
if (op==) {
scanf("%d%d", &x, &y);
y--;
printf("%I64d\n", query(x, y, , n, ).sum());
} else {
scanf("%d%d%d", &x, &y, &z);
y--; z--;
update(x, y, z);
}
}
} return ;
}

HDU 5068 Harry And Math Teacher 线段树+矩阵乘法的更多相关文章

  1. hdu 5068(线段树+矩阵乘法)

    矩阵乘法来进行所有路径的运算, 线段树来查询修改. 关键还是矩阵乘法的结合律. Harry And Math Teacher Time Limit: 5000/3000 MS (Java/Others ...

  2. HDU 5068 Harry And Math Teacher

    主题链接~~> 做题情绪:的非常高深,有种高大上的感觉. 解题思路: 两层之间的联通能够看成是一个矩阵  代表上下两层都能够联通,,代表下层第1个门与上层第一个门不联通,以此类推联通就能够用矩阵 ...

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

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

  4. LOJ2980 THUSC2017大魔法师(线段树+矩阵乘法)

    线段树每个节点维护(A,B,C,len)向量,操作即是将其乘上一个矩阵. #include<iostream> #include<cstdio> #include<cma ...

  5. 【对不同形式矩阵的总结】WC 2009 最短路径问题(线段树+矩阵乘法)

    题意 ​ 题目链接:https://www.luogu.org/problem/P4150 ​ 一个 \(6\times n\) 的网格图,每个格点有一个初始权值.有两种操作: 修改一个格子的权值 求 ...

  6. MAZE(2019年牛客多校第二场E题+线段树+矩阵乘法)

    题目链接 传送门 题意 在一张\(n\times m\)的矩阵里面,你每次可以往左右和下三个方向移动(不能回到上一次所在的格子),\(1\)表示这个位置是墙,\(0\)为空地. 现在有\(q\)次操作 ...

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

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

  8. Wannafly Winter Camp Day8(Div1,onsite) E题 Souls-like Game 线段树 矩阵乘法

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog @ Problem:传送门  Portal  原题目描述在最下面.  简单的 ...

  9. [tsA1490][2013中国国家集训队第二次作业]osu![概率dp+线段树+矩阵乘法]

    这样的题解只能舔题解了,,,qaq 清橙资料里有.. #include <iostream> #include <cstdio> #include <cstdlib> ...

随机推荐

  1. 转:EL表达式

    简介: EL 全名为 Language ,JSP2.0 之后,EL 成为了标准规范.因此,只要是支持Servlet2.4/JSP2.0 的容器,就都可以在JSP 网页中直接使用EL . 除了JSP2. ...

  2. [转]C++ 获取文件夹下的所有文件名

    转自http://www.cnblogs.com/fnlingnzb-learner/p/6424563.html 头文件:#include<io.h> char * filePath = ...

  3. vector ----- size函数注意事项

    vector 的size函数返回vector大小,返回值类型为size_type,Member type size_type is an unsigned integral type,即无符号整数: ...

  4. HDU-1024 Max Sum Plus Plus 动态规划 滚动数组和转移优化

    题目链接:https://cn.vjudge.net/problem/HDU-1024 题意 给n, m和一个序列,找m个不重叠子串,使这几个子串内元素和的和最大. n<=1e6 例:1 3 1 ...

  5. Oralce 视图 view

    Oracle视图 Oracle的数据库对象分为五种:表,视图,序列,索引和同义词. 视图是基于一个表或多个表或视图的逻辑表,本身不包含数据,通过它可以对表里面的数据进行查询和修改.视图基于的表称为基表 ...

  6. 从A小程序跳转到B小程序

    从A小程序跳转到B小程序: A小程序 wxml: <navigator target="miniProgram" open-type="navigate" ...

  7. Noip-pj2018游记

    2019/1/3 搬运于我的luogu博客 2018/10/9 没有去试机,在学校搞文化课去了.准考证是让学校的信息课老师帮我拿的 回家后随手A了P1198 P3870 P2846 P1531 感觉真 ...

  8. Camera Calibration 相机标定:原理简介(五)

    5 基于2D标定物的标定方法 基于2D标定物的标定方法,原理与基于3D标定物相同,只是通过相机对一个平面进行成像,就可得到相机的标定参数,由于标定物为平面,本身所具有的约束条机,相对后者标定更为简单. ...

  9. 2016 10 26考试 NOIP模拟赛 杂题

    Time 7:50 AM -> 11:15 AM 感觉今天考完后,我的内心是崩溃的 试题 考试包 T1: 首先看起来是个贪心,然而,然而,看到那个100%数据为n <= 2000整个人就虚 ...

  10. Fragmen直接来回切换deno

    思路: 第一步.建立一个activity.用来管理fragment. 第二步'获取fragmentManger 和fragmentTraction. private FragmentManager f ...