hdu 5068 Harry And Math Teacher ( 线段树+矩阵乘法 )
来源:互联网 发布:linux mount nfs目录 编辑:程序博客网 时间:2024/05/18 06:23
Harry And Math Teacher
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 475 Accepted Submission(s): 139
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.
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 andm(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 integersx,y,z(1≤x<n,1≤y,z≤2) follow, it means that the stair between the xth floor’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).
For each test case, there are two integers n and
Output
For each query, if op equals 0, you should output one line that contains an integer indicates the number of ways fromath floor to the bth floor.
Sample Input
3 10 1 33 21 2 1 10 1 3
Sample Output
86
Source
BestCoder Round #14
题目大意:每个楼层有两个门,各有通向下一层两个门的一条路
初始路全部都通,可以破坏或恢复一条路,统计从一层到另一层的走法的总数
题目大意:每个楼层有两个门,各有通向下一层两个门的一条路
初始路全部都通,可以破坏或恢复一条路,统计从一层到另一层的走法的总数
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#define size 3#define MOD 1000000007#define MAX 50007using namespace std;typedef long long LL;int n,m;struct mat{ LL a[size][size]; mat ( ) { memset ( a , 0 , sizeof ( a ) ); }};void print ( mat a );mat multi ( mat m1 , mat m2 ){ mat ret; for ( int i = 1 ; i < size ; i++ ) for ( int j = 1 ; j < size ; j++ ) if ( m1.a[i][j] ) for ( int k = 1 ; k < size ; k++ ) { ret.a[i][k] += (m1.a[i][j]*m2.a[j][k])%MOD; ret.a[i][k] %= MOD; } return ret;}struct Tree{ int l,r; mat m;}tree[MAX<<2];void push_up ( int u ){ tree[u].m = multi ( tree[u<<1].m , tree[u<<1|1].m );}void build ( int u , int l , int r ){ tree[u].l = l , tree[u].r = r; if ( l == r ) { tree[u].m.a[1][1] = tree[u].m.a[1][2] = 1; tree[u].m.a[2][1] = tree[u].m.a[2][2] = 1; return; } int mid = l + r >>1; build ( u<<1 , l , mid ); build ( u<<1|1 , mid+1 , r ); push_up ( u );}void update ( int u , int x , int y , int z ){ int l = tree[u].l , r = tree[u].r; if ( l == r ) { tree[u].m.a[y][z] ^= 1; return; } int mid = l + r >> 1; if ( x > mid ) update ( u<<1|1 , x , y , z ); else update ( u<<1 , x , y , z ); push_up ( u );}void set ( mat &m ){ for ( int i = 1 ; i < size ; i++ ) m.a[i][i] = 1;}mat query ( int u , int left , int right ){ int l = tree[u].l , r = tree[u].r; if ( left <= l && r <= right ) return tree[u].m; int mid = l + r >>1; mat ret; set ( ret ); // print ( ret ); if ( left <= mid && right >= l ) ret = multi ( ret , query ( u<<1 , left , right ) ); if ( left <= r && right > mid ) ret = multi ( ret , query ( u<<1|1 , left , right ) ); return ret;}void print ( mat m ){ cout << "--------------------------------" << endl; for ( int i = 1 ; i < size ; i ++ ) { for ( int j = 1 ; j < size ; j++ ) printf ( "%lld " , m.a[i][j] ); puts ( "" ); } cout << " --------------------------------" << endl;}int main ( ){ while ( ~scanf ( "%d%d" , &n , &m ) ) { build ( 1 , 1 , n ); // printf ( "%lld %lld\n" , tree[1].m.a[1][1] , tree[1].m.a[1][2] ); // printf ( "%lld %lld\n" , tree[2].m.a[1][1] , tree[2].m.a[1][2] ); // printf ( "%lld %lld\n" , tree[3].m.a[1][1] , tree[3].m.a[1][2] ); int a,b,c,d; for ( int i = 0 ; i < m ; i++ ) { scanf ( "%d" , &a ); if ( a ) { scanf ( "%d%d%d" , &b , &c , &d ); update ( 1 , b , c , d ); // printf ( "%lld %lld\n" , tree[1].m.a[1][1] , tree[1].m.a[1][2] ); // printf ( "%lld %lld\n" , tree[2].m.a[1][1] , tree[2].m.a[1][2] ); // printf ( "%lld %lld\n" , tree[3].m.a[1][1] , tree[3].m.a[1][2] ); } else { scanf ( "%d%d" , &b , &c ); mat ans; ans.a[1][1] = ans.a[1][2] = 1; // print ( ans ); // print ( query ( 1 , b , c-1 ) ); ans = multi ( ans , query ( 1 , b , c-1 ) ); // print ( ans ); printf ( "%lld\n" , (ans.a[1][1] + ans.a[1][2])%MOD ); } } }}
0 0
- hdu 5068 Harry And Math Teacher ( 线段树+矩阵乘法 )
- HDU 5068 Harry And Math Teacher (矩阵处理,线段树)
- hdu 5068 Harry And Math Teacher 线段树
- HDU-5068-Harry And Math Teacher(线段树)
- HDU 5068 Harry And Math Teacher 线段树维护矩阵乘积
- 【线段树】 HDOJ 5068 Harry And Math Teacher
- HDU 5068 Harry And Math Teacher
- hdu-5068 Harry And Math Teacher
- BestCoder Round #14 03 Harry And Math Teacher (线段树)
- hdu 5068 Harry And Math Teacher(BestCoder Round #14)
- 【HDU5068】Harry And Math Teacher
- HDU 5069 Harry And Biological Teacher(fail树+线段树优化)
- HDU 5069 Harry And Biological Teacher AC自动机 + 线段树优化 (BestCoder Round 14 D)
- HDU 5066 Harry And Physical Teacher 物理
- hdu 5066 Harry And Physical Teacher(数学)
- HDU 5066 Harry And Physical Teacher
- HDU 5066Harry And Physical Teacher(数学)
- HDU 5066 Harry And Physical Teacher
- Html5游戏之KiwiJs(2)-显示游戏精灵
- 使用svn进行项目版本管理
- 如何正确的给数码设备充电
- 黑马程序员——Objective-C学习之不可变与可变字典(NSDictionary\NSMutableDictionary)
- 层序遍历及其进阶版
- hdu 5068 Harry And Math Teacher ( 线段树+矩阵乘法 )
- Chrome 扩展程序 CrxMouse Techzero优化版 更新至 v3.0.4
- 【c语言】输出乘法口诀表
- VB获取本地IP、计算机名、用户名代码
- 阿拉伯语下wifi突变显示不全
- Chrome 扩展程序 OneTab Techzero优化版 v1.0.0 发布
- javascript类和原型学习笔记
- 开始iOS 7中自动布局教程(一、二)
- Chrome 扩展程序 Speed Dial 2 Techzero优化版 更新至 v1.3.0