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. P2168 [NOI2015]荷马史诗

    题目描述 追逐影子的人,自己就是影子 ——荷马 Allison 最近迷上了文学.她喜欢在一个慵懒的午后,细细地品上一杯卡布奇诺,静静地阅读她爱不释手的<荷马史诗>.但是由<奥德赛&g ...

  2. BZOJ1982 [Spoj 2021]Moving Pebbles 【博弈论】

    题目 Moving Pebbles Two players play the following game. At the beginning of the game they start with ...

  3. BZOJ3648 寝室管理 【点分治 + 环套树】

    3648: 寝室管理 Time Limit: 40 Sec  Memory Limit: 512 MB Submit: 366  Solved: 152 [Submit][Status][Discus ...

  4. 【NOIP 模拟赛】Evensgn 剪树枝 树形dp

    由于树规做的少所以即使我考试想出来正确的状态也不会转移. 一般dp的转移不那么繁杂(除了插头.....),即使多那也是清晰明了的,而且按照树规的一般思路,我们是从下到上的,所以我们要尽量简洁地从儿子那 ...

  5. There is an overlap in the region chain修复

    ERROR: (region day_hotstatic,860010-2355010000_20140417_12_entry_00000000321,1400060700465.fda3b0aca ...

  6. Prepare and Deploy Windows Server 2016 Active Directory Federation Services

    https://docs.microsoft.com/en-us/windows/security/identity-protection/hello-for-business/hello-key-t ...

  7. c++虚析构函数的必要性

    我们知道,用C++开发的时候,用来做基类的类的析构函数一般都是虚函数. 可是,为什么要这样做呢?下面用一个小例子来说明: #include<iostream> using namespac ...

  8. CSS垂直居中小结

    1.设置子元素: { ... position :absolute; margin:auto; top:; right:; bottom:; left:; } 2.设置子元素:(height必须是固定 ...

  9. lesson 1

    1.当前只是开始接触,安装并开始熟悉Visual Studio 的操作界面及基本设置 2.学习了新建项目,简单的hello world,及对颜色的更改.

  10. Nim博弈(nim游戏)

    http://blog.csdn.net/qiankun1993/article/details/6765688 NIM 游戏 重点结论:对于一个Nim游戏的局面(a1,a2,...,an),它是P- ...