A string t is called nice if a string "2017" occurs in t as a subsequence but a string "2016" doesn't occur in t 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.

Limak has a string s of length n, with characters indexed 1 through n. He asks you q queries. In the i-th query you should compute and print the ugliness of a substring (continuous subsequence) of s starting at the index ai and ending at the index bi (inclusive).

Input

The first line of the input contains two integers n and q (4 ≤ n ≤ 200 000, 1 ≤ q ≤ 200 000) — the length of the string s and the number of queries respectively.

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

The i-th of next q lines contains two integers ai and bi (1 ≤ ai ≤ bi ≤ n), describing a substring in the i-th query.

Output

For each query print the ugliness of the given substring.

Examples
input
8 3
20166766
1 8
1 7
2 8
output
4
3
-1
input
15 5
012016662091670
3 4
1 14
4 15
1 13
10 15
output
-1
2
1
-1
-1
input
4 2
1234
2 4
1 2
output
-1
-1
Note

In the first sample:

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

In the second sample:

  • In the second query, ugliness("01201666209167") = 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("016662091670") = 1. It's optimal to remove the last digit '6', what gives a nice string "01666209170".

题目大意

  给定一个长度为$n$的数字串。每次询问一个区间至少要删除多少个数字使得包含子序列"2017"但不包含子序列"2016",无解输出-1。

  dp是显然的。

  因为每次询问一个区间,所以需要把dp状态扔到某个数据结构上。先考虑线段树。

  线段树更新的时候是拿两段的信息合并,所以不能像做1~n的dp那样记录状态。

  考虑2017之间的间隔:

| 2 | 0 | 1 | 7 |

0   1   2   3   4

  线段树的每个节点存一个矩阵$A$。$a_{ij}$表示使原串的子序列包含2017中第$i$个间隔到第$j$个间隔组成的子串,但不包含严格包含它的子序列最少需要删除的数字、

  转移是显然的,和区间dp一样。枚举区间,枚举中间点,然后转移就好了。

  考虑初值问题,显然的是非2、0、1、7、6的数字对答案不影响,所以令$a_{ii} = 0$,$a_{ij} = \infty \ \ \  \left ( i \neq j \right )$。

  考虑当前数字是2的时候,如果我希望只包含子串$[0, 0]$(这里表示两个间隔间的子串),那么就必须删掉这个2,故$a_{00} = 1$,如果希望包含子串$[0, 1]$,那么什么都不用做,所以$a_{01} = 0$。对于0、1、7同理。

  考虑当前数字是6的时候,那么遇到子串$[i, 3]$希望转移回自己,那么需要付出1的代价,因为否则会包含子序列"2016",同样如果遇到子串$[i, 4]$希望转移回自己,那么也需要付出1的代价。

  由于很早以前过的这道题,所以不想重写一份,代码有点丑,请谅解。

Code

 /**
* Codeforces
* Problem#750E
* Accepted
* Time: 998ms
* Memory: 49276k
*/
#include<iostream>
#include<fstream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<malloc.h>
#ifndef WIN32
#define AUTO "%lld"
#else
#define AUTO "%I64d"
#endif
using namespace std;
typedef bool boolean;
#define inf 0xfffffff
#define smin(a, b) (a) = min((a), (b))
#define smax(a, b) (a) = max((a), (b))
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) return;
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
} typedef class Matrix {
public:
int mat[][]; //0: 1:2 2:20 3:201 4:2017
Matrix(){ }
Matrix(char x){
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
mat[i][j] = (i == j) ? () : (inf);
if(x == '') mat[][] = , mat[][] = ;
else if(x == '') mat[][] = , mat[][] = ;
else if(x == '') mat[][] = , mat[][] = ;
else if(x == '') mat[][] = , mat[][] = ;
else if(x == '') mat[][] = , mat[][] = ;
} Matrix operator +(Matrix x) {
Matrix res;
for(int i = ; i < ; i++)
for(int j = ; j < ; j++){
res.mat[i][j] = inf;
for(int k = ; k < ; k++)
smin(res.mat[i][j], mat[i][k] + x.mat[k][j]);
}
return res;
}
}Matrix; typedef class SegTreeNode {
public:
Matrix a;
SegTreeNode* left, *right;
SegTreeNode():left(NULL), right(NULL){ }
SegTreeNode(char x):left(NULL), right(NULL){
a = Matrix(x);
} void pushUp() {
a = left->a + right->a;
}
}SegTreeNode; typedef class SegTree {
public:
SegTreeNode* root;
SegTree():root(NULL){ }
SegTree(int size, char* str){
build(root, , size, str);
} void build(SegTreeNode*& node, int l, int r, char* list){
if(l == r){
node = new SegTreeNode(list[l]);
return;
}
node = new SegTreeNode();
int mid = (l + r) >> ;
build(node->left, l, mid, list);
build(node->right, mid + , r, list);
node->pushUp();
} Matrix query(SegTreeNode*& node, int l, int r, int from, int end) {
if(l == from && r == end) return node->a;
int mid = (l + r) >> ;
if(end <= mid) return query(node->left, l, mid, from, end);
if(from > mid) return query(node->right, mid + , r, from, end);
return query(node->left, l, mid, from, mid) + query(node->right, mid + , r, mid + , end);
}
}SegTree; int n, q;
char* str;
SegTree st; inline void init() {
readInteger(n);
readInteger(q);
str = new char[(const int)(n + )];
scanf("%s", str + );
st = SegTree(n, str);
} inline void solve() {
int a, b;
while(q--) {
readInteger(a);
readInteger(b);
Matrix c = st.query(st.root, , n, a, b);
printf("%d\n", (c.mat[][] == inf) ? (-) : (c.mat[][]));
}
} int main() {
init();
solve();
return ;
}

Codeforces 750E New Year and Old Subsequence - 线段树 - 动态规划的更多相关文章

  1. Codeforces 750E New Year and Old Subsequence 线段树 + dp (看题解)

    New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解 ...

  2. Codeforces 750E - New Year and Old Subsequence(线段树维护矩阵乘法,板子题)

    Codeforces 题目传送门 & 洛谷题目传送门 u1s1 我做这道 *2600 的动力是 wjz 出了道这个套路的题,而我连起码的思路都没有,wtcl/kk 首先考虑怎样对某个固定的串计 ...

  3. [Codeforces 750E]New Year and Old Subsequence

    Description 题库链接 给出一个长度为 \(n\) 的仅包含数字的字符串. \(q\) 次询问,每次询问该串 \([a,b]\) 段内删去几个数能够使其不含 \(2016\) 的子串,但存在 ...

  4. Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)

    题目链接:http://codeforces.com/contest/522/problem/D 题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...

  5. Codeforces Codeforces Round #316 (Div. 2) C. Replacement 线段树

    C. ReplacementTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/570/problem ...

  6. CodeForces 587 E.Duff as a Queen 线段树动态维护区间线性基

    https://codeforces.com/contest/587/problem/E 一个序列, 1区间异或操作 2查询区间子集异或种类数 题解 解题思路大同小异,都是利用异或的性质进行转化,st ...

  7. Codeforces 558E A Simple Task (计数排序&&线段树优化)

    题目链接:http://codeforces.com/contest/558/problem/E E. A Simple Task time limit per test5 seconds memor ...

  8. Codeforces 629D Babaei and Birthday Cakes DP+线段树

    题目:http://codeforces.com/contest/629/problem/D 题意:有n个蛋糕要叠起来,能叠起来的条件是蛋糕的下标比前面的大并且体积也比前面的大,问能叠成的最大体积 思 ...

  9. Codeforces Round #406 (Div. 1) B. Legacy 线段树建图跑最短路

    B. Legacy 题目连接: http://codeforces.com/contest/786/problem/B Description Rick and his co-workers have ...

随机推荐

  1. iOS UI基础-9.2 UITableView 简单微博列表

    概述 我们要实现的效果: 这个界面布局也是UITableView实现的,其中的内容就是UITableViewCell,只是这个UITableViewCell是用户自定义实现的.虽然系统自带的UITab ...

  2. ASP.Net Core 2.2 MVC入门到基本使用系列 (一)(转)

    本教程会对基本的.Net Core 进行一个大概的且不会太深入的讲解, 在您看完本系列之后, 能基本甚至熟练的使用.Net Core进行Web开发, 感受到.Net Core的魅力. 本教程知识点大体 ...

  3. react native中使用 react-native-easy-toast 和react-native-htmlview

    第一步,下载依赖 npm install react-native-htmlview --save npm install react-native-easy-toast --save 第二步,引入 ...

  4. node.js初识02

    node.js相较于那些老的服务器语言,他的优势在于,节省了I/O的时间,主要的特点是单线程,非阻塞和事件驱动,其实三个说的是同一个事情,相较于多线程而言,单线程的特点是,使用的那一条线程的cpu的利 ...

  5. hiho #1014 : Trie树(模板)

    Trie树 [题目链接]Trie树 &题意: 输入 输入的第一行为一个正整数n,表示词典的大小,其后n行,每一行一个单词(不保证是英文单词,也有可能是火星文单词哦),单词由不超过10个的小写英 ...

  6. 关于Sublime Text3的emmet插件和tab快捷键冲突问题

    当使用Sublime text3时会遇到快捷键冲突的问题,其中就有安装Emmet之后,tab无法缩进了, 网上有些说看看Browse Packages目录下是否有PyV8插件安装,该插件一般情况下随E ...

  7. 有关g++的Xlinker选项

    事情的起因是,在使用Eclipse配置opencv的库时,发现使用-Wl 选项需要自己在Linker flags的输入框里指定.并不是像使用-l选项或-I选项那样可以直接添加选项的条目就可以了.但是在 ...

  8. Python底层库的函数中from __future__ import absolute_import的作用

    在查看TensorFlow的底层优化器时候看到from __future__ import absolute_import 查找相关资料后发现 这个语句的意思是加入绝对引用的特征 直白的意思是,比如: ...

  9. STL之Vector容器

    1.Vector容器 1)vector是将元素置于一个动态数组中加以管理的容器. 2)vector可以随机存取元素(支持索引值直接存取, 用[]操作符或at()方法,这个等下会详讲). 3)vecto ...

  10. linux小倒腾

    1.vim安装,sudo apt-get install vim-gtk,于是vim就安装好了.当然在我电脑上还出现了gvim,简单的vim配置(etc/vim/vimrc): "我的设置 ...