# Codeforces 750E New Year and Old Subsequence - 线段树 - 动态规划

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 3201667661 81 72 8
output
43-1
input
15 50120166620916703 41 144 151 1310 15
output
-121-1-1
input
4 212342 41 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

  1 /**
2  * Codeforces
3  * Problem#750E
4  * Accepted
5  * Time: 998ms
6  * Memory: 49276k
7  */
8 #include<iostream>
9 #include<fstream>
10 #include<sstream>
11 #include<cstdio>
12 #include<cstdlib>
13 #include<cstring>
14 #include<ctime>
15 #include<cctype>
16 #include<cmath>
17 #include<algorithm>
18 #include<stack>
19 #include<queue>
20 #include<set>
21 #include<map>
22 #include<vector>
23 #include<malloc.h>
24 #ifndef WIN32
25 #define AUTO "%lld"
26 #else
27 #define AUTO "%I64d"
28 #endif
29 using namespace std;
30 typedef bool boolean;
31 #define inf 0xfffffff
32 #define smin(a, b)    (a) = min((a), (b))
33 #define smax(a, b)    (a) = max((a), (b))
34 template<typename T>
36     char x;
37     int aFlag = 1;
38     while(!isdigit((x = getchar())) && x != '-' && x != -1);
39     if(x == -1)    return;
40     if(x == '-'){
41         x = getchar();
42         aFlag = -1;
43     }
44     for(u = x - '0'; isdigit((x = getchar())); u = (u << 3) + (u << 1) + x - '0');
45     ungetc(x, stdin);
46     u *= aFlag;
47 }
48
49 typedef class Matrix {
50     public:
51         int mat[5][5];    //0:        1:2            2:20        3:201        4:2017
52         Matrix(){        }
53         Matrix(char x){
54             for(int i = 0; i <= 4; i++)
55                 for(int j = 0; j <= 4; j++)
56                     mat[i][j] = (i == j) ? (0) : (inf);
57             if(x == '2')    mat[0][0] = 1, mat[0][1] = 0;
58             else if(x == '0')    mat[1][1] = 1, mat[1][2] = 0;
59             else if(x == '1')    mat[2][2] = 1, mat[2][3] = 0;
60             else if(x == '7')    mat[3][3] = 1, mat[3][4] = 0;
61             else if(x == '6')    mat[3][3] = 1, mat[4][4] = 1;
62         }
63
64         Matrix operator +(Matrix x) {
65             Matrix res;
66             for(int i = 0; i < 5; i++)
67                 for(int j = 0; j < 5; j++){
68                     res.mat[i][j] = inf;
69                     for(int k = 0; k < 5; k++)
70                         smin(res.mat[i][j], mat[i][k] + x.mat[k][j]);
71                 }
72             return res;
73         }
74 }Matrix;
75
76 typedef class SegTreeNode {
77     public:
78         Matrix a;
79         SegTreeNode* left, *right;
80         SegTreeNode():left(NULL), right(NULL){        }
81         SegTreeNode(char x):left(NULL), right(NULL){
82             a = Matrix(x);
83         }
84
85         void pushUp() {
86             a = left->a + right->a;
87         }
88 }SegTreeNode;
89
90 typedef class SegTree {
91     public:
92         SegTreeNode* root;
93         SegTree():root(NULL){        }
94         SegTree(int size, char* str){
95             build(root, 1, size, str);
96         }
97
98         void build(SegTreeNode*& node, int l, int r, char* list){
99             if(l == r){
100                 node = new SegTreeNode(list[l]);
101                 return;
102             }
103             node = new SegTreeNode();
104             int mid = (l + r) >> 1;
105             build(node->left, l, mid, list);
106             build(node->right, mid + 1, r, list);
107             node->pushUp();
108         }
109
110         Matrix query(SegTreeNode*& node, int l, int r, int from, int end) {
111             if(l == from && r == end)    return node->a;
112             int mid = (l + r) >> 1;
113             if(end <= mid)    return query(node->left, l, mid, from, end);
114             if(from > mid)    return query(node->right, mid + 1, r, from, end);
115             return query(node->left, l, mid, from, mid) + query(node->right, mid + 1, r, mid + 1, end);
116         }
117 }SegTree;
118
119 int n, q;
120 char* str;
121 SegTree st;
122
123 inline void init() {
126     str = new char[(const int)(n + 5)];
127     scanf("%s", str + 1);
128     st = SegTree(n, str);
129 }
130
131 inline void solve() {
132     int a, b;
133     while(q--) {
145 }