hdu6155
hdu6155
题意
给出一个只由 \(01\) 组成的字符串 \(s\),有两种操作:
- 翻转区间 \([l, r]\)
- 查询区间 \([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]\) 。
可以用矩阵递推:
\]
又矩阵存在结合律,所以一段区间的查询,只需要求右边一系列乘数的乘积即可。
我们可以使用线段树,去区间求积。
对于翻转操作,先将第一列和第二列交换,再将第一行和第二行交换。因为 \(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的更多相关文章
- [HDU6155]Subsequence Count(线段树+矩阵)
DP式很容易得到,发现是线性递推形式,于是可以矩阵加速.又由于是区间形式,所以用线段树维护. https://www.cnblogs.com/Miracevin/p/9124511.html 关键在于 ...
- [HDU6155]Subsequence Count
题目大意: 给定一个01序列,支持以下两种操作: 1.区间反转: 2.区间求不同的子序列数量. 思路: 首先我们考虑区间反转,这是一个经典的线段树操作. 接下来考虑求不同的子序列数量,在已知当前区间的 ...
- 洛谷T21776 子序列
题目描述 你有一个长度为 nn 的数列 \{a_n\}{an} ,这个数列由 0,10,1 组成,进行 mm 个的操作: 1~l~r1 l r :把数列区间 [l, r][l,r] 内的所有数取反. ...
随机推荐
- [51nod1482]部落信号 单调栈
~~~题面~~~ 题解: 可以发现这是一道单调栈的题目,首先来考虑数字没有重复时如何统计贡献. 因为这是一个环,而如果我们从最高的点把环断开,并把最高点放在链的最后面(顺时针移动),那么因为在最高点两 ...
- org.json与json-lib的区别(补充 FastJson)
org.json 是JSON国际组织官方推出的标准json解析方案,已经被 android sdk 纳入到标准内置类库,依赖项少,但直至API17版本SDK中,仅支持JSONObject与JSONAr ...
- ubuntu12.04 Qt WebKit编译
转载自:http://my.oschina.net/u/257674/blog/167050 官方文档: http://trac.webkit.org/wiki/BuildingQtOnLinux#D ...
- 纯手工 CheckboxTree 实现
数据结构及页面显示格式: INSERT INTO AS_CombRules VALUES('', '', '', '', '', '', '') 实现 CheckboxTree 功能: html代码: ...
- 【BZOJ2832&&3874】宅男小C [模拟退火][贪心]
宅男小C Time Limit: 10 Sec Memory Limit: 256 MB[Submit][Status][Discuss] Description 众所周知,小C是个宅男,所以他的每 ...
- 51nodeE 斜率最大
题目传送门 这道题只要证明最佳解一定在相邻两个点之间的好啦 这个自己证一证就okay啦 而且我发现n方的算法可以过耶... #include<cstdio> #include<cst ...
- bzoj1053 搜索
2013-11-16 17:43 原题传送门http://www.lydsy.com/JudgeOnline/problem.php?id=1053 因为使pi(prime[i])<20亿的i不 ...
- HDU1143(3*N的地板铺1*2的砖)
Tri Tiling Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- python实战===石头剪刀布,简单模型
#石头剪刀布 import random import time win_list = [("石头","剪刀"),("布","石头 ...
- 新版谷歌浏览器设置flash插件不提示步骤
1.先去chrome实验室界面chrome://flags/#enable-ephemeral-flash-permission选择取消Disabled.取消该实验室选项. 2.然后去chrome:/ ...