HDU 5068 Harry And Math Teacher 线段树+矩阵乘法
题意:
一栋楼有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 线段树+矩阵乘法的更多相关文章
- hdu 5068(线段树+矩阵乘法)
矩阵乘法来进行所有路径的运算, 线段树来查询修改. 关键还是矩阵乘法的结合律. Harry And Math Teacher Time Limit: 5000/3000 MS (Java/Others ...
- HDU 5068 Harry And Math Teacher
主题链接~~> 做题情绪:的非常高深,有种高大上的感觉. 解题思路: 两层之间的联通能够看成是一个矩阵 代表上下两层都能够联通,,代表下层第1个门与上层第一个门不联通,以此类推联通就能够用矩阵 ...
- 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法
C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...
- LOJ2980 THUSC2017大魔法师(线段树+矩阵乘法)
线段树每个节点维护(A,B,C,len)向量,操作即是将其乘上一个矩阵. #include<iostream> #include<cstdio> #include<cma ...
- 【对不同形式矩阵的总结】WC 2009 最短路径问题(线段树+矩阵乘法)
题意 题目链接:https://www.luogu.org/problem/P4150 一个 \(6\times n\) 的网格图,每个格点有一个初始权值.有两种操作: 修改一个格子的权值 求 ...
- MAZE(2019年牛客多校第二场E题+线段树+矩阵乘法)
题目链接 传送门 题意 在一张\(n\times m\)的矩阵里面,你每次可以往左右和下三个方向移动(不能回到上一次所在的格子),\(1\)表示这个位置是墙,\(0\)为空地. 现在有\(q\)次操作 ...
- CF718C Sasha and Array 线段树 + 矩阵乘法
有两个操作: 将 $[l,r]$所有数 + $x$ 求 $\sum_{i=l}^{r}fib(i)$ $n=m=10^5$ 直接求不好求,改成矩阵乘法的形式: $a_{i}=M^x\times ...
- Wannafly Winter Camp Day8(Div1,onsite) E题 Souls-like Game 线段树 矩阵乘法
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog @ Problem:传送门 Portal 原题目描述在最下面. 简单的 ...
- [tsA1490][2013中国国家集训队第二次作业]osu![概率dp+线段树+矩阵乘法]
这样的题解只能舔题解了,,,qaq 清橙资料里有.. #include <iostream> #include <cstdio> #include <cstdlib> ...
随机推荐
- SpringMVC(一) 简单代码编写,注解,重定向与转发
SpringMVC是什么 SpringMVC是目前最好的实现MVC设计模式的框架,是Spring框架的一个分支产品,以SpringIOC容器为基础,并利用容器的特性来简化它的配置.SpringMVC相 ...
- 还是bib问题
虽然昨天把添加bib的基本问题解决了,但是bib的参考文献没有了中括号.查了一下华东师大,只是给出了如何去掉中括号的方法. http://math.ecnu.edu.cn/~latex/doc.htm ...
- Eclipse使用struts2开发web应用快速搭建
eclipse 下载javaEE版,这里用4.5(Mars). Tomcat8解压,设置好JAVA_HOME环境变量. 下载struts2官网上的lib包,struts-2.3.24-lib.zip, ...
- 捕捉soap的xml形式
下面是我以前对Php的soap接口进行抓包分析出的结果,这个分析在当服务端或者客户端的Php没有安装soap模块时,可以使用构建xml的方式实现相同的功能 服务端: $data = $HTTP_RAW ...
- mysql中如何查看某个数据库或表占用的磁盘空间
查整个库的状态:select concat(truncate(sum(data_length)/1024/1024,2),'MB') as data_size, concat(tru ...
- BZOJ 1085 / LOJ 2151 [SCOI2005]骑士精神 (剪枝/A*迭代搜索)
题目大意:略 直接爆搜会T,我们优化一下,统计出当前棋盘和目标棋盘不同的位置的数量k,那么当前棋盘变成目标棋盘最少的移动次数是k-1 每次选择一个最大深度ma,那么如果当前走了dep步,显然必须保证d ...
- 【Paper Reading】Improved Textured Networks: Maximizing quality and diversity in Feed-Forward Stylization and Texture Synthesis
Improved Textured Networks: Maximizing quality and diversity in Feed-Forward Stylization and Texture ...
- dockerhub 推送镜像
登录dockerhub [root@riyimei-node1:/home] > docker login Login with your Docker ID to push and pull ...
- JAVA jsp page指令的属性 errorPage 和isErrorPage
>errorPage指定当前页面出现错误的实际响应页面是什么, 其中“/” 表示的是当前WEB应用的根目录 <% page errorPage="/error.jsp" ...
- linux进程管理之轻量级进程(四)
在Linux中,轻量级进程可以是进程,也可以是线程.我们所说的线程,在Linux中,其实是轻量级进程之间共享代码段,文件描述符,信号处理,全局变量时: 如果不共享,就是我们所说的进程. 进程是资源管理 ...