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 ...
随机推荐
- Utils--字符串的帮助类
Utils--字符串的帮助类 一个关于字符串过滤的工具类,主要时针对username和password的过滤 package com.bw.yun.utils; import java.securit ...
- Linux中环境变量文件
一.环境变量文件介绍 转自:http://blog.csdn.net/cscmaker/article/details/7261921 Linux中环境变量包括系统级和用户级,系统级的环境变量是每个登 ...
- 使用JdbcTemplate操作数据库(二十九)
使用JdbcTemplate操作数据库 Spring的JdbcTemplate是自动配置的,你可以直接使用@Autowired来注入到你自己的bean中来使用. 举例:我们在创建User表,包含属性n ...
- Object转Integer,String
object先转为字符串,然后通过int 的封装类(Integer)的pasreInt()方法转为int 例如: Object ob = 123; Integer.parseInt(String.v ...
- cpu-z for ubuntu 12.04 64bit : cpu-g
wget https://launchpad.net/~phantomas/+archive/ppa/+files/cpu-g_0.9.0_amd64.deb sudo dpkg -i cpu-g*. ...
- window7下载安装桌面版ubuntu
首先需要下载VMware Workstation 下载地址:http://pan.baidu.com/s/1qXS0rhi 秘钥:bbpn 我的环境是ubuntu-14.10-desktop-a ...
- linux下正则表达式学习
下表包含了元字符的完整列表以及它们在正则表达式上下文中的行为: 字符 描述 \ 将下一个字符标记为一个特殊字符.或一个原义字符.或一个 向后引用.或一个八进制转义符.例如,'n' 匹配字符 " ...
- day04 列表
今天主要学习了 列表 什么是列表 定义: 能装对象的对象 在python中使用[]来描述列表, 内部元素用逗号隔开. 对数据类型没有要求 列表存在索引和切片. 和字符串是一样的. 2. 相关的增删改查 ...
- 每天CSS学习之box-shadow
box-shadow是CSS3的属性,目的是给盒子添加一个或多个阴影.怎么感觉有点像光明使者使用该法术照亮敌人的阴暗面? box-shadow一共有六个属性,请看: box-shadow: h-sha ...
- 二叉排序树,Binary_Sort_Tree,C++完整实现
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...