HDU 6155 Subsequence Count 线段树维护矩阵
Subsequence Count
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Others)
There are two types of queries:
1. Flipping the bits (i.e., changing all 1 to 0 and 0 to 1) between l and r (inclusive).
2. Counting the number of distinct subsequences in the substring S[l,...,r].
For each test, the first line contains two integers N and Q.
The second line contains the string S.
Then Q lines follow, each with three integers type, l and r, denoting the queries.
1≤T≤5
1≤N,Q≤105
S[i]∈{0,1},∀1≤i≤N
type∈{1,2}
1≤l≤r≤N
4 4
1010
2 1 4
2 2 4
1 2 3
2 1 4
4 4
0000
1 1 2
1 2 3
1 3 4
2 1 4
6
8
10
#include<bits/stdc++.h>
using namespace std;
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N=5e5+,M=1e6+,inf=; inline LL read(){
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} const LL mod = 1e9+;
char s[N]; struct Matix {
LL arr[][];
}E,F,again,EE;
inline Matix mul(Matix a,Matix b) {
Matix ans;
memset(ans.arr,,sizeof(ans.arr));
for(int i = ; i < ; i++) {
for(int j = ; j < ; j++) {
for(int k = ; k < ; k++)
ans.arr[i][j] += a.arr[i][k] * b.arr[k][j],ans.arr[i][j] %= mod;
}
}
return ans;
} Matix v[N * ],now,facE[N],facF[N];
int lazy[N * ],fi[N * ],se[N * ]; void change(int i) {
swap(v[i].arr[][],v[i].arr[][]);
swap(v[i].arr[][],v[i].arr[][]);
swap(v[i].arr[][],v[i].arr[][]);
swap(v[i].arr[][],v[i].arr[][]);
swap(v[i].arr[][],v[i].arr[][]);
swap(v[i].arr[][],v[i].arr[][]);
}
void push_down(int i,int ll,int rr) {
if(!lazy[i]) return;
lazy[ls] ^= ;
lazy[rs] ^= ;
change(ls);change(rs);
lazy[i] ^= ;
}
inline void push_up(int i,int ll,int rr) {
v[i] = mul(v[ls],v[rs]);
}
void build(int i,int ll,int rr) {
lazy[i] = ;
if(ll == rr) {
if(s[ll] == '') v[i] = E,fi[i] = ,se[i] = ;
else v[i] = F,fi[i] = ,se[i] = ;
return ;
}
build(ls,ll,mid);
build(rs,mid+,rr);
push_up(i,ll,rr);
}
inline void update(int i,int ll,int rr,int x,int y) {
push_down(i,ll,rr);
if(ll == x && rr == y) {
lazy[i] ^= ;
change(i);
return ;
}
if(y <= mid) update(ls,ll,mid,x,y);
else if(x > mid) update(rs,mid+,rr,x,y);
else update(ls,ll,mid,x,mid),update(rs,mid+,rr,mid+,y);
push_up(i,ll,rr);
}
inline Matix ask(int i,int ll,int rr,int x,int y) {
push_down(i,ll,rr);
if(ll == x && rr == y) {
return v[i];
}
if(y <= mid) return ask(ls,ll,mid,x,y);
else if(x > mid) return ask(rs,mid+,rr,x,y);
else return mul(ask(ls,ll,mid,x,mid),ask(rs,mid+,rr,mid+,y));
push_up(i,ll,rr);
} int main() {
EE.arr[][] = ,EE.arr[][] = ,EE.arr[][] = ; E.arr[][] = ;E.arr[][] = ;E.arr[][] = ;
E.arr[][] = ;E.arr[][] = ; F.arr[][] = ;F.arr[][] = ;F.arr[][] = ;
F.arr[][] = ;F.arr[][] = ; again.arr[][] = ; int T;
T = read();
while(T--) {
int n,Q;
n = read();
Q = read();
scanf("%s",s+);
build(,,n);
while(Q--) {
int op,l,r;
op = read();
l = read();
r = read();
if(op == )
update(,,n,l,r);
else {
now = mul(again,ask(,,n,l,r));
printf("%lld\n",(now.arr[][]+now.arr[][])%mod);
}
}
}
return ;
}
先考虑怎么算 s_1, s_2, \ldots, s_ns1,s2,…,sn
的答案。设 dp(i, 0/1)dp(i,0/1)
表示考虑到 s_isi
,以 0/10/1
结尾的串的数量。那么 dp(i, 0) =dp(i - 1, 0) + dp(i - 1, 1) + 1dp(i,0)=dp(i−1,0)+dp(i−1,1)+1
. 11
也同理。
那么假设在某个区间之前,dp(i, 0/1) = (x, y)dp(i,0/1)=(x,y)
的话,过了这段区间,就会变成 (ax + by + c, dx + ey + f)(ax+by+c,dx+ey+f)
的形式,只要用线段树维护这个线性变化就好了。
HDU 6155 Subsequence Count 线段树维护矩阵的更多相关文章
- 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] + ...
- Codeforces 750E - New Year and Old Subsequence(线段树维护矩阵乘法,板子题)
Codeforces 题目传送门 & 洛谷题目传送门 u1s1 我做这道 *2600 的动力是 wjz 出了道这个套路的题,而我连起码的思路都没有,wtcl/kk 首先考虑怎样对某个固定的串计 ...
- Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵
Problem Description Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries ...
- hdu 5068 线段树维护矩阵乘积
http://acm.hdu.edu.cn/showproblem.php?pid=5068 题意给的略不清晰 m个询问:从i层去j层的方法数(求连段乘积)或者修改从x层y门和x+1层z门的状态反转( ...
- 线段树维护矩阵【CF718C】 Sasha and Array
Description 有一个长为\(n\)的数列\(a_{1},a_{2}...a_{n}\),你需要对这个数列维护如下两种操作: \(1\space l \space r\space x\) 表示 ...
- CF718C Sasha and Array(线段树维护矩阵)
题解 (不会矩阵加速的先去学矩阵加速) 反正我想不到线段树维护矩阵.我太菜了. 我们在线段树上维护一个区间的斐波那契的列矩阵的和. 然后询问时提取每个符合题意列矩阵的答案项(不是列矩阵存了两项吗,一个 ...
- Codeforces 1368H - Breadboard Capacity(最小割+线段树维护矩阵乘法)
Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 首先看到这种从某一种颜色 ...
- HDU 6155 Subsequence Count(矩阵乘法+线段树+基础DP)
题意 给定一个长度为 \(n\) 的 \(01\) 串,完成 \(m\) 种操作--操作分两种翻转 \([l,r]\) 区间中的元素.求区间 \([l,r]\) 有多少个不同的子序列. \(1 \le ...
- HDU 6155 Subsequence Count(矩阵 + DP + 线段树)题解
题意: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] ...
随机推荐
- Laya List翻页滚动方案 & List滚动源码解析
Laya List翻页滚动方案 & List滚动源码解析 @author ixenos 2019-03-29 1.List翻页滚动方案 /** * 计算下一页的起始索引, 不足时补足 * @p ...
- 【UML】关联、依赖、泛化、实现等关系说明
导读:再上一篇博客中,介绍了UML的9种图,现在,将对UML中的关系进行总结.图很重要,但图形中的各种关系也很重要,这扯关系的事儿,从来都是大事儿. 一.基本定义 1.1 总体说明 1.2 具体定义 ...
- BZOJ1926 [Sdoi2010]粟粟的书架 【主席树 + 二分 + 前缀和】
题目 幸福幼儿园 B29 班的粟粟是一个聪明机灵.乖巧可爱的小朋友,她的爱好是画画和读书,尤其喜欢 Thomas H. Co rmen 的文章.粟粟家中有一个 R行C 列的巨型书架,书架的每一个位置都 ...
- iOS APP 架构漫谈[转]
Mark 一下 很不错的文章 最近看了一些有关server的东西,一些很简单的东西,不外乎是一些文档规范,另外结合最近看的wwdc的一些video,觉得对软件架构(software arch ...
- 更改App名称
To change the installed application name, in Xcode: 1. Select your Target on the left side under Gro ...
- Scrapy学习-2-xpath&css使用
xpath使用 简介 xpath使用路径表达式在xml和html中进行导航 语法 body # 选取所有body元素的所有子节点 /html # 选取根元素 body/a # 选取所有属于body ...
- ci框架——辅助函数
辅助函数:application/helper下面.命名要求为***_helper.php;这样在调用的时候直接$this->load->helper('***');若想给自定义的辅助函数 ...
- postman 快捷方式--启动图标
下载,解压,安装,(此安装位置在/opt) 1.创建全局变量,也就是在任何地方都可以执行postman,不用去到安装目录,执行 : sudo ln -s /opt/postman/Postman /u ...
- 一起来学Spring Cloud | 第五章:熔断器 ( Hystrix)
在微服务项目中,一个系统可以分割成很多个不同的服务模块,不同模块之间我们通常需要进行相互调用.springcloud中可以使用RestTemplate+Ribbon和Feign来调用(工作中基本都是使 ...
- Spark学习(三): 基本架构及原理
Apache Spark是一个围绕速度.易用性和复杂分析构建的大数据处理框架,最初在2009年由加州大学伯克利分校的AMPLab开发,并于2010年成为Apache的开源项目之一,与Hadoop和St ...