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] ...
随机推荐
- acm之简单博弈 Nim Bash Wythoff
前些日子我打算开了博弈基础,事后想进行总结下 一句话就是分析必胜或必败,异或为0. 以下内容来自转载: Nim游戏的概述: 还记得这个游戏吗?给出n列珍珠,两人轮流取珍珠,每次在某一列中取至少1颗珍珠 ...
- hdu2043
#include <stdio.h> #include <string.h> char sign[]={'A','B','C','D','E','F','G','H','I', ...
- POJ 1543 Perfect Cubes
Perfect Cubes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12595 Accepted: 6707 De ...
- 源码分析 脱壳神器ZjDroid工作原理
0. 神器ZjDroid Xposed框架的另外一个功能就是实现应用的简单脱壳,其实说是Xposed的作用其实也不是,主要是模块编写的好就可以了,主要是利用Xposed的牛逼Hook技术实现的,下面就 ...
- django-Ajax发送POST请求-csrf跨站请求的三种方式
第一种 <script> $(".eq").on("click",function () { $.ajax({ url:"/eq/&quo ...
- *hdu5632Rikka with Array
$n \leq 10^300$,问所有$i<j$且$f_i>f_j$的$(i,j),1 \leq i \leq n,1 \leq j \leq n$数量.对某个数取模. $f(a,b,0/ ...
- iOS 设置RGB色的宏
转自:http://lizhuang.iteye.com/blog/1931768 //RGB Color macro #define UIColorFromRGB(rgbValue) [UICol ...
- Objective-C NSString的常用用法
//1.创建常量字符串. NSString *astring = @"This is a String!"; //2.创建空字符串,给予赋值. NSString *astrin ...
- Codeforces 667D World Tour【最短路+枚举】
垃圾csdn,累感不爱! 题目链接: http://codeforces.com/contest/667/problem/D 题意: 在有向图中找到四个点,使得这些点之间的最短距离之和最大. 分析: ...
- T1077 多源最短路 codevs
http://codevs.cn/problem/1077/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知n个点(n&l ...