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>
 35 inline void readInteger(T& u){
 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() {
124     readInteger(n);
125     readInteger(q);
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--) {
134         readInteger(a);
135         readInteger(b);
136         Matrix c = st.query(st.root, 1, n, a, b);
137         printf("%d\n", (c.mat[0][4] == inf) ? (-1) : (c.mat[0][4]));
138     }
139 }
140
141 int main() {
142     init();
143     solve();
144     return 0;
145 }

原文地址:https://www.cnblogs.com/yyf0309/p/8475433.html

时间: 2024-08-26 12:42:45

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

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

New Year and Old Subsequence 第一感觉是离线之后分治求dp, 但是感觉如果要把左边的dp值和右边的dp值合起来, 感觉很麻烦而且时间复杂度不怎么对.. 然后就gun取看题解了, 用线段树维护dp的值, 然后区间合并求答案. 每个节点保存dp[ i ][ j ]表示, 把当前管理的区间删到 s{2017}中的 s[ i + 1 ] - s[ j - 1 ],最少删几个, 然后合并的时候5 ^ 3合并. #include<bits/stdc++.h> #define L

CodeForces 52C Circular RMQ(区间循环线段树,区间更新,区间求和)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://codeforces.com/problemset/problem/52/C You are given circular array a0,?a1,?...,?an?-?1. There are two types of operations with it: inc(lf,?rg,?v) - this operation increases each element on the segm

codeforces 446C DZY Loves Fibonacci Numbers 数论+线段树成段更新

DZY Loves Fibonacci Numbers Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2014-07-14) Description In mathematical terms, the sequence Fn of Fibonacci numbers is defi

Codeforces Beta Round #12 D. Ball (线段树)

题目大意: n个女性中,如果有一个女性的三维比这个女性的三维都大,这个女性就要自杀.问要自杀多少个. 思路分析: 先按照第一维排序. 然后离散化第二维,用第二维的下标建树,树上的值是第三维,更新的时候取最大值. 注意是按照第一维度从大到小进入线段树. 而且还要严格递增. 所以处理第一维度比较大小的时候要分开处理,要把相同的先判断,再更新入树. 那么如何判断这个女性是否自杀. 首先,你知道第一维度是从大到小的,所以先进树了的节点的第一维度一定更大. 再次,我们考虑第二维度,我们去树上第二维度下标大

Educational Codeforces Round 23 F. MEX Queries(线段树)

题目链接:Educational Codeforces Round 23 F. MEX Queries 题意: 一共有n个操作. 1.  将[l,r]区间的数标记为1. 2.  将[l,r]区间的数标记为0. 3.  将[l,r]区间取反. 对每个操作,输出标记为0的最小正整数. 题解: hash后,用线段树xjb标记一下就行了. 1 #include<bits/stdc++.h> 2 #define ls l,m,rt<<1 3 #define rs m+1,r,rt<&l

Codeforces 484E Sign on Fence(可持久化线段树+二分)

题目链接:Codeforces 484E Sign on Fence 题目大意:给定给一个序列,每个位置有一个值,表示高度,现在有若干查询,每次查询l,r,w,表示在区间l,r中, 连续最长长度大于w的最大高度为多少. 解题思路:可持久化线段树维护区间合并,前端时间碰到一题可持久化字典树,就去查了一下相关论文,大概知道了是 什么东西. 将高度按照从大到小的顺序排序,然后每次插入一个位置,线段树维护最长连续区间,因为插入是按照从大到小的顺 序,所以每次的线段树中的连续最大长度都是满足高度大于等于当

Codeforces Round #401 (Div. 2) E 贪心,线段树

Codeforces Round #401 (Div. 2) A 循环节 B 暴力排一下 C 标记出来,但10^5,特耿直地码了个O(n^2)的上去,最气的是在最后3分钟的时候被叉== D 从后往前贪心暴糙一下就好.比赛时一眼瞄出来了不敢写,搞不懂这样竟然不会超时.. E. Hanoi Factory 题意:n个环体,内径a[i],外径b[i],高h[i].当 a[i+1]<b[i]<=b[i+1] 时,第 i 个环体可以堆在第 i+1个环体上.求可以堆出的最高高度. tags:佩服那些大佬,

codeforces 340D D. Bubble Sort Graph(dp+线段树)

题目链接: codeforces 340D 题目大意: 给出一个程序,就是冒泡排序,每次如果发现相邻两个数前一个大于后一个,交换位置后建边,问最后得到的这个图中的最大独立集,这个最大独立集定义为所有的点都不相邻的最大点的集合的规模. 题目分析: 首先我们可以知道对于a[i],它只会且一定会与后面的比它小的建边,所以我们只需要固定第一个点,然后找最长上升子序列即可.(这样能够保证没有相邻的点) 定义状态dp[i]为以i项结尾的最长上升子序列的长度. 转移方程如下: dp[i]=max{dp[j]+

Codeforces 242E. XOR on Segment (二维线段树 lazy操作 xor)

题目链接: http://codeforces.com/problemset/problem/242/E 题意: 给出一个序列,有两种操作,一种是计算l到r的和,另一种是让l到r的数全部和x做异或运算. 思路: from: http://blog.csdn.net/u013912596/article/details/39006317 很显然直接暴力是不可能的,又是两种操作,又想到了线段树..但是这并不简单,异或操作该怎么处理? 异或是一种位运算,如果x的第j位是1,那么说明l到r的每个数的第j