codeforces750E New Year and Old Subsequence 矩阵dp + 线段树
思路:
先看一个大牛的题解
题解里面对矩阵的构造已经写的很清楚了,其实就是因为在每个字符串都有固定的很多中状态,刚好可以用矩阵来表达,所以$(i,j)$这种状态可以通过两个相邻的矩阵的$min(i,k)+(k,j)$得到,取最小值即可,由于这是一个区间问题,所以用线段树来维护区间的矩阵运算,这个运算就是取min的过程。
虽然这道原题被出在2019icpc南昌网络赛中了,但这个做法以前确实没有遇见过,开阔了思路。
代码和博客里的其实几乎一样。
#pragma GCC optimize (2)
#pragma G++ optimize (2)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<unordered_map>
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define dep(i,b,a) for(int i=b;i>=a;--i)
#define clr(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define pii pair<int,int >
using namespace std;
typedef long long ll;
ll rd()
{
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 int maxn = 2e5+;
int n,m;
char s[maxn];
struct matrix{
int a[][];
matrix(){clr(a,0x3f);}
matrix operator *(matrix x){
matrix res;
rep(i,,){
rep(j,,){
rep(k,,){
res.a[i][j]=min(res.a[i][j],a[i][k]+x.a[k][j]);
}
}
}
return res;
}
};
struct Tree{
int l,r;
matrix M;
}t[maxn<<];
void pushup(int x){
t[x].M=t[x<<].M*t[x<<|].M;
}
void build(int x,int l,int r){
t[x].l=l,t[x].r=r;int mid=(l+r)>>;
if(l == r){
for(int i = ; i < ; i++) t[x].M.a[i][i] = ;
if(s[l] == '') t[x].M.a[][] = , t[x].M.a[][] = ;
if(s[l] == '') t[x].M.a[][] = , t[x].M.a[][] = ;
if(s[l] == '') t[x].M.a[][] = , t[x].M.a[][] = ;
if(s[l] == '') t[x].M.a[][] = , t[x].M.a[][] = ;
if(s[l] == '') t[x].M.a[][] = , t[x].M.a[][] = ;
return;
}
build(x<<,l,mid),build(x<<|,mid+,r);
pushup(x);
}
matrix query(int x,int l,int r){
if(l<=t[x].l&&t[x].r<=r)return t[x].M;
int mid=(t[x].l+t[x].r)>>;
if(r<=mid)return query(x<<,l,r);
if(l>mid)return query(x<<|,l,r);
return query(x<<,l,r)*query(x<<|,l,r);
}
int main(){
int x,y;
cin>>n>>m>>(s+);
build(,,n);
rep(i,,m){
scanf("%d%d",&x,&y);
matrix ans=query(,x,y);
printf("%d\n",ans.a[][]>n?-:ans.a[][]);
}
}
codeforces750E New Year and Old Subsequence 矩阵dp + 线段树的更多相关文章
- ZOJ 3349 Special Subsequence 简单DP + 线段树
同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...
- 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] ...
- Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)
Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- cf834D(dp+线段树区间最值,区间更新)
题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- 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 6155 Subsequence Count(矩阵乘法+线段树+基础DP)
题意 给定一个长度为 \(n\) 的 \(01\) 串,完成 \(m\) 种操作--操作分两种翻转 \([l,r]\) 区间中的元素.求区间 \([l,r]\) 有多少个不同的子序列. \(1 \le ...
- Special Subsequence(离散化线段树+dp)
Special Subsequence Time Limit: 5 Seconds Memory Limit: 32768 KB There a sequence S with n inte ...
随机推荐
- CSS2 从入门到精通
1. 常用的选择器 1. 元素选择器 作用:通过元素选择器可以选择指定的元素 语法:tag{} p{ color: red; } h1{ color: red; } 2. id 选择器 作用:通过元素 ...
- CodeForces - 337D 树形dp
题意:一颗树上有且仅有一只恶魔,恶魔会污染距离它小于等于d的点,现在已经知道被污染的m个点,问恶魔在的可能结点的数量. 容易想到,要是一个点到(距离最远的两个点)的距离都小于等于d,那么这个点就有可能 ...
- DLL 本地定义符号*****在函数****中导入
把_DLL_ELOGEVENT 定义到你的工程预编译宏中 IPCLASSDLL_DLL https://bbs.csdn.net/topics/300140279
- Manacher模板(O(n)内求最长回文串长度)
转自:https://segmentfault.com/a/1190000008484167 /* 由于回文分为偶回文(比如 bccb)和奇回文(比如 bcacb),而在处理奇偶问题上会比较繁琐,所以 ...
- 批量修改root密码
公司有五十多台服务器.每台服务器中使用的密码完全不同,同时操作系统也不一样,centos5,6,7 .ubuntu,windows都有,更不用提其中各种小版本. root密码定期更改是一个大问题(wi ...
- 对AngularJs的简单了解
一.简单介绍 AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也可以觉得 ...
- Ubuntu下终端命令安装sublime
Ubuntu下终端命令安装sublime出现软件包无法定位 sublime-text-install 且多次换源不成功 建议采用离线安装 安装教程如下 用Ubuntu上的浏览器下载一个 Sublime ...
- 关于Web前端密码加密是否有意义的总结
关于Web前端密码加密是否有意义的总结 : https://blog.csdn.net/hla199106/article/details/45114801 个人:加密涉及到的是前后端的数 ...
- SQL语句常用优化技巧
提高SQL语句的执行效率,最常见的方法就是建立索引,以及尽量避免全表扫描. ①.避免在where子句中使用 is null 或 is not null 对字段进行判断. 如:select id fro ...
- 带头节点的单链表-------C语言实现
/***************************************************** Author:Simon_Kly Version:0.1 Date:20170520 De ...