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. ARC075 E.Meaningful Mean(树状数组)

    题目大意:给定n和k,问an中有多少子区间的平均值大于等于k 很巧妙的一个式子,就是如果一个区间[l, r]满足条件 那么则有 sum[r] - sum[l-1] >= (r-l+1)*k 整理 ...

  2. SQL SERVER:删除筛选记录中前100条数据

    delete from table1 where id in (select top 100 id from table1)

  3. 停止ambari上服务的顺序

    Before performing any upgrades or uninstalling software, stop all of the Hadoop services in the foll ...

  4. linux内存条排查

    已发现2个内存错误,应用名称(kernel:),日志内容(hangzhou-jishuan-DDS0248 kernel: sbridge: HANDLING MCE MEMORY ERROR han ...

  5. PHP报错Cannot adopt OID in UCD-SNMP-MIB、 LM-SENSORS-MIB

    Cannot adopt OID in UCD-SNMP-MIB: Cannot adopt OID in LM-SENSORS-MIB: lmTempSensorsValue 运行PHP遇到这些错误 ...

  6. Java中Class<T>与Class<?>的区别

    E - Element (在集合中使用,因为集合中存放的是元素) T - Type(Java 类) K - Key(键) V - Value(值) N - Number(数值类型) ? - 表示不确定 ...

  7. 普通table表格样式及代码大全

     普通table表格样式及代码大全(全)(一) 单实线边框表格 <table style="border-collapse: collapse" borderColor=#0 ...

  8. 数据结构之(HDU2051 Bitset)

    Problem Description Give you a number on base ten,you should output it on base two.(0 < n < 10 ...

  9. noip2016 提高组

    T1 玩具谜题 题目传送门 这道题直接模拟就好了哇 233 #include<cstdio> #include<cstring> #include<algorithm&g ...

  10. usaco 月赛 2005 january watchcow

    2013-09-18 08:13 //By BLADEVIL var n, m :longint; pre, other :..] of longint; last :..] of longint; ...