Harry And Math Teacher
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 76 Accepted Submission(s): 17
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 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 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 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 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).
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
题意:N层楼梯,每层有2个门,上下两层门四条路互通。M种操作,op0: a b 询问从a楼到b楼有多少种走法。 op2:x y z x楼的y门和(x+1)楼的z门之间的楼梯状态改变(可走 /不可走)。
分析:
我们可以把第i层跟第i+1层之间楼梯的通断性构造成一个2*2的通断性矩阵,1表示通,0表示不通。那么从第a层到第b层,就是将a到b-1的通断性矩阵连乘起来,然后将得到的答案矩阵上的每个元素加起来即为方案数。想到矩阵的乘法是满足结合律的,那么我们可以用线段树来维护矩阵的乘积。每次我们只会修改某一个楼梯的通断性,所以就只是简单的线段树单点更新,成段求乘积而已。
整体复杂度
2∗2∗2∗nlogn
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007LL;
#define N 50100
LL a[N<<2][2][2];
void mul(LL x[2][2], LL y[2][2])
{
LL z[2][2] = {0,0,0,0};
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
z[i][j] = (z[i][j]+x[i][k]*y[k][j])%MOD;
memcpy(x,z,sizeof(z));
}
void build(int l, int r, int rt)
{
if(l==r)
{
a[rt][0][0] = a[rt][0][1] = a[rt][1][0] = a[rt][1][1] = 1;
return ;
}
int m = l+r>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
for(int i=0;i<2;i++)
for(int j = 0;j<2;j++)
a[rt][i][j] = a[rt<<1][i][j];
mul(a[rt],a[rt<<1|1]);
}
void query(LL s[2][2], int L, int R, int l, int r, int rt)
{
if(L<=l && R>=r)
{
mul(s,a[rt]);
return;
}
int m = l+r >>1;
if(L<=m) query(s,L,R,l,m,rt<<1);
if(R>m) query(s,L,R,m+1,r,rt<<1|1);
}
void update(int floor, int x, int y, int l, int r, int rt)
{
if(l==r)
{
a[rt][x][y]^=1;
return;
}
int m = l + r >>1;
if(floor<=m) update(floor,x,y,l,m,rt<<1);
else update(floor,x,y,m+1,r,rt<<1|1);
for(int i=0;i<2;i++)
for(int j = 0;j<2;j++)
a[rt][i][j] = a[rt<<1][i][j];
mul(a[rt],a[rt<<1|1]);
}
int main()
{
int i,j,k,m,n;
while(scanf("%d %d",&n,&m)==2)
{
int op,l,r,x,y,z;
build(1,n,1);
while(m--)
{
scanf("%d",&op);
if(op==0)
{
scanf("%d%d",&l,&r); l++;
LL s[2][2] = { 1,0,0,1};
query(s,l,r,1,n,1);
LL ans = (s[0][0]+s[0][1]+s[1][0]+s[1][1])%MOD;
printf("%I64d\n",ans);
}
else
{
scanf("%d%d%d",&x,&y,&z);
update(x+1,--y,--z,1,n,1);
}
}
}
return 0;
}