hdu6155

题意

给出一个只由 \(01\) 组成的字符串 \(s\),有两种操作:

  1. 翻转区间 \([l, r]\)
  2. 查询区间 \([l, r]\) 有多少不同的子串

分析

首先考虑怎么统计区间有多少不同的子串。

\(dp[i][0]\) 表示以 \(s[i]=0\) 结尾的字符串的个数,\(dp[i][1]\) 同理。

若 \(s[i]=0\),有状态转移方程:\(dp[i + 1][0] = dp[i][0] + dp[i][1] + 1\),\(dp[i+1][1]=dp[i][1]\)。

\(dp[i][1]\) 同理。

那么答案就是 \(dp[len][0]+dp[len][1]\) 。

可以用矩阵递推:

\[\begin{bmatrix} dp[i][0] & dp[i][1] & 1 \end{bmatrix} * \begin{bmatrix} 1 & 0 & 0\\ 1 & 1 & 0\\ 1 & 0 & 1 \end{bmatrix} = \begin{bmatrix} dp[i + 1][0] & dp[i + 1][1] & 1 \end{bmatrix}
\]

又矩阵存在结合律,所以一段区间的查询,只需要求右边一系列乘数的乘积即可。

我们可以使用线段树,去区间求积。

对于翻转操作,先将第一列和第二列交换,再将第一行和第二行交换。因为 \(01\) 是相对的,只需要交换对应的值即可。

可以在线段树中标记是否需要翻转某个区间。

看完题解写完后,猛然发现,这竟是一道线段树区间更新区间求和的“模板题”。

code

#include<bits/stdc++.h>
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 7;
struct Matrix {
ll mat[3][3];
void init() {
memset(mat, 0, sizeof mat);
}
Matrix operator *(Matrix A) {
Matrix B;
B.init();
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
for(int k = 0; k < 3; k++) {
B.mat[i][j] += mat[i][k] * A.mat[k][j];
}
B.mat[i][j] %= MOD;
}
}
return B;
}
};
char s[MAXN];
int flip[MAXN << 2];
Matrix sum[MAXN << 2];
inline void magic(Matrix& A) { // 先将第一列和第二列交换,再将第一行和第二行交换
for(int i = 0; i < 3; i++) swap(A.mat[i][0], A.mat[i][1]);
for(int i = 0; i < 2; i++) swap(A.mat[0][i], A.mat[1][i]);
}
inline void pushUp(int rt) {
sum[rt] = sum[rt << 1] * sum[rt << 1 | 1];
}
inline void pushDown(int rt) {
if(flip[rt]) {
flip[rt << 1] ^= flip[rt];
flip[rt << 1 | 1] ^= flip[rt];
magic(sum[rt << 1]);
magic(sum[rt << 1 | 1]);
flip[rt] = 0;
}
}
void build(int l, int r, int rt) {
flip[rt] = 0;
if(l == r) {
Matrix& A = sum[rt];
if(s[l] == '0') {
A = Matrix{1, 0, 0, 1, 1, 0, 1, 0, 1};
} else {
A = Matrix{1, 1, 0, 0, 1, 0, 0, 1, 1};
}
return;
}
int m = (l + r) / 2;
build(lson);
build(rson);
pushUp(rt);
}
void update(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
flip[rt] ^= 1;
magic(sum[rt]);
return;
}
pushDown(rt);
int m = (l + r) / 2;
if(L <= m) update(L, R, lson);
if(R > m) update(L, R, rson);
pushUp(rt);
}
Matrix query(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
return sum[rt];
}
pushDown(rt);
int m = (l + r) / 2;
Matrix A;
A.init();
for(int i = 0; i < 3; i++) A.mat[i][i] = 1;
if(L <= m) A = A * query(L, R, lson);
if(R > m) A = A * query(L, R, rson);
pushUp(rt);
return A;
}
//适用于正整数
template <class T>
inline void scan_d(T &ret) {
char c; ret=0;
while((c=getchar())<'0'||c>'9');
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
}
int main() {
int T;
scanf("%d", &T);
int n, q;
while(T--) {
scanf("%d%d", &n, &q);
scanf("%s", s + 1);
build(1, n, 1);
while(q--) {
int type, l, r;
scan_d(type); scan_d(l); scan_d(r);
if(type == 1) update(l, r, 1, n, 1);
else {
Matrix A = query(l, r, 1, n, 1);
printf("%lld\n", (A.mat[2][0] + A.mat[2][1]) % MOD);
}
}
}
return 0;
}

hdu6155的更多相关文章

  1. [HDU6155]Subsequence Count(线段树+矩阵)

    DP式很容易得到,发现是线性递推形式,于是可以矩阵加速.又由于是区间形式,所以用线段树维护. https://www.cnblogs.com/Miracevin/p/9124511.html 关键在于 ...

  2. [HDU6155]Subsequence Count

    题目大意: 给定一个01序列,支持以下两种操作: 1.区间反转: 2.区间求不同的子序列数量. 思路: 首先我们考虑区间反转,这是一个经典的线段树操作. 接下来考虑求不同的子序列数量,在已知当前区间的 ...

  3. 洛谷T21776 子序列

    题目描述 你有一个长度为 nn 的数列 \{a_n\}{an​} ,这个数列由 0,10,1 组成,进行 mm 个的操作: 1~l~r1 l r :把数列区间 [l, r][l,r] 内的所有数取反. ...

随机推荐

  1. CF985F Isomorphic Strings

    题目描述 You are given a string s s s of length n n n consisting of lowercase English letters. For two g ...

  2. CF#366 704D Captain America 上下界网络流

    CF上的题,就不放链接了,打开太慢,直接上题面吧: 平面上有n个点, 第 i 个点的坐标为 ($X_i ,Y_i$), 你需要把每个点染成红色或者蓝色, 染成红色的花费为 r , 染成蓝色的花费为 b ...

  3. AOJ.559 丢失的数字

    丢失的数字 Time Limit: 1000 ms Memory Limit: 64 MB Total Submission: 1552 Submission Accepted: 273 Descri ...

  4. 如何开始创建第一个基于Spring MVC的Controller

    万事开头难,良好的开端是成功的一半! 以下示例怎么开始创建我们的第一个Spring MVC控制器Controller 1.新建一个java类,命名为:MyFirstController,包含以下代码, ...

  5. 7月20号day12总结

    今天学习过程和小结 先进行了复习,主要 1,hive导入数据的方式有 本地导入  load data [local] inpath 'hdfs-dir' into table tablename; s ...

  6. Ubuntu系统iptables规则的查看和清除

    系统不支持service iptables restart,service iptables status,如何查看与清除iptable的规则呢? 一 iptables查看基本语法 iptables ...

  7. PowerShell官方文档

    PowerShell PowerShell 在 .NET Framework 基础之上构建,是一种基于任务的命令行 Shell 脚本语言:专门面向系统管理员和高级用户,可快速自动化多个操作系统(Lin ...

  8. 转:深入理解javascript原型和闭包系列

    转自:深入理解javascript原型和闭包系列 从下面目录中可以看到,本系列有16篇文章,外加两篇后补的,一共18篇文章.写了半个月,从9月17号开始写的.每篇文章更新时,读者的反馈还是可以的,虽然 ...

  9. MySQL中大于等于小于等于的写法

    由于在mybatis框架的xml中<= , >=解析会出现问题,编译报错,所以需要转译 第一种写法: 原符号 < <= > >= & ' " 替换 ...

  10. loj6087 毒瘤题

    传送门:https://loj.ac/problem/6087 [题解] 这垃圾题目卡空间啊... k=1相信大家都会,把所有数异或起来就是答案了. 考虑k=2,把所有数异或起来得到两个答案数的异或值 ...