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 ...
随机推荐
- Python之os.path.join()
os.path.join()函数用于路径拼接文件路径. os.path.join()函数中可以传入多个路径: 会从第一个以”/”开头的参数开始拼接,之前的参数全部丢弃. 以上一种情况为先.在上一种情况 ...
- 事件循环--eventloop
一.什么是事件循环? 事件循环是 JS 实现异步的具体解决方案,同步代码直接执行,异步函数或代码块先放在异步队列中,待同步函数执行完毕,轮询执行异步队列的函数. 事件循环 二.node.js中的事件循 ...
- SqlServer 将表里面的值赋给变量
@@c_beiz varchar(500) select top 1 @c_beiz = c_beiz from g_billd_qh_tmp where u_id=''+@uid+'' and io ...
- Java文件拷贝方式
原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11444284.html 利用java.io类库,直接为源文件构建一个FileInputStream读取 ...
- leetcode-回溯②-难题
题10: 回溯:另:动态规划复杂度更低 class Solution: def isMatch(self, s: str, p: str) -> bool: def helper(s,p): i ...
- 使用springBoot和mybatis整合时出现如下错误:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)解决方案
在pom.xml文件中添加如下: <build> <resources> <resource> & ...
- git ,报403错误,完美解决方案
首先命令行操作结果如下: root@zhiren-PowerEdge-T110-II:/zrun# git clone https://git.coding.net/xxxxxxxx/xxxx.git ...
- 【时间】Unix时间戳
UNIX时间戳:Unix时间戳(英文为Unix epoch, Unix time, POSIX time 或 Unix timestamp) 是从1970年1月1日(UTC/GMT的午夜)开始所经过的 ...
- 【操作】Linux快捷键
- 【Flutter学习】基本组件之基本表单组件
一,概述 表单时一个包含表单元素的区域. 表单元素允许用户输入内容,比如文本域,下拉列表,单选框,复选框等.常见的应用场景有:登录,注册,输入信息等. 表单里有两个重要的组件: Form:用来做整个表 ...