题意:

  一栋楼有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. C++之const关键字

    本文引自http://www.cnblogs.com/lichkingct/archive/2009/04/21/1440848.html ,略有增删 const关键字在c++中用法有很多,总结如下: ...

  2. C++之易混淆知识点二

    1.数据抽象与封装 数据抽象是一种接口和实现相分离的编程技术,设计者关心的是如何实现这些接口,而使用者仅仅知道这些接口,抽象地考虑这些接口做什么的就可以了,不必去考虑如何实现这一层次. 封装是将低层次 ...

  3. CSS3的常用属性(一)

    选择器 属性选择器(通过标签属性来选择) E[attr]: 表示只要元素<E>存在属性attr就能被选中  如: div[class] E[attr=val]: 表示元素<E> ...

  4. (转载)所有分类 > 开发语言与工具 > 移动开发 > Android开发 Android中的Service:默默的奉献者 (1)

    前言 这段时间在看一些IPC相关的东西,这里面就不可避免的要涉及到service,进程线程这些知识点,而且在研究的过程中我惊觉自己对这些东西的记忆已经开始有些模糊了——这可要不得.于是我就干脆花了点心 ...

  5. 高阶函数-lambda表达式

    #2.6 map()# 第一个参数传入一个函数,,第二个参数为一个可迭代对象li_1 = (1,3,5,7)def funcA(x): return x*xm1 = map(funcA,li_1)pr ...

  6. EFcore笔记之创建模型

    排除属性:NotMapped NotMapped:排除属性在CodeFirst的时候在数据库里不创建该属性   public class Blog { public int BlogId { get; ...

  7. 结构化数据、半结构化数据、非结构化数据——Hadoop处理非结构化数据

    刚开始接触Hadoop ,指南中说Hadoop处理非结构化数据,学习数据库的时候,老师总提结构化数据,就是一张二维表,那非结构化数据是什么呢?难道是文本那样的文件?经过上网搜索,感觉这个帖子不错 网址 ...

  8. 在 android studio 中更新安卓应用版本号

    随着应用程序不断修改,版本号也应当变化.要更新安卓应用的版本号,只需要在 build.gradle(module:app) 中修改 versionCode 和 versionName 即可,也可以只改 ...

  9. angular7升级到angular8

    1.首先我们对:angular的命令的安装 ng install -g @angular/cli的安装则会升级到最新的版本,并且再次创建项目的时候,我们就能够使用ng version查看到已经是最新的 ...

  10. BZOJ 4033[HAOI2015] 树上染色(树形DP)

    4033: [HAOI2015]树上染色 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 3188  Solved: 1366[Submit][Stat ...