Problem Description
Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries on the string.
There are two types of queries:
1. Flipping the bits (i.e., changing all 1 to 0 and 0 to 1) between l and r (inclusive).
2. Counting the number of distinct subsequences in the substring S[l,...,r].
 
Input
The first line contains an integer T, denoting the number of the test cases.
For each test, the first line contains two integers N and Q.
The second line contains the string S.
Then Q lines follow, each with three integers type, l and r, denoting the queries.

1≤T≤5
1≤N,Q≤105
S[i]∈{0,1},∀1≤i≤N
type∈{1,2}
1≤l≤r≤N

 
Output
For each query of type 2, output the answer mod (109+7) in one line.
 
首先考虑怎么求一个01串有多少种不同的子序列
dp[i][0]表示考虑到第i位时以0结尾的不同的子序列个数
dp[i][1]表示考虑到第i位时以1结尾的不同的子序列个数
若第i+1位为1,则有:
以01为结尾的子序列个数为dp[i][0]
以11为结尾的子序列个数为dp[i][1]
只有一个1的子序列个数为1
以0为结尾的子序列个数为dp[i][0]
 
以上4种情况统计了考虑到i+1处时所有的子序列
于是有 dp[i+1][1]=dp[i][0]+dp[i][1]+1
   dp[i+1][0]=dp[i][0]
            (1  1  0
(dp[i][0],dp[i][1],1)* 0  1  0   =(dp[i+1][0],dp[i+1][1],1)
             0  1  1)
 记为A矩阵。
若i+1位为0同理有:    
    dp[i+1][1]=dp[i][0]
    dp[i+1][0]=dp[i][0]+dp[i][1]+1
    对应矩阵为(记为B矩阵)
          1  0  0                          
          1  1  0                          
          1  0  1                          
其次考虑优化的问题。将以上的两种转移视为矩阵,用线段树维护矩阵的乘积即可。
 
对于将所有0换成1,1换成0的操作而言,等价于将所有A矩阵换成B,B换成A,而A和B通过交换1,2行及1,2列可互相转换,或者说,乘以初等矩阵Fs,t  ,该矩阵的逆为自身。(记其为F)

                                                            0  1  0     
                                                            1  0  0

  

                                                          0  0  1
 
 
于是有FAFFAF……FBF……=FAAB……F,或者说,将大量0换成1,1换成0的时候只需要在乘积最外面进行一次交换1,2行与1,2列的操作。
 
然而。。仍然TLE..估计是被卡了常数。心塞。

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int N,Q;
const long long int mo=1e9+;
char s[];
int lazy[<<];
struct Matrix{
int n,m;
long long a[][];
Matrix (){clear();}
void clear(){
n=m=;
memset(a,,sizeof(a));
}
Matrix operator *(const Matrix &b) const{
Matrix tmp;
for (int i=;i<n;++i)
for (int j=;j<b.m;++j)
for (int k=;k<m;++k)
tmp.a[i][j]=(tmp.a[i][j]+a[i][k]*b.a[k][j])%mo;
return tmp;
}
};
Matrix A0,A1,E;
Matrix cnt[<<];
inline void init()
{
A0.a[][]=,A0.a[][]=,A0.a[][]=;
A0.a[][]=,A0.a[][]=,A0.a[][]=;
A0.a[][]=,A0.a[][]=,A0.a[][]=; A1.a[][]=,A1.a[][]=,A1.a[][]=;
A1.a[][]=,A1.a[][]=,A1.a[][]=;
A1.a[][]=,A1.a[][]=,A1.a[][]=; E.a[][]=,E.a[][]=,E.a[][]=;
E.a[][]=,E.a[][]=,E.a[][]=;
E.a[][]=,E.a[][]=,E.a[][]=;
}
inline void Pushup(int rt)
{
cnt[rt]=cnt[rt<<]*cnt[rt<<|];
}
inline void build(int l,int r,int rt)
{
if(l==r)
{
if(s[l-]-''==)
{
cnt[rt]=A0;
}
else cnt[rt]=A1;
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
Pushup(rt);
}
inline void change(Matrix &X)
{
swap(X.a[][],X.a[][]);
swap(X.a[][],X.a[][]);
swap(X.a[][],X.a[][]);
} inline void pushdown(int rt)
{
if(lazy[rt])
{
change(cnt[rt<<]);
change(cnt[rt<<|]);
lazy[rt<<]^=;
lazy[rt<<|]^=;
lazy[rt]=;
}
}
inline void update(int a,int b,int l,int r,int rt)
{
if(l>=a&&r<=b)
{
change(cnt[rt]);
lazy[rt]^=;
return;
}
pushdown(rt);
int m=(l+r)>>;
if(a<=m) update(a,b,lson);
if(b>m) update(a,b,rson);
Pushup(rt);
}
inline void Input()
{
scanf("%d%d",&N,&Q);
scanf("%s",s);
}
inline Matrix query(int a,int b,int l,int r,int rt)
{
if(l>=a&&r<=b) return cnt[rt];
pushdown(rt);
Matrix t1=E,t2=E;
int m=(r+l)>>;
if(a<=m) t1=query(a,b,lson);
if(b>m) t2=query(a,b,rson);
return t1*t2;
}
int main()
{
//freopen("in.txt","r",stdin);
int T,type,l,r;
scanf("%d",&T);
init();
rep(t,,T)
{
Input();
build(,N,);
rep(i,,Q)
{
scanf("%d%d%d",&type,&l,&r);
if(type==)
{
update(l,r,,N,);
// rep(j,1,2*N) printf("i=%d dp%d=%lld\n",i,j,(cnt[j].a[2][0]+cnt[j].a[2][1])%mo);
}
else
{
Matrix tmp;
tmp=query(l,r,,N,);
printf("%lld\n",(tmp.a[][]+tmp.a[][])%mo);
}
}
}
return ;
}
 
代码如下:

Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵的更多相关文章

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

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

  2. [动态dp]线段树维护转移矩阵

    背景:czy上课讲了新知识,从未见到过,总结一下. 所谓动态dp,是在动态规划的基础上,需要维护一些修改操作的算法. 这类题目分为如下三个步骤:(都是对于常系数齐次递推问题) 1先不考虑修改,不考虑区 ...

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

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

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

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

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

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

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

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

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

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

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

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

  9. Codeforces 834D The Bakery【dp+线段树维护+lazy】

    D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...

随机推荐

  1. lombok @EqualsAndHashCode 注解的影响

    官方文档:@EqualsAndHashCode 原文中提到的大致有以下几点: 1. 此注解会生成equals(Object other) 和 hashCode()方法. 2. 它默认使用非静态,非瞬态 ...

  2. [转载]Python使用@property装饰器--getter和setter方法变成属性

    原贴:为什么Python不需要getter和setter getter 和 setter在java中被广泛使用.一个好的java编程准则为:将所有属性设置为私有的,同时为属性写getter和sette ...

  3. 在js中if条件为null/undefined/0/NaN/""表达式时,统统被解释为false,此外均为true

    Boolean 表达式 一个值为 true 或者 false 的表达式.如果需要,非 Boolean 表达式也可以被转换为 Boolean 值,但是要遵循下列规则: 所有的对象都被当作 true. 当 ...

  4. PyCharm+Scrapy爬取安居客楼盘信息

    一.说明 1.1 开发环境说明 开发环境--PyCharm 爬虫框架--Scrapy 开发语言--Python 3.6 安装第三方库--Scrapy.pymysql.matplotlib 数据库--M ...

  5. js--事件冒泡-捕获

    什么是事件流: 事件流描述的是从页面中接受事件的顺序,但有意思的是,微软(IE)和网景(Netscape)开发团队居然提出了两个截然相反的事件流概念, IE的事件流是事件冒泡流(event bubbl ...

  6. laravel查找某个类拥有的方法:

    1.在当前项目下,使用cmd窗口,输入: php artisan tinker 在输入: app('log') 显示出:Illuminate\Log\Writer 2.在phpstorm中按:shif ...

  7. MAVEN 创建 JAR项目

    从Maven模板创建一个项目 在终端或命令提示(windows)中,浏览到要创建JAVA项目的文件夹下 键入如下命令: mvn archetype:generate -DgroupId={projec ...

  8. 容错机制和熔断(Hystrix)

    雪崩效应 在微服务架构中,由于服务众多,通常会涉及多个服务层级的调用,而一旦基础服务发生故障,很可能会导致级联故障,进而造成整个系统不可用,这种现象被称为服务雪崩效应.服务雪崩效应是一种因“服务提供者 ...

  9. [转载] JAVA面试题和项目面试核心要点精华总结(想进大公司必看)

    JAVA面试题和项目面试核心要点精华总结(想进大公司必看) JAVA面试题和项目面试核心要点精华总结(想进大公司必看)

  10. 读书笔记 C# 接口之浅析

    一.接口可以包含 属性.方法.事件和索引器: 二.接口不能被实例化: 三.一个类可以继承多个接口: 四.接口不能包含方法的实现: 五.继承接口的类必须实现接口中所有成员: 六.显式实现接口的成员,不能 ...