hdu 5068(线段树+矩阵乘法)
矩阵乘法来进行所有路径的运算, 线段树来查询修改。 关键还是矩阵乘法的结合律。
Harry And Math Teacher
Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 326    Accepted Submission(s): 89
In Hogwarts, there is a tall castle in which all teachers live. The castle is rather special. In every floor there are two doors, behind each of them there existing two stairs to the next floor’s two doors. And if you are at the i-th floor’s j-th door , then you can just go to the next floor from this door. However, something even more interesting (or we can say "magic") can happen to the stairs: sometimes they break into pieces (making it impossible to go to the next floor), and sometimes the fragments can joint together and become the whole stair again. Now suppose Harry is in the a-th floor (you know, Harry is the hero, so he lives in the teachers’ building somehow), and his math teacher b-th floor. Sometimes the math teacher will call Harry to his room. Facing these magic stairs, Harry gets puzzled how he can go to see the math teacher. Can you help Harry figure out how many ways exactly he can choose without going backwards? You can assume that the change of the stairs will not happen when Harry is on his way. Harry can begin at any doors in floor a and he can end at any doors in floor b. And as Harry want to arrive as soon as possible, so he can not go back to the past. And at the beginning all the stairs are intact. And the answer may be too large, you should output the answer mod 1000000007.
For each test case, there are two integers n and m(2≤n≤50000,1≤m≤50000) in the first line, indicate the number of the castle’s layers and the number of queries. And the following m lines, each line contains three or four integers. If the first integer op equals 0, there are two integers a and b (1≤a<b≤n) follow, indicate the position of Harry and math teacher. Otherwise, there are three integers x,y,z(1≤x<n,1≤y,z≤2)follow, it means that the stair between the xthfloor’s yth door and the (x+1)th floor’s z-th door changes its state(if it is intact, then it breaks else it joints).
0 1 3
3 2
1 2 1 1
0 1 3
6
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MOD 1000000007
#define N 50050 struct node
{
__int64 g[][];
};
int n,m;
int l[*N],r[*N];
node num[*N]; void up(int s)
{
num[s].g[][]=;num[s].g[][]=;
num[s].g[][]=;num[s].g[][]=;
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
num[s].g[i][j] = (num[s].g[i][j]+num[*s].g[i][k]*num[*s+].g[k][j])%MOD;
} node mul(node x,node y)
{
node s;
s.g[][]=; s.g[][]=;
s.g[][]=; s.g[][]=;
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
s.g[i][j]=( s.g[i][j]+x.g[i][k]*y.g[k][j])%MOD;
return s;
} void build(int tl,int tr,int s)
{
l[s]=tl;
r[s]=tr;
if(tl==tr)
{
num[s].g[][]=; num[s].g[][]=;
num[s].g[][]=; num[s].g[][]=;
return ;
}
int mid = (tl+tr)/;
build(tl,mid,*s);
build(mid+,tr,*s+);
up(s);
} void update(int x,int y,int z,int s)
{
if(l[s]==x&&r[s]==x)
{
int tx,ty;
if(y==&&z==) tx=,ty=;
if(y==&&z==) tx=,ty=;
if(y==&&z==) tx=,ty=;
if(y==&&z==) tx=,ty=;
if(num[s].g[tx][ty]==)
{
num[s].g[tx][ty]=;
}
else num[s].g[tx][ty]=;
return ;
}
int mid= (l[s]+r[s])/;
if(x<=mid) update(x,y,z,*s);
else update(x,y,z,*s+);
up(s);
} node ask(int a,int b,int s)
{
if(l[s]==a&&r[s]==b)
{
return num[s];//这里查询的不对
}
int mid=(l[s]+r[s])/;
if(b<=mid) return ask(a,b,*s);
else if(a>mid) return ask(a,b,*s+);
else
{
return mul(ask(a,mid,*s),ask(mid+,b,*s+));
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
build(,n-,);
for(int i=;i<m;i++)
{
int a,b,c,d;
scanf("%d%d%d",&a,&b,&c);
if(a==)
{
node tmp=ask(b,c-,);
printf("%I64d\n", (tmp.g[][]+tmp.g[][]+tmp.g[][]+tmp.g[][])%MOD );
}
else
{
scanf("%d",&d);
update(b,c,d,);
}
}
}
return ;
}
hdu 5068(线段树+矩阵乘法)的更多相关文章
- HDU 5068 Harry And Math Teacher 线段树+矩阵乘法
		
题意: 一栋楼有n层,每一层有2个门,每层的两个门和下一层之间的两个门之间各有一条路(共4条). 有两种操作: 0 x y : 输出第x层到第y层的路径数量. 1 x y z : 改变第x层 的 y门 ...
 - hdu 5068 线段树维护矩阵乘积
		
http://acm.hdu.edu.cn/showproblem.php?pid=5068 题意给的略不清晰 m个询问:从i层去j层的方法数(求连段乘积)或者修改从x层y门和x+1层z门的状态反转( ...
 - 【Codeforces718C】Sasha and Array     线段树 + 矩阵乘法
		
C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...
 - 【对不同形式矩阵的总结】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 原题目描述在最下面. 简单的 ...
 - LOJ2980 THUSC2017大魔法师(线段树+矩阵乘法)
		
线段树每个节点维护(A,B,C,len)向量,操作即是将其乘上一个矩阵. #include<iostream> #include<cstdio> #include<cma ...
 - hdu 5068 线段树加+dp
		
这题说的是 有n 层每层 有两个门 每个门 可以到达上一层的两个门,然后求从a 层到达b 层的方案总数, 不能后退, 在同一层中不能从第一个门到达另一层 我们只要我们可以对于每个 区间内 有dp[o] ...
 
随机推荐
- Linux下中断程序导致写文件失败的分析
			
案例: 一个普通linux C程序,执行期间会进行多次printf操作,利用bash脚本重定向功能,将stdout重定向到一个另一个文件中去.在运行途中用ctrl+C终止程序,发现定向文件始终为空,即 ...
 - iOS 自定义转场动画浅谈
			
代码地址如下:http://www.demodashi.com/demo/11612.html 路漫漫其修远兮,吾将上下而求索 前记 想研究自定义转场动画很久了,时间就像海绵,挤一挤还是有的,花了差不 ...
 - webview漏洞 -转
			
原文链接:http://www.cnblogs.com/goodhacker/p/3343837.html 一.漏洞描述 近期,微信等多款安卓流行应用曝出高危挂马漏洞:只要点击好友消息或朋友圈中的一条 ...
 - EPH接收Event Hub Message
			
简介: 使用Python SDK,基于EPH方式接收Azure Event Hub中存储的message,EventProcessorHost()中使用Azure Storage存储offerset等 ...
 - 【转载】HTTP和SOAP完全就是两个不同的协议
			
http:是一个客户端和服务器端请求和应答的标准(TCP). http协议其目的是为了提供一种发布和接收htttp页面的方法 http协议的客户端与服务器的交互:由HTTP客户端发起一个请求,建立一个 ...
 - arm linux c++11编译
			
include(CheckCXXCompilerFlag) CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11 ...
 - FMDB(一)— 简单介绍
			
在iOS开发过程中常常会用到数据库方面的操作,但是iOS原生的SQLite API使用起来并不十分友好,对于C语言基础较薄弱的朋友来说.使用起来可能会认为比較不便.于是,一些第三方的对SQLite A ...
 - 美团HD(8)-利用NSPredicate匹配搜索结果
			
监听文本框改变: DJSelectCityViewController.m /** 当searchBar内的文字发生改变时调用此方法 */ - (void)searchBar:(UISearchBar ...
 - 四个 jQuery 方法:
			
append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插入内容 before() - 在被选元素之前插入内容
 - 福昕阅读器打开PDF文档速度慢
			
RT---------------- 操作如下两步可加快打开速度: 1.Program Files\Foxit Software\Foxit Reader下面的Shell Extensions文件夹删 ...