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 ...
随机推荐
- Python爬虫有道翻译接口
import urllib.request import urllib.parse import json import hashlib from datetime import datetime i ...
- HTML标签(二)
一.字体标签 字体标签包含:h1~h6.<font>.<u>.<b>.<strong><em>.<sup>.<sub> ...
- MapReduce- 数据的排序处理
MapReduce- 数据的排序处理 package com.huhu.day02; import java.io.DataInput; import java.io.DataOutput; impo ...
- java旅程(一) 配置环境
(一)安装java JDK 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151. ...
- Qt绘制文本二 弯曲排列和旋转效果 弧形路径 正弦函数路径
void WgtText::paintEvent(QPaintEvent *event) { QPainter painter(this); QString m_string("abcdef ...
- POJ 2243 Knight Moves(BFS)
POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...
- 中国队再创佳绩,IOI2018喜获四金
第30届国际信息学奥林匹克竞赛(IOI2018)于9月1日-8日在日本筑波举行,共有来自87个国家(地区)的335名选手参赛. 中国代表队四名选手经过努力拼搏,获得金牌.其中,杨懋龙(湖南长沙市 ...
- flask使用配置文件
引入配置 app = Flask(__name__) app.config.from_pyfile('config.py') config.py DEBUG = True SECRET_KEY = '
- Guns后台管理系统框架(毕业设计神器)
Guns后台管理系统, 基于Spring Boot + Maven构建 + MyBatis + MySql数据库 导入Eclipse即可使用 十分钟即可搞定,做毕业设计的好帮手啊 最终效果图 登陆页 ...
- Spring、SpringMVC、Hibernate详细整合实例,包含所有步骤
Eclipse完整工程如下 Jar包如下 CSDN下载地址:https://download.csdn.net/download/zhutouaizhuwxd/9721062 其中,整个工程主要可以分 ...