题意:

  一栋楼有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. Android 常用系统服务

    WindowManager:WindowManager服务是全局的唯一的.它会将用户在屏幕上的操作发送给界面上的各个Window,Activity会将顶层控件注册到WindowManager中.Win ...

  2. 学习es6 setter/getter研究

    1.背景 在ES6中,我们对类的定义如下 class Person { // 构造函数 constructor (name) { // 属性初始化 this.name = name; } // 成员方 ...

  3. (转载) 百度地图工具类封装(包括定位,附近、城市、范围poi检索,反地理编码)

    目录视图 摘要视图 订阅 赠书 | 异步2周年,技术图书免费选      程序员8月书讯      项目管理+代码托管+文档协作,开发更流畅 百度地图工具类封装(包括定位,附近.城市.范围poi检索, ...

  4. Codeforces 988E. Divisibility by 25

    解题思路: 只有尾数为25,50,75,00的数才可能是25的倍数. 对字符串做4次处理,以25为例. a. 将字符串中的最后一个5移到最后一位.计算交换次数.(如果没有找到5,则不可能凑出25,考虑 ...

  5. H5中嵌入flash

    <object height="900px" width="100%" classid="clsid:D27CDB6E-AE6D-11cf-96 ...

  6. How Javascript works (Javascript工作原理) (六) WebAssembly 对比 JavaScript 及其使用场景

    个人总结: 1.webassembly简介:WebAssembly是一种用于开发网络应用的高效,底层的字节码.允许在网络应用中使用除JavaScript的语言以外的语言(比如C,C++,Rust及其他 ...

  7. 【BZOJ4448】【SCOI2015】情报传递

    这题面错别字真tm多 题意: Description 奈特公司是一个巨大的情报公司,它有着庞大的情报网络.情报网络中共有n名情报员.每名情报员口J-能有若T名(可能没有)下线,除1名大头日外其余n-1 ...

  8. visual studio 2015下python编程的中文字符串问题

    visual studio 2015强大的编程功能,编写起python来也是非常方便的,但其对中文字符的支持不是很好,经常发生莫名其妙的错误,最常见的错误是不报错,也不执行代码. 代码简单如下: x= ...

  9. MongoDB学习笔记&lt;七&gt;

    继续MongoDB的学习 1.导出数据(中断其它操作) 把数据库test中的books集合导出 在终端下进行操作 mongoexport -d test -c books -o /home/hadoo ...

  10. How to: Create Custom Configuration Sections Using ConfigurationSection

    https://msdn.microsoft.com/en-us/library/2tw134k3.aspx You can extend ASP.NET configuration settings ...