time limit per test3 seconds

memory limit per test256 megabytes

inputstandard input

outputstandard output

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

【题目链接】:http://codeforces.com/contest/750/problem/E

【题解】

  1. merge矩阵(dp)+线段树;
  2. m[i][j]表示从状态i->j转移的花费;
  3. 0表示什么都没有
  4. 1表示出现了2
  5. 2表示出现了20
  6. 3表示出现了201
  7. 4表示出现了2017
  8. 出现了2,20,则遇到6的时候我么可以不用管;
  9. 但是如果出现了2012017,然后又遇到了6,则我们需要把这个6删掉;
  10. for (i = 0;i <= 4;i++)//除了遇到2,0,1,6,7这5个特别的数字之外,
  11. m[i][i] = 0;//遇到的时候状态都不会变;所以不用花费(操作);
  12. if (ch == '2')
  13. {
  14. m[0][0] = 1;//什么都没有->什么都没有,删掉这个2
  15. m[0][1] = 0;//什么都没有->出现了2,花费变成0
  16. //注意这里m[1][1] = 0,表示从出现了一个2到出现了一个2可以不用删掉任何东西,因为加上一个2也无妨,下面的解释就省略了,希望大家能看得明白.
  17. }
  18. if (ch=='0')
  19. {
  20. m[1][1] = 1 ;//出现了一个2->出现了一个2,则把0删掉
  21. m[1][2] = 0;//从出现一个2->出现了20,因为当前就是0,所以什么都不用加
  22. }
  23. if (ch=='1')
  24. {
  25. ma[2][2] = 1;//出现了20->出现了20,则把1删掉
  26. ma[2][3] = 0;//出现了20->出现了201,因为刚好遇到一个1则什么都不加
  27. }
  28. if (ch=='7')
  29. {
  30. m[3][3] = 1;//出现了201->出现了201,则把遇到的7删掉
  31. m[3][4] = 0;//出现了201->出现了2017,刚好遇到7,则什么都不做
  32. }
  33. if (ch=='6')
  34. {
  35. a[3][3] = 1;//出现了201->出现了数字6,则一定要把6删掉,不然无法满足题意
  36. a[4][4] = 1;//出现了2017->出现了数字6,则也一定要把6删掉.
  37. }
  38. 线段树加一个合并操作就好;
  39. 那个合并的操作和floyd算法类似;
  40. 最后输出m[0][4];表示从什么都没有然后出现"2017"

【完整代码】

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define lson l,m,rt<<1
  4. #define rson m+1,r,rt<<1|1
  5. #define LL long long
  6. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  7. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  8. #define mp make_pair
  9. #define pb push_back
  10. #define fi first
  11. #define se second
  12. #define rei(x) scanf("%d",&x)
  13. #define rel(x) scanf("%I64d",&x)
  14. typedef pair<int,int> pii;
  15. typedef pair<LL,LL> pll;
  16. const int MAXN = 2e5+10;
  17. const int INF = 7e8;
  18. const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
  19. const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
  20. const double pi = acos(-1.0);
  21. struct abc
  22. {
  23. int m[5][5];
  24. friend abc operator * (abc a,abc b)
  25. {
  26. abc d;
  27. rep1(i,0,4)
  28. rep1(j,0,4)
  29. {
  30. d.m[i][j] = INF;
  31. rep1(k,0,4)
  32. d.m[i][j] = min(d.m[i][j],a.m[i][k]+b.m[k][j]);
  33. }
  34. return d;
  35. }
  36. };
  37. int n,q;
  38. abc a[MAXN*4];
  39. char s[MAXN];
  40. void build(int l,int r,int rt)
  41. {
  42. if (l==r)
  43. {
  44. char ch = s[l];
  45. rep1(i,0,4)
  46. rep1(j,0,4)
  47. a[rt].m[i][j] = (i==j)?0:INF;
  48. if (ch == '2')
  49. {
  50. a[rt].m[0][0] = 1;
  51. a[rt].m[0][1] = 0;
  52. }
  53. if (ch=='0')
  54. {
  55. a[rt].m[1][1] = 1;
  56. a[rt].m[1][2] = 0;
  57. }
  58. if (ch=='1')
  59. {
  60. a[rt].m[2][2] = 1;
  61. a[rt].m[2][3] = 0;
  62. }
  63. if (ch=='7')
  64. {
  65. a[rt].m[3][3] = 1;
  66. a[rt].m[3][4] = 0;
  67. }
  68. if (ch=='6')
  69. {
  70. a[rt].m[3][3] = 1;
  71. a[rt].m[4][4] = 1;
  72. }
  73. return;
  74. }
  75. int m = (l+r)>>1;
  76. build(lson);build(rson);
  77. a[rt] = a[rt<<1]*a[rt<<1|1];
  78. }
  79. abc query(int L,int R,int l,int r,int rt)
  80. {
  81. if (L<=l && r <= R)
  82. return a[rt];
  83. int m = (l+r)>>1;
  84. abc temp1,temp2;
  85. bool f1 = 0,f2 = 0;
  86. if (L <= m)
  87. {
  88. temp1 = query(L,R,lson);
  89. f1 = 1;
  90. }
  91. if (m < R)
  92. {
  93. temp2 = query(L,R,rson);
  94. f2 = 1;
  95. }
  96. if (f1&&f2)
  97. return temp1*temp2;
  98. if (f1)
  99. return temp1;
  100. if (f2)
  101. return temp2;
  102. }
  103. int main()
  104. {
  105. //freopen("F:\\rush.txt","r",stdin);
  106. rei(n);rei(q);
  107. scanf("%s",s+1);
  108. build(1,n,1);
  109. rep1(i,1,q)
  110. {
  111. int L,R;
  112. rei(L);rei(R);
  113. int ans = query(L,R,1,n,1).m[0][4];
  114. if (ans>=INF)
  115. puts("-1");
  116. else
  117. cout << ans << endl;
  118. }
  119. return 0;
  120. }

【codeforces 750E】New Year and Old Subsequence的更多相关文章

  1. 【codeforces 766A】Mahmoud and Longest Uncommon Subsequence

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  4. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  5. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  6. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  7. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

  8. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  9. 【Codeforces 670C】 Cinema

    [题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...

随机推荐

  1. Android实现点击通知栏后,先启动应用再打开目标Activity

    情况简述 在开发Android app的过程中,遇到这样一个需求:app中启动一个Service,该Service在独立进程中运行,与服务器保持长连接,将服务器推送过来的消息在通知栏中显示,并设置点击 ...

  2. 学习活动管理系统:LAMS

    学习活动管理系统:LAMS 一.总结 基于java的cms 二.LAMS Learning Activity Management System,学习活动管理系统. 数字化学习已经具有完整的发展方法来 ...

  3. ORACLE10g R2【单实例 FS→单实例FS】

    ORACLE10g R2[单实例FS→单实例FS] 本演示案例所用环境:   primary standby OS Hostname pry std OS Version RHEL5.8 RHEL5. ...

  4. 【例题 7-10 UVA - 11212】Editing a Book

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 迭代加深搜. 很容易想到,最多只要搜8层就可以得到答案了 ->最多8下肯定可以还原. 则枚举一下最大层数.然后做个深搜就好. ...

  5. [置顶] WebService学习总结(3)——使用java JDK开发WebService

    一.WebService的开发手段 使用Java开发WebService时可以使用以下两种开发手段 1. 使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) 二.使用JDK开发Web ...

  6. mycat快速体验(转)

    横空出世的MyCat截至到2015年4月,保守估计已经有超过60个项目在使用,主要应用在电信领域.互联网项目,大部分是交易和管理系统,少量是信息系统.比较大的系统中,数据规模单表单月30亿.本人也初步 ...

  7. PatentTips - Method and Apparatus to Support Virtualization with Code Patches

    BACKGROUND As recognized in Revision 2.0 of the Intel® Virtualization Technology Specification for t ...

  8. Spring Boot集成EHCache实现缓存机制

    SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...

  9. [Android5.1]ActivityManagerService启动过程分析

    ActivityManagerService(简称AMS)是Android系统的关键服务之中的一个.它的主要作用例如以下: 管理系统中全部应用进程的整个生命周期 管理应用进程中的Activity.Se ...

  10. HDU - 4552 怪盗基德的挑战书 (后缀数组)

    Description "在树最漂亮的那天,当时间老人再次把大钟平均分开时,我会降临在灯火之城的金字塔前.带走那最珍贵的笑容."这是怪盗基德盗取巴黎卢浮宫的<蒙娜丽莎的微笑& ...