题意:01串,操作1:把l r区间的0变1,1变0;操作2:求出l r区间的子序列种数

思路:设DP[i][j]为到i为止以j结尾的种数,假设j为0,那么dp[i][0] = dp[i - 1][1] + dp[i -1][0] (0结尾新串) + dp[i - 1][0] (0结尾旧串) - dp[i - 1][0] (重复) + 1(0本身被重复时去除)。

那么可以得到转移时的矩阵

$$ \left( \begin{matrix} dp[i - 1][0] & dp[i - 1][1] & 1 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \end{matrix} \right) * \left( \begin{matrix} 1 & 0 & 0 \\ 1 &1 & 0 \\ 1 & 0 & 1 \end{matrix} \right)  =  \left( \begin{matrix} dp[i][0] & dp[i][1] & 1 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \end{matrix} \right) $$

那么我们只要用线段树维护连续的矩阵乘积就行了。

如果翻转,那么存在一个规律,可以打表找出,直接实现连续区间矩阵乘积的翻转。

代码:

#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + 5;
const int M = 50 + 5;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
const ll MOD = 1000000007;
char str[maxn];
struct Mat{
ll s[3][3];
void init(){
memset(s, 0, sizeof(s));
}
};
Mat Mamul(Mat a, Mat b){
Mat ret;
ret.init();
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
for(int k = 0; k < 3; k++){
ret.s[i][j] = (ret.s[i][j] + a.s[i][k] * b.s[k][j]) % MOD;
}
}
}
return ret;
}
Mat mul[maxn << 2];
int lazy[maxn << 2];
void is(Mat &a, char s){
if(s == '0'){
a.s[0][0] = 1, a.s[0][1] = 0, a.s[0][2] = 0;
a.s[1][0] = 1, a.s[1][1] = 1, a.s[1][2] = 0;
a.s[2][0] = 1, a.s[2][1] = 0, a.s[2][2] = 1;
}
else{
a.s[0][0] = 1, a.s[0][1] = 1, a.s[0][2] = 0;
a.s[1][0] = 0, a.s[1][1] = 1, a.s[1][2] = 0;
a.s[2][0] = 0, a.s[2][1] = 1, a.s[2][2] = 1;
}
}
void change(Mat &a){
swap(a.s[0][0], a.s[1][1]);
swap(a.s[1][0], a.s[0][1]);
swap(a.s[2][0], a.s[2][1]);
}
void pushdown(int rt){
if(lazy[rt]){
lazy[rt << 1] ^= lazy[rt];
lazy[rt << 1 | 1] ^= lazy[rt];
change(mul[rt << 1]);
change(mul[rt << 1 | 1]);
lazy[rt] = 0;
}
}
void pushup(int rt){
mul[rt] = Mamul(mul[rt << 1], mul[rt << 1 | 1]);
}
void build(int l, int r, int rt){
lazy[rt] = 0;
if(l == r){
is(mul[rt], str[l]);
return;
}
int m = (l + r) >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
pushup(rt);
}
void update(int L, int R, int l, int r, int rt){
if(L <= l && R >= r){
lazy[rt] ^= 1;
change(mul[rt]);
return;
}
pushdown(rt);
int m = (l + r) >> 1;
if(L <= m)
update(L, R, l, m, rt << 1);
if(R > m)
update(L, R, m + 1, r, rt << 1 | 1);
pushup(rt);
}
Mat query(int L, int R, int l, int r, int rt){
if(L <= l && R >= r){
return mul[rt];
}
pushdown(rt);
int m = (l + r) >> 1;
Mat ret;
ret.init();
for(int i = 0; i < 3; i++)
ret.s[i][i] = 1;
if(L <= m)
ret = Mamul(ret, query(L, R, l, m, rt << 1));
if(R > m)
ret = Mamul(ret, query(L, R, m + 1, r, rt << 1 | 1));
pushup(rt);
return ret;
}
int main(){
// Mat a, b;
// is(a, '0'), is(b, '1');
// a = Mamul(Mamul(Mamul(a, b), b), b);
// for(int i = 0; i < 3; i++){
// for(int j = 0; j < 3; j++){
// printf("%d ", a.s[i][j]);
// }
// puts("");
// }
// printf("\n\n\n\n");
//
// is(a, '1'), is(b, '0');
// a = Mamul(Mamul(Mamul(a, b), b), b);
// for(int i = 0; i < 3; i++){
// for(int j = 0; j < 3; j++){
// printf("%d ", a.s[i][j]);
// }
// puts("");
// }
// printf("\n\n\n\n");
int T;
scanf("%d", &T);
while(T--){
int n, q;
scanf("%d%d", &n, &q);
scanf("%s", str + 1);
build(1, n, 1);
while(q--){
int op, l, r;
scanf("%d%d%d", &op, &l, &r);
if(op == 1){
update(l, r, 1, n, 1);
}
else{
Mat a;
a.init();
a.s[0][2] = 1;
a = Mamul(a, query(l, r, 1, n, 1));
ll ans = (a.s[0][0] + a.s[0][1]) % MOD;
printf("%lld\n", ans);
}
}
}
return 0;
}

HDU 6155 Subsequence Count(矩阵 + DP + 线段树)题解的更多相关文章

  1. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6155 Subsequence Count 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6155 题意: 题解来自:http://www.cnblogs.com/iRedBean/p/73982 ...

  2. HDU 6155 Subsequence Count(矩阵乘法+线段树+基础DP)

    题意 给定一个长度为 \(n\) 的 \(01\) 串,完成 \(m\) 种操作--操作分两种翻转 \([l,r]\) 区间中的元素.求区间 \([l,r]\) 有多少个不同的子序列. \(1 \le ...

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

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

  4. HDU 6155 Subsequence Count (DP、线性代数、线段树)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=6155 题解 DP+线代好题.(考场上过多时间刚前两题,没怎么想这题--) 首先列出一个DP式: 设\( ...

  5. codeforces750E New Year and Old Subsequence 矩阵dp + 线段树

    题目传送门 思路: 先看一个大牛的题解 题解里面对矩阵的构造已经写的很清楚了,其实就是因为在每个字符串都有固定的很多中状态,刚好可以用矩阵来表达,所以$(i,j)$这种状态可以通过两个相邻的矩阵的$m ...

  6. hdu 6155 - Subsequence Count

    话说这题比赛时候过的好少,连题都没读TOT 先考虑dp求01串的不同子序列的个数. dp[i][j]表示用前i个字符组成的以j为结尾的01串个数. 如果第i个字符为0,则dp[i][0] = dp[i ...

  7. HDU.6155.Subsequence Count(线段树 矩阵)

    题目链接 首先考虑询问[1,n]怎么做 设 f[i][0/1]表示[1,i]以0/1结尾的不同子序列个数 则 \(if(A[i]) f[i][1] = f[i-1][0] + f[i-1][1] + ...

  8. ZOJ 3349 Special Subsequence 简单DP + 线段树

    同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...

  9. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

随机推荐

  1. 阿里云VOD(二)

    一.准备工作 1.设置不转码 测试之前设置默认"不转码",以节省开发成本 2.找到子账户的AccessKey ID 3.给子账户添加授权 AliyunVODFullAccess 4 ...

  2. 阿里云镜像仓库镜像迁移至私有Harbor

    下载镜像同步工具 wget https://goodrain-delivery.oss-cn-hangzhou.aliyuncs.com/boe/image-syncer && chm ...

  3. 转 Fiddler1 简单使用

    Fiddler1 简单使用   文章转自:https://www.cnblogs.com/zhengna/p/9008014.html   1.Fiddler下载地址:https://www.tele ...

  4. 超详细oracle 11g安装步骤 win版本

    1. 打开网址: https://edelivery.oracle.com 使用oracle 任意账号登录 账号:2696671285@qq.com 密码:Oracle123 感谢来自某位好心大佬的共 ...

  5. day132:2RenMJ:MJ需求文档&MJ游戏流程&Egret白鹭引擎安装&TypeScript简要介绍

    目录 1.麻将产品需求文档 2.麻将游戏流程 3.Egret白鹭引擎 4.TypeScript简要了解 5.TypeScript快速入门 1.麻将产品需求文档 1.麻将术语 1.名词术语 牌⼦: 序数 ...

  6. 符号表 symbol table 符号 地址 互推

    https://zh.wikipedia.org/wiki/符号表 https://en.wikipedia.org/wiki/Symbol_table 在计算机科学中,符号表是一种用于语言翻译器(例 ...

  7. 请你尽量全面的说一个对象在 JVM 内存中的结构?

    从 Java 14 开始,Project Valhala引入了 Value Type(或者称为 inline type),参考: Valhalla: https://openjdk.java.net/ ...

  8. 非Windows系统 如何解压带中文密码和中文文件名的zip压缩文件

    数据科学交流群,群号:189158789 ,欢迎各位对数据科学感兴趣的小伙伴的加入! 一.安装unar软件包: Linux(Debian系列): apt install unarLinux(RedHa ...

  9. Hadoop伪分布式环境搭建+Ubuntu:16.04+hadoop-2.6.0

    Hello,大家好 !下面就让我带大家一起来搭建hadoop伪分布式的环境吧!不足的地方请大家多交流.谢谢大家的支持 准备环境: 1, ubuntu系统,(我在16.04测试通过.其他版本请自行测试, ...

  10. 从ReentrantLock实现非公平锁的源码理解AQS中的CLH队列

    虽然前面也看过AQS的文章,并且转载过一篇大佬的分析,但是我觉得他们对于AQS和ReentrantLock部分的源码的分析并不详细,自己理解期来还是有问题,于是自己准备花时间重新梳理下,好了,进入正题 ...