题目传送门

思路:

  先看一个大牛的题解

  题解里面对矩阵的构造已经写的很清楚了,其实就是因为在每个字符串都有固定的很多中状态,刚好可以用矩阵来表达,所以$(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 + 线段树的更多相关文章

  1. ZOJ 3349 Special Subsequence 简单DP + 线段树

    同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...

  2. 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] ...

  3. Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

  4. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  5. cf834D(dp+线段树区间最值,区间更新)

    题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...

  6. [Codeforces 280D]k-Maximum Subsequence Sum(线段树)

    [Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...

  7. 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 ...

  8. HDU 6155 Subsequence Count(矩阵乘法+线段树+基础DP)

    题意 给定一个长度为 \(n\) 的 \(01\) 串,完成 \(m\) 种操作--操作分两种翻转 \([l,r]\) 区间中的元素.求区间 \([l,r]\) 有多少个不同的子序列. \(1 \le ...

  9. Special Subsequence(离散化线段树+dp)

    Special Subsequence Time Limit: 5 Seconds      Memory Limit: 32768 KB There a sequence S with n inte ...

随机推荐

  1. docker--环境搭建

    我的电脑是win10,虽然现在win10开始也支持docker,但在linux机器会合适些,所以我先用VMware创建一个linux虚拟机--Centos7 安装虚拟机不多说,现在开始安装docker ...

  2. Windows IIS7 下安装配置 PHP7.0

    确认Microsoft Visual C++版本 Microsoft Visual C++的版本至关重要,根据Microsoft Visual C++版本下载的PHP不同.        PHP官网的 ...

  3. imagepicker插件的使用方法和选择按钮汉化

    1,使用cordova-plugin-image-picker插件. cordova plugin add https://github.com/wymsee/cordova-imagePicker. ...

  4. shell read变量

  5. 界面设计微信小程序

    上一个周期将微信小程序的程序编完了,虽然还有很多需要修改的地方,大体已经完成.剩下的边角料以后再弄 这个周期来学习一下微信小程序的设计....好吧. 首先还是需要看一下微信的设计指南的.地址 http ...

  6. PHP-在排序数组中查找元素的第一个和最后一个位置

    给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [ ...

  7. Magento 消息提示

    Magento 消息提示 //成功 Mage::getSingleton('customer/session')->addSuccess('恭喜您关联会员卡成功!'); //失败 Mage::g ...

  8. The method setPositiveButton(int, DialogInterface.OnClickListener) in the type AlertDialog.Builder i

    参考资料: http://blog.csdn.net/competerh_programing/article/details/7377950 在创建Dialog的时候,出现: The method ...

  9. nteract 使用教程

    安装 直接去官网下载 一路回车 官网 建立python虚拟环境 和我们平时一样 不同的是在建立完之后 要安装一个kernel Using Python3 with pip and a virtual ...

  10. flask json

    导入 from flask import Flask,jsonify 1.列表 def index(): arr=['mkdir','md','touch'] return jsonify(arr) ...