Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵
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
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int N,Q;
const long long int mo=1e9+;
char s[];
int lazy[<<];
struct Matrix{
int n,m;
long long a[][];
Matrix (){clear();}
void clear(){
n=m=;
memset(a,,sizeof(a));
}
Matrix operator *(const Matrix &b) const{
Matrix tmp;
for (int i=;i<n;++i)
for (int j=;j<b.m;++j)
for (int k=;k<m;++k)
tmp.a[i][j]=(tmp.a[i][j]+a[i][k]*b.a[k][j])%mo;
return tmp;
}
};
Matrix A0,A1,E;
Matrix cnt[<<];
inline void init()
{
A0.a[][]=,A0.a[][]=,A0.a[][]=;
A0.a[][]=,A0.a[][]=,A0.a[][]=;
A0.a[][]=,A0.a[][]=,A0.a[][]=; A1.a[][]=,A1.a[][]=,A1.a[][]=;
A1.a[][]=,A1.a[][]=,A1.a[][]=;
A1.a[][]=,A1.a[][]=,A1.a[][]=; E.a[][]=,E.a[][]=,E.a[][]=;
E.a[][]=,E.a[][]=,E.a[][]=;
E.a[][]=,E.a[][]=,E.a[][]=;
}
inline void Pushup(int rt)
{
cnt[rt]=cnt[rt<<]*cnt[rt<<|];
}
inline void build(int l,int r,int rt)
{
if(l==r)
{
if(s[l-]-''==)
{
cnt[rt]=A0;
}
else cnt[rt]=A1;
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
Pushup(rt);
}
inline void change(Matrix &X)
{
swap(X.a[][],X.a[][]);
swap(X.a[][],X.a[][]);
swap(X.a[][],X.a[][]);
} inline void pushdown(int rt)
{
if(lazy[rt])
{
change(cnt[rt<<]);
change(cnt[rt<<|]);
lazy[rt<<]^=;
lazy[rt<<|]^=;
lazy[rt]=;
}
}
inline void update(int a,int b,int l,int r,int rt)
{
if(l>=a&&r<=b)
{
change(cnt[rt]);
lazy[rt]^=;
return;
}
pushdown(rt);
int m=(l+r)>>;
if(a<=m) update(a,b,lson);
if(b>m) update(a,b,rson);
Pushup(rt);
}
inline void Input()
{
scanf("%d%d",&N,&Q);
scanf("%s",s);
}
inline Matrix query(int a,int b,int l,int r,int rt)
{
if(l>=a&&r<=b) return cnt[rt];
pushdown(rt);
Matrix t1=E,t2=E;
int m=(r+l)>>;
if(a<=m) t1=query(a,b,lson);
if(b>m) t2=query(a,b,rson);
return t1*t2;
}
int main()
{
//freopen("in.txt","r",stdin);
int T,type,l,r;
scanf("%d",&T);
init();
rep(t,,T)
{
Input();
build(,N,);
rep(i,,Q)
{
scanf("%d%d%d",&type,&l,&r);
if(type==)
{
update(l,r,,N,);
// rep(j,1,2*N) printf("i=%d dp%d=%lld\n",i,j,(cnt[j].a[2][0]+cnt[j].a[2][1])%mo);
}
else
{
Matrix tmp;
tmp=query(l,r,,N,);
printf("%lld\n",(tmp.a[][]+tmp.a[][])%mo);
}
}
}
return ;
}
Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵的更多相关文章
- DP+线段树维护矩阵(2019牛客暑期多校训练营(第二场))--MAZE
题意:https://ac.nowcoder.com/acm/contest/882/E 给你01矩阵,有两种操作:1是把一个位置0变1.1变0,2是问你从第一行i开始,到最后一行j有几种走法.你只能 ...
- [动态dp]线段树维护转移矩阵
背景:czy上课讲了新知识,从未见到过,总结一下. 所谓动态dp,是在动态规划的基础上,需要维护一些修改操作的算法. 这类题目分为如下三个步骤:(都是对于常系数齐次递推问题) 1先不考虑修改,不考虑区 ...
- Codeforces 750E - New Year and Old Subsequence(线段树维护矩阵乘法,板子题)
Codeforces 题目传送门 & 洛谷题目传送门 u1s1 我做这道 *2600 的动力是 wjz 出了道这个套路的题,而我连起码的思路都没有,wtcl/kk 首先考虑怎样对某个固定的串计 ...
- Codeforces 1368H - Breadboard Capacity(最小割+线段树维护矩阵乘法)
Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 首先看到这种从某一种颜色 ...
- 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(线段树维护矩阵)
题解 (不会矩阵加速的先去学矩阵加速) 反正我想不到线段树维护矩阵.我太菜了. 我们在线段树上维护一个区间的斐波那契的列矩阵的和. 然后询问时提取每个符合题意列矩阵的答案项(不是列矩阵存了两项吗,一个 ...
- HDU 6155 Subsequence Count 线段树维护矩阵
Subsequence Count Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 256000/256000 K (Java/Oth ...
- Codeforces 834D The Bakery【dp+线段树维护+lazy】
D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...
随机推荐
- centos命令行系列之centos6防火墙的关闭以及开启
输入:cat /etc/issue 查看版本 (一)通过service命令 注:service命令开启以及关闭防火墙为即时生效,下次重启机器的时候会自动复原 查看防火墙状态:service ipt ...
- volatile原理解析
Java并发编程:volatile关键字解析 volatile 有序性.可见性 volatile可以保证一定程度上有序性,即volatile前面的代码先于后面的代码先执行. 但是前.后代码,各自里面的 ...
- 记录一次面试中的HTTP请求相关问题
1.HTTP中的状态码分别代表什么 比较基础,自行百度. 2.HTTP请求,响应头部content-length用来做什么的 答:Content-Length首部告诉浏览器报文中实体主体的大小.这 ...
- CSS3 的box-shadow进阶之 - 动画篇 - 制作辐射动画
本篇文章是上一篇讲box-shadow基础知识的延伸,建议先花几分钟阅读那篇文章,点击阅读,再来看这篇. 除了box-shadow属性知识外,制作动画,还需要对CSS3的animation, @key ...
- Weka中数据挖掘与机器学习系列之数据格式ARFF和CSV文件格式之间的转换(五)
不多说,直接上干货! Weka介绍: Weka是一个用Java编写的数据挖掘工具,能够运行在各种平台上.它不仅提供了可以直接用于数据挖掘的软件,还提供了src代码,使用者可以修改源代码,进行二次开发. ...
- Win10系列:UWP界面布局进阶4
在开发Windows应用商店应用程序时,可以为页面中的界面元素添加快捷菜单,并设置与其相关的菜单项,用户通过选择快捷菜单中的菜单项来执行与被选择对象相关的操作.下面通过一个示例来介绍如何为页面中的一张 ...
- bzoj1294
题解: 首先发现假如一个豆豆被多边形围住了,那么从这个豆豆引出一条射线 会有奇数个焦点 然后我们从每个豆豆引出一条射线 然后状压dfs 代码: #include<bits/stdc++.h> ...
- zookeeper:springboot+dubbo配置zk集群并测试
1.springboot配置zk集群 1.1:非主从配置方法 dubbo: registry: protocol: zookeeper address: ,, check: false 1.2:主从配 ...
- 【原创】<Debug> not positioned on a valid record
[Problem] QSqlQuery::value: not positioned on a valid record QSqlQuery :: value:未定位在有效记录上 [Solution] ...
- centos 安装git服务器,配置使用证书登录并你用hook实现代码自动部署
安装git服务器先安装依赖软件:yum -y install gcc zlib-devel openssl-devel perl cpio expat-devel gettext-devel open ...