题意: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. 【MYSQL】DDL语句

    介绍:DDL语句,即数据定义语句,定义了不同的数据段,数据库表.表.列.索引等数据库对象:例如,create.drop.alter 适用对象:一般是由数据库管理员DBA使用 1.连接数据库 mysql ...

  2. 手机QQ空间自动点赞登录

    学以致用~使用 Appium 模拟人类操控手机行为 V2.0在手机上运行,目前实现以下功能: 1.小黑屋模式,一分钟内给好友发100条消息然后进了好友的小黑屋 2.定时发消息提醒对象多喝热水~ 3.对 ...

  3. Flask源码流程分析(一)

    Flask源码流程分析: 1.项目启动: 1.实例化Flask对象 1. 重要的加载项: * url_rule_class = Rule * url_map_class = Map * session ...

  4. 抛弃 .NET 经典错误:object null reference , 使用安全扩展方法? 希望对大家有帮助---Bitter.Frame 引用类型的安全转换

    还是一样,我不喜欢长篇大论,除非关乎我设计思想领域的文章.大家过来看,都是想节省时间,能用白话表达的内容,绝不长篇大论.能直接上核心代码的,绝不上混淆代码. 长期从事 .NET 工作的人都知道..NE ...

  5. (003)每日SQL学习:普通视图和物化视图

    关于这一点一直就是很懵懂的状态,今天特意网上查了一下资料,以下摘抄网上比较好的答案.以作记录. 普通视图和物化视图的区别答曰:普通视图和物化视图根本就不是一个东西,说区别都是硬拼到一起的,首先明白基本 ...

  6. STM32 定时器详细篇(基于HAL库)

    l  16位的向上.向下.向上/向下(中心对齐)计数模式,支持自动重装载 l  16位的预分频器 l  每个定时器都有多个独立通道,每个通道可用于 *  输入捕获 *  输出比较 *  PWM输出 * ...

  7. 洛谷P3501

    Description 对于一个 \(0/1\) 串,如果取反后再将整个串反过来和原串一样,就称作「反对称」字符串 给出一个长度为 \(n\) 的 \(0/1\) 串,求它有多少个反对称子串 Solu ...

  8. Java并发练习

    1.按顺序打印ABC 三个线程,每个线程分别打印A,B,C各十次,现在要求按顺序输出A,B,C package concurrency; import java.util.concurrent.Exe ...

  9. Golang之如何(优雅的)比较两个未知结构的json

    这是之前遇到的一道面试题,后来也确实在工作中实际遇到了.于是记录一下,如何(优雅的)比较两个未知结构的json. 假设,现在有两个简单的json文件. { "id":1, &quo ...

  10. Mac变卡顿,优化性能

    当调整窗口大小,同时按住"Option"键,可以从中央调整大小 同时按住"Shift"键时,可以按比例调整大小.同时按住这两个键,那么既成比例,又从中央调整大小 ...