http://acm.hdu.edu.cn/showproblem.php?pid=5068

题意给的略不清晰

m个询问:从i层去j层的方法数(求连段乘积)或者修改从x层y门和x+1层z门的状态反转(更新只需更新一个节点的矩阵)

直接贴题解

我们可以把第i层跟第i+1层之间楼梯的通断性构造成一个2*2的通断性矩阵,1表示通,0表示不通。那么从第a层到第b层,就是将a到b-1的通断性矩阵连乘起来,然后将得到的答案矩阵上的每个元素加起来即为方案数。想到矩阵的乘法是满足结合律的,那么我们可以用线段树来维护矩阵的乘积。每次我们只会修改某一个楼梯的通断性,所以就只是简单的线段树单点更新,成段求乘积而已。
整体复杂度2∗2∗2∗nlogn

线段树维护矩阵乘积

初始化时要当成所有门是完好的

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
const int maxn = 30010;
#define M 50005
#define N 11
#define P 1000000007
using namespace std;
struct node{
int L,R;
int num[2][2];
}tree[M<<2];
void up(node &A,node &B,node &C){
int i,j,k;
for(i=0;i<2;i++)
for(j=0;j<2;j++){
A.num[i][j]=0;
for(k=0;k<2;k++){
A.num[i][j]+=(1LL*B.num[i][k]*C.num[k][j])%P;
}
A.num[i][j]%=P;
}
}
void build(int L,int R,int p){
tree[p].L=L,tree[p].R=R;
if(L==R){
tree[p].num[0][0]=1; tree[p].num[0][1]=1;
tree[p].num[1][0]=1; tree[p].num[1][1]=1;
return;
}
int mid=(L+R)>>1;
build(L,mid,2*p);
build(mid+1,R,2*p+1);
up(tree[p],tree[2*p],tree[2*p+1]);
}
node query(int L,int R,int p){
if(tree[p].L==L&&tree[p].R==R){
return tree[p];
}
int mid=(tree[p].L+tree[p].R)>>1;
if(R<=mid)return query(L,R,2*p);
else if(L>mid)return query(L,R,2*p+1);
else{
node tmp1=query(L,mid,2*p);
node tmp2=query(mid+1,R,2*p+1);
node res;
up(res,tmp1,tmp2);
return res;
}
}
void update(int x,int a,int b,int p){
if(tree[p].L==tree[p].R){
tree[p].num[a][b]^=1;
return ;
}
int mid=(tree[p].L+tree[p].R)>>1;
if(x<=mid)update(x,a,b,2*p);
else update(x,a,b,2*p+1);
up(tree[p],tree[2*p],tree[2*p+1]);
}
int main(){
int n,m,i,j,k,a,b,x,y,z;
while(~RD2(n,m)){
build(1,n-1,1);
while(m--){
RD(k);
if(k==0){
RD2(a,b);
node res=query(a,b-1,1);
int ans=(1LL*res.num[0][0]+res.num[0][1]+res.num[1][0]+res.num[1][1])%P;
printf("%d\n",ans);
}else{
RD3(x,y,z);
update(x,y-1,z-1,1);
}
}
}
return 0;
}

hdu 5068 线段树维护矩阵乘积的更多相关文章

  1. Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵

    Problem Description Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries ...

  2. 线段树维护矩阵【CF718C】 Sasha and Array

    Description 有一个长为\(n\)的数列\(a_{1},a_{2}...a_{n}\),你需要对这个数列维护如下两种操作: \(1\space l \space r\space x\) 表示 ...

  3. CF718C Sasha and Array(线段树维护矩阵)

    题解 (不会矩阵加速的先去学矩阵加速) 反正我想不到线段树维护矩阵.我太菜了. 我们在线段树上维护一个区间的斐波那契的列矩阵的和. 然后询问时提取每个符合题意列矩阵的答案项(不是列矩阵存了两项吗,一个 ...

  4. Codeforces 1368H - Breadboard Capacity(最小割+线段树维护矩阵乘法)

    Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 首先看到这种从某一种颜色 ...

  5. Codeforces 750E - New Year and Old Subsequence(线段树维护矩阵乘法,板子题)

    Codeforces 题目传送门 & 洛谷题目传送门 u1s1 我做这道 *2600 的动力是 wjz 出了道这个套路的题,而我连起码的思路都没有,wtcl/kk 首先考虑怎样对某个固定的串计 ...

  6. HDU 6155 Subsequence Count 线段树维护矩阵

    Subsequence Count Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Oth ...

  7. DP+线段树维护矩阵(2019牛客暑期多校训练营(第二场))--MAZE

    题意:https://ac.nowcoder.com/acm/contest/882/E 给你01矩阵,有两种操作:1是把一个位置0变1.1变0,2是问你从第一行i开始,到最后一行j有几种走法.你只能 ...

  8. Codeforces 719E (线段树教做人系列) 线段树维护矩阵

    题面简洁明了,一看就懂 做了这个题之后,才知道怎么用线段树维护递推式.递推式的递推过程可以看作两个矩阵相乘,假设矩阵A是初始值矩阵,矩阵B是变换矩阵,求第n项相当于把矩阵B乘了n - 1次. 那么我们 ...

  9. hdu 5068(线段树+矩阵乘法)

    矩阵乘法来进行所有路径的运算, 线段树来查询修改. 关键还是矩阵乘法的结合律. Harry And Math Teacher Time Limit: 5000/3000 MS (Java/Others ...

随机推荐

  1. linux替换字符串的几种方法

    1. 基本替换:s/str1/str2/ 替换当前行第一个str1为str2:s/str1/str2/g 替换当前行所有str1为str2:n,$s/str1/str2/ 替换第 n 行开始到最后一行 ...

  2. clusterProfiler包

    1)enrichGO:(GO富集分析) 描述:GO Enrichment Analysis of a gene set. Given a vector of genes, this function ...

  3. Ubuntu下Anaconda3的安装

    1)去https://www.anaconda.com/download/#download下载Anaconda安装文件(python3.6的版本). 2)进入到Anaconda3-5.0.1-Lin ...

  4. mydqldump

    [mydqldump] One way to create a snapshot of the data in an existing master database is to use the my ...

  5. for循环语句输出菱形

    for(int a = 5; a > 0 ; a--){ for(int b = 1; b <= a; b++){ System.out.print(" "); } f ...

  6. poj3278-Catch That Cow 【bfs】

    http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

  7. Linux配置Hadoop 常用的命令

    uname -a 看主机位数 ip a 看IP地址 vi /etc/sysconfig/network 改主机的名字 vi /etc/hosts 改映射关系 vi /etc/sysconfig/net ...

  8. ECMAScript5新特性之isFrozen、freeze

    对象被冻结后: 1 不能添加属性. 2 不能删除属性. 3 不能修改属性.(赋值) 4 不能修改属性描述符.(会抛异常) var fruit = { name : '苹果', desc : '红富士' ...

  9. UVa 1595 Symmetry(set)

    We call a figure made of points is left-right symmetric as it is possible to fold the sheet of paper ...

  10. ILSpy 反编译.NET

    ILSpy 是一个开源的.NET反编译工具,简洁强大易用是它的特征.在绝大多数情况下,它都能很好的完成你对未知程序集内部代码的探索.