题目描述

A string tt is called nice if a string "2017" occurs in tt as a subsequence but a string "2016" doesn't occur in tt as a subsequence. For example, strings "203434107" and "9220617" are nice, while strings "20016", "1234" and "20167" aren't nice.

The ugliness of a string is the minimum possible number of characters to remove, in order to obtain a nice string. If it's impossible to make a string nice by removing characters, its ugliness is -1−1 .

Limak has a string ss of length nn , with characters indexed 11 through nn . He asks you qq queries. In the ii-th query you should compute and print the ugliness of a substring (continuous subsequence) of ssstarting at the index a_{i}ai​ and ending at the index b_{i}bi​ (inclusive).

输入输出格式

输入格式:

The first line of the input contains two integers nn and qq ( 4<=n<=2000004<=n<=200000 , 1<=q<=2000001<=q<=200000 ) — the length of the string ss and the number of queries respectively.

The second line contains a string ss of length nn . Every character is one of digits '0'–'9'.

The ii -th of next qq lines contains two integers a_{i}ai​ and b_{i}bi​ ( 1<=a_{i}<=b_{i}<=n1<=ai​<=bi​<=n ), describing a substring in the ii -th query.

输出格式:

For each query print the ugliness of the given substring.

输入输出样例

输入样例#1:

8 3
20166766
1 8
1 7
2 8
输出样例#1:

4
3
-1
输入样例#2:

15 5
012016662091670
3 4
1 14
4 15
1 13
10 15
输出样例#2:

-1
2
1
-1
-1
输入样例#3:

4 2
1234
2 4
1 2
输出样例#3:

-1
-1

说明

In the first sample:

  • In the first query, ugliness(ugliness( "20166766" )=4)=4 because all four sixes must be removed.
  • In the second query, ugliness(ugliness( "2016676" )=3)=3 because all three sixes must be removed.
  • In the third query, ugliness(ugliness( "0166766" )=-1)=−1 because it's impossible to remove some digits to get a nice string.

In the second sample:

  • In the second query, ugliness(ugliness( "01201666209167" )=2)=2 . It's optimal to remove the first digit '2' and the last digit '6', what gives a string "010166620917", which is nice.
  • In the third query, ugliness(ugliness( "016662091670" )=1)=1 . It's optimal to remove the last digit '6', what gives a nice string "01666209170".

-------------------------------------------------------------------

这是一个大坑

先把代码丢在这里,改天详细写个题解 233333

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define N 200100
#define INF 1e9
#define lc (p<<1)
#define rc (p<<1|1)
using namespace std;
int n,m;
char s[N];
struct node
{
int a[][];
node(){
for(int i=;i<;i++)
for(int j=;j<;j++)
a[i][j]=INF;
}
}T[N<<];
char ch[]={'','','','',''};
int find (char x)//返回数字本身的愚蠢办法
{
for(int i=;i<;i++)
if(x==ch[i]) return i;
return -;
}
node pushup(node &a,node &b){
node res;
for(int i=;i<;i++)
for(int j=i;j<;j++)
for(int k=i;k<=j;k++)
res.a[i][j]=min(res.a[i][j],a.a[i][k]+b.a[k][j]);
return res;
}
void build(int p,int l,int r)
{
if(l==r)
{
int f=find(s[l]);
for(int i=;i<;i++)
T[p].a[i][i]=;//初始化——隔壁有更好的方法 //以下为转移方程初始化
if(f!=-&&f<)//l的值为2 0 1 7
{
T[p].a[f][f+]=;
T[p].a[f][f]=;
}
else if(f==)//l为6
T[p].a[][]=T[p].a[][]=;
return;
}
int mid=(l+r)>>;
build(lc,l,mid);
build(rc,mid+,r);
T[p]=pushup(T[lc],T[rc]);
}
node query(int p,int l,int r,int ql,int qr)
{
if(ql==l&&qr==r)
return T[p];
int mid=(l+r)>>;
if(qr<=mid) return query(lc,l,mid,ql,qr);
if(ql>mid) return query(rc,mid+,r,ql,qr);
else
{
node tmpl=query(lc,l,mid,ql,mid);
node tmpr=query(rc,mid+,r,mid+,qr);
return pushup(tmpl,tmpr);
}
}
int main()
{
scanf("%d%d",&n,&m);
scanf("%s",s+);
build(,,n);
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
int ans=query(,,n,l,r).a[][];
if(ans==INF) printf("-1\n");
else printf("%d\n",ans);
}
return ;
}

CF750E 线段树+矩阵乘矩阵加的更多相关文章

  1. 「模板」 线段树——区间乘 && 区间加 && 区间求和

    「模板」 线段树--区间乘 && 区间加 && 区间求和 原来的代码太恶心了,重贴一遍. #include <cstdio> int n,m; long l ...

  2. UOJ#299. 【CTSC2017】游戏 线段树 概率期望 矩阵

    原文链接www.cnblogs.com/zhouzhendong/p/UOJ299.html 前言 不会概率题的菜鸡博主做了一道概率题. 写完发现运行效率榜上的人都没有用心卡常数——矩阵怎么可以用数组 ...

  3. ZOJ - 2671 Cryptography(线段树+求区间矩阵乘积)

    题意:已知n个矩阵(下标从1开始),求下标x~y区间矩阵的乘积.最多m次询问,n ( 1 <= n <= 30,000) and m ( 1 <= m <= 30,000). ...

  4. poj 3468 A Simple Problem with Integers (线段树 成段更新 加值 求和)

    题目链接 题意: 只有这两种操作 C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.&quo ...

  5. POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  6. hdu 5068 线段树维护矩阵乘积

    http://acm.hdu.edu.cn/showproblem.php?pid=5068 题意给的略不清晰 m个询问:从i层去j层的方法数(求连段乘积)或者修改从x层y门和x+1层z门的状态反转( ...

  7. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并

    D. Vika and Segments     Vika has an infinite sheet of squared paper. Initially all squares are whit ...

  8. BZOJ 4085 丧心病狂的毒瘤题目 线段树+矩乘

    思路: 一眼矩阵快速幂 再用线段树维护一下矩阵就完了... 我hhhhh    哎我还是too young,too simple 入了这个大坑 线段树维护9个值 以上 如果A+1   转移矩阵是这个样 ...

  9. bzoj1018[SHOI2008]堵塞的交通traffic——线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1018 巧妙的线段树.维护矩阵四个角的连通性. 考虑两个点连通的可能路径分成3部分:两点左边. ...

随机推荐

  1. 小目标 | DAX高级实践-Power BI与Excel联合应用

    · 适用人群:数据分析专业人士,在数据分析方向需求发展人士 · 应用场景:数据汇报.数据可视化展现.数据建模分析 · 掌握难度:★★★★☆ 本期讲师 DAX高级实践-Power BI与Excel联合应 ...

  2. 一键部署joomla开源内容管理平台

    https://market.azure.cn/Vhd/Show?vhdId=10896&version=12949 产品详情 产品介绍Joomla是一套自由.开放源代码的内容管理系统,以PH ...

  3. 一键部署基于GitLab的自托管Git项目仓库

    https://market.azure.cn/Vhd/Show?vhdId=9851&version=11921 产品详情 产品介绍GitLab https://about.gitlab.c ...

  4. HTML_3

    html列表 有序列表:在网页上定义一个有编号的内容列表可以用<ol>.<li>配合使用来实现,在网页上生成的列表,每条项目上按1.2.3编号,有序列表在实际开发中较少使用.代 ...

  5. Centos7.3 安装devstack stein版本

    1. 系统准备 # 关闭防火墙 systemctl stop firewalld systemctl disable firewalld # 关闭selinux setenforce 0 sed -i ...

  6. 安装JDK1.8以及配置环境变量的步骤

    一. 首先到官网下载jdk1.8,下载的版本分为windows和linux,这里需要安装操作系统进行下载.我的是64位就下载x64,32位系统则下载x86 二. 然后就是安装,双击进行安装,这里不用更 ...

  7. Ubuntu编译Android源码过程中的空间不足解决方法

    Android源码一般几十G,就拿Android5.0来说,下载下来大概也有44G左右,和编译产生的文件以及Ubuntu系统占用的空间加起来,源码双倍的空间都不够有.编译源码前能分配足够的空间再好不过 ...

  8. POP简单动画简单使用 (入门级别)

    动画可以让APP“更友好”的与用户交互,苹果提供很多的好看的动画供开发者使用,不过简单的平移.旋转.缩放.......使用起来很简单,但是想要进行一些比较复杂的动画效果,使用起来就比较难以实现,俗话说 ...

  9. OpenCV3.42+VS2017配置+模块计算机类型“X86”与目标计算机类型“x64”冲突”的问题解决

    目录 OpenCV3.42+VS2017配置 Visual Studio 2017 第三方依赖设置,附加依赖项和附加库目录 "fatal error LNK1112: 模块计算机类型&quo ...

  10. 机器学习(一)之KNN算法

    knn算法原理 ①.计算机将计算所有的点和该点的距离 ②.选出最近的k个点 ③.比较在选择的几个点中那个类的个数多就将该点分到那个类中 KNN算法的特点: knn算法的优点:精度高,对异常值不敏感,无 ...