矩阵乘法来进行所有路径的运算, 线段树来查询修改。 关键还是矩阵乘法的结合律。

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

Problem Description
As we all know, Harry Porter learns magic at Hogwarts School. However, learning magical knowledge alone is insufficient to become a great magician. Sometimes, Harry also has to gain knowledge from other certain subjects, such as language, mathematics, English, and even algorithm. 
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.
 
Input
They are sever test cases, you should process to the end of file.
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).
 
Output
For each query, if op equals 0, you should output one line that contains an integer indicates the number of ways from ath floor to the bth floor.
 
Sample Input
3 1
0 1 3
3 2
1 2 1 1
0 1 3
 
Sample Output
8
6
 
Source
 
 
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <algorithm>
  5. using namespace std;
  6. #define MOD 1000000007
  7. #define N 50050
  8.  
  9. struct node
  10. {
  11. __int64 g[][];
  12. };
  13. int n,m;
  14. int l[*N],r[*N];
  15. node num[*N];
  16.  
  17. void up(int s)
  18. {
  19. num[s].g[][]=;num[s].g[][]=;
  20. num[s].g[][]=;num[s].g[][]=;
  21. for(int i=;i<;i++)
  22. for(int j=;j<;j++)
  23. for(int k=;k<;k++)
  24. num[s].g[i][j] = (num[s].g[i][j]+num[*s].g[i][k]*num[*s+].g[k][j])%MOD;
  25. }
  26.  
  27. node mul(node x,node y)
  28. {
  29. node s;
  30. s.g[][]=; s.g[][]=;
  31. s.g[][]=; s.g[][]=;
  32. for(int i=;i<;i++)
  33. for(int j=;j<;j++)
  34. for(int k=;k<;k++)
  35. s.g[i][j]=( s.g[i][j]+x.g[i][k]*y.g[k][j])%MOD;
  36. return s;
  37. }
  38.  
  39. void build(int tl,int tr,int s)
  40. {
  41. l[s]=tl;
  42. r[s]=tr;
  43. if(tl==tr)
  44. {
  45. num[s].g[][]=; num[s].g[][]=;
  46. num[s].g[][]=; num[s].g[][]=;
  47. return ;
  48. }
  49. int mid = (tl+tr)/;
  50. build(tl,mid,*s);
  51. build(mid+,tr,*s+);
  52. up(s);
  53. }
  54.  
  55. void update(int x,int y,int z,int s)
  56. {
  57. if(l[s]==x&&r[s]==x)
  58. {
  59. int tx,ty;
  60. if(y==&&z==) tx=,ty=;
  61. if(y==&&z==) tx=,ty=;
  62. if(y==&&z==) tx=,ty=;
  63. if(y==&&z==) tx=,ty=;
  64. if(num[s].g[tx][ty]==)
  65. {
  66. num[s].g[tx][ty]=;
  67. }
  68. else num[s].g[tx][ty]=;
  69. return ;
  70. }
  71. int mid= (l[s]+r[s])/;
  72. if(x<=mid) update(x,y,z,*s);
  73. else update(x,y,z,*s+);
  74. up(s);
  75. }
  76.  
  77. node ask(int a,int b,int s)
  78. {
  79. if(l[s]==a&&r[s]==b)
  80. {
  81. return num[s];//这里查询的不对
  82. }
  83. int mid=(l[s]+r[s])/;
  84. if(b<=mid) return ask(a,b,*s);
  85. else if(a>mid) return ask(a,b,*s+);
  86. else
  87. {
  88. return mul(ask(a,mid,*s),ask(mid+,b,*s+));
  89. }
  90. }
  91.  
  92. int main()
  93. {
  94. while(scanf("%d%d",&n,&m)!=EOF)
  95. {
  96. build(,n-,);
  97. for(int i=;i<m;i++)
  98. {
  99. int a,b,c,d;
  100. scanf("%d%d%d",&a,&b,&c);
  101. if(a==)
  102. {
  103. node tmp=ask(b,c-,);
  104. printf("%I64d\n", (tmp.g[][]+tmp.g[][]+tmp.g[][]+tmp.g[][])%MOD );
  105. }
  106. else
  107. {
  108. scanf("%d",&d);
  109. update(b,c,d,);
  110. }
  111. }
  112. }
  113. return ;
  114. }

hdu 5068(线段树+矩阵乘法)的更多相关文章

  1. HDU 5068 Harry And Math Teacher 线段树+矩阵乘法

    题意: 一栋楼有n层,每一层有2个门,每层的两个门和下一层之间的两个门之间各有一条路(共4条). 有两种操作: 0 x y : 输出第x层到第y层的路径数量. 1 x y z : 改变第x层 的 y门 ...

  2. hdu 5068 线段树维护矩阵乘积

    http://acm.hdu.edu.cn/showproblem.php?pid=5068 题意给的略不清晰 m个询问:从i层去j层的方法数(求连段乘积)或者修改从x层y门和x+1层z门的状态反转( ...

  3. 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法

    C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...

  4. 【对不同形式矩阵的总结】WC 2009 最短路径问题(线段树+矩阵乘法)

    题意 ​ 题目链接:https://www.luogu.org/problem/P4150 ​ 一个 \(6\times n\) 的网格图,每个格点有一个初始权值.有两种操作: 修改一个格子的权值 求 ...

  5. MAZE(2019年牛客多校第二场E题+线段树+矩阵乘法)

    题目链接 传送门 题意 在一张\(n\times m\)的矩阵里面,你每次可以往左右和下三个方向移动(不能回到上一次所在的格子),\(1\)表示这个位置是墙,\(0\)为空地. 现在有\(q\)次操作 ...

  6. CF718C Sasha and Array 线段树 + 矩阵乘法

    有两个操作: 将 $[l,r]$所有数 + $x$ 求 $\sum_{i=l}^{r}fib(i)$ $n=m=10^5$   直接求不好求,改成矩阵乘法的形式:  $a_{i}=M^x\times ...

  7. Wannafly Winter Camp Day8(Div1,onsite) E题 Souls-like Game 线段树 矩阵乘法

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog @ Problem:传送门  Portal  原题目描述在最下面.  简单的 ...

  8. LOJ2980 THUSC2017大魔法师(线段树+矩阵乘法)

    线段树每个节点维护(A,B,C,len)向量,操作即是将其乘上一个矩阵. #include<iostream> #include<cstdio> #include<cma ...

  9. hdu 5068 线段树加+dp

    这题说的是 有n 层每层 有两个门 每个门 可以到达上一层的两个门,然后求从a 层到达b 层的方案总数, 不能后退, 在同一层中不能从第一个门到达另一层 我们只要我们可以对于每个 区间内 有dp[o] ...

随机推荐

  1. select * from (select user())a 为什么是查询user()的意思?

    步骤:1.先查询 select user() 这里面的语句,将这里面查询出来的数据作为一个结果集 取名为 a2.然后 再 select * from a 查询a ,将 结果集a 全部查询出来

  2. GDB基本命令(整合)(转)

    directory:添加源文件目录 l src.cpp:line_num可进入文件 如:l src.cpp:10 回车自动重复上一命令 一.gdb调试基本知识a.调试器指示的是将要执行的代码行b.只有 ...

  3. 【C++基础 03】do...while(0)妙用

    我的主题是,有时候知道一些细节会让你写出更好的代码. ============================================ 之前学coocs2d-x的时候,发现有非常多do...w ...

  4. 《windows核心编程》 18章 堆

    堆的优缺点: 优点:让我们专心解决手头问题,不必理会分配粒度和页边界这类事情. 缺点:分配和释放内存块的速度比其他方式慢,而且也无法对物理存储器的调拨和撤销进行直接控制. 什么是堆: 堆就是一块预订的 ...

  5. Ant 编译项目资源不足

    http://www.cnblogs.com/interboy/archive/2008/07/15/1243265.html今天用ant编译项目出现 [javac] 系统资源不足.的错误,如下 Bu ...

  6. Cocos2dx报OpenGL error 0x0506错误

    近期做第三方sdk接入时,发现iOS8系统下,进行银联充值后,返回游戏有很大几率会报 OpenGL error 0x0506............ 之类的绘制问题,游戏卡死,花了很长时间,一直没有头 ...

  7. JS倒计时效果

    [html] <div id="time"></div> <script> var pad = function(num){ return nu ...

  8. CentOS 6.3下部署LVS(NAT)+keepalived实现高性能高可用负载均衡(转)

    一.简介 VS/NAT原理图: 二.系统环境 实验拓扑: 系统平台:CentOS 6.3 Kernel:2.6.32-279.el6.i686 LVS版本:ipvsadm-1.26 keepalive ...

  9. C++语言基础(8)-引用

    (重要)使用引用的一些注意点: 1.引用不能绑定临时数据,也不能绑定任何无法获取内存地址的常量,表达式,或值,常引用除外. 第一种写法:(错误) int func_int(){ ; return n; ...

  10. 159. Find Minimum in Rotated Sorted Array 【medium】

    159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...