题意:

  一栋楼有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. Django(part3)

    URLConf:负责url到view的map,就是一个urls.py module,通常在project和app级别都要定义, #mysite/urls.py from django.conf.url ...

  2. Intellij IDEA安装与使用,完整详细。

    https://blog.csdn.net/qq_41879385/article/details/81952656 https://www.jetbrains.com/idea/download/# ...

  3. 洛谷P3254 圆桌问题 网络流_二分图

    Code: #include<cstdio> #include<algorithm> #include<vector> #include<queue> ...

  4. 获取鼠标经过处的标签的标签名和id

    <script> var el = window.document.body; // 声明一个变量,默认值为body window.document.body.onmouseover = ...

  5. 计算a-b的差[返回BigDecimal 类型]

    /*** * 返回 a-b 的差 [返回 BigDecimal 类型] * @param a 被减数 * @param b 减数 * @return */ public static BigDecim ...

  6. NOIp2018模拟赛四十五~??

    欠的太多,咕了咕了 最近复赛临近时间紧,就不每次都写感想和题解了,只写点有意义的好题

  7. git 常用操作命令行

    mkdir files : 创建一个名字为files的文件夹 cd files : 切换目录到files pwd ; 显示当前所在目录 ls -ah : 查看本地隐藏不可见的文件夹 git init ...

  8. npm install报错类似于npm WARN tar ENOENT: no such file or directory, open '***\node_modules\.staging\***

    报错类似于如下图 解决方法: 删除文件 package-lock.json,再重新执行npm i或者npm install

  9. Docker安装MySQL忽略大小写问题的问题

    原文:Docker安装MySQL忽略大小写问题的问题 连接MySQL: 查看当前mysql的大小写敏感配置show global variables like '%lower_case%';+---- ...

  10. Java基础学习总结(46)——JAVA注解快速入门

    各位开发童鞋,注解这个东西我们肯定每天都能看见,也许有时候看的太多了到是会忽略注解这东西具体是如何工作的.今天在这里用最短的篇幅快速讲解下注解的原理,对这块记的不太清楚的同学也可以再次看看,下次有人详 ...