CF2260E.Cyclic Balance

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Consider a string t=t1t2…tmt=t_1t_2\ldots t_m consisting of the characters 0 and 1. We call the pairs of adjacent characters of the string tt the pairs t1t2,t2t3,…,tm−1tmt_1t_2, t_2t_3, \ldots, t_{m-1}t_m, as well as the pair tmt1t_mt_1. The last pair connects the end of the string with its beginning, so exactly mm pairs are considered in total. If the string consists of only one character, the only pair that is considered is t1t1t_1 t_1.

We call a string tt cyclically balanced if, among its pairs of adjacent characters, the numbers of pairs 00, 01, 10, and 11 are equal.

The cost of a binary string is the minimum number of characters that need to be inserted into it so that it becomes cyclically balanced. Characters may be inserted in any positions, including before the first and after the last character of the string. It is not allowed to delete or replace the original characters.

You are given a binary string ss and qq queries. In each query, indices ll and rr are given. Find the cost of the substring slsl+1…srs_l s_{l+1}\ldots s_r.

考虑一个由字符 0 和 1 组成的字符串 t=t1t2…tmt = t_1 t_2 \ldots t_m。我们将字符串 tt 中相邻字符组成的对称为:t1t2, t2t3, …, tm−1tmt_1 t_2,\, t_2 t_3,\, \ldots,\, t_{m-1} t_m,以及 tmt1t_m t_1。最后一对将字符串末尾与开头连接起来,因此总共恰好考虑 mm 对。若字符串仅含一个字符,则唯一考虑的对为 t1t1t_1 t_1。

若在字符串 tt 的所有相邻字符对中,00、01、10 和 11 这四类对的数量均相等,则称该字符串 tt 是循环平衡的(cyclically balanced)。

一个二进制字符串的**代价(cost)**定义为:使其变为循环平衡所需插入的最少字符个数。字符可插入到任意位置(包括原字符串首字符之前和末字符之后)。不允许删除或替换原字符串中的任何字符。

给定一个二进制字符串 ss 和 qq 个查询。每个查询给出下标 ll 和 rr,请计算子串 slsl+1…srs_l s_{l+1} \ldots s_r 的代价。

输入格式

The first line contains two integers nn and qq (1≤n,q≤3⋅1051 \le n, q \le 3 \cdot 10^5) — the length of the string and the number of queries.

The second line contains ss — a sequence of length nn consisting of the characters 0 and/or 1.

Then follow qq lines; the ii-th of them contains two integers lil_i and rir_i (1≤li≤ri≤n1 \le l_i \le r_i \le n) — the boundaries of the substring for the corresponding query.

第一行包含两个整数 nn 和 qq(1≤n,q≤3⋅1051 \le n, q \le 3 \cdot 10^5)—— 分别表示字符串的长度和查询次数。

第二行包含字符串 ss —— 一个长度为 nn 的、仅由字符 0 和/或 1 组成的序列。

接下来是 qq 行;其中第 ii 行包含两个整数 lil_i 和 rir_i(1≤li≤ri≤n1 \le l_i \le r_i \le n)—— 表示对应查询的子串边界。

输出格式

Print qq integers: the ii-th integer should be equal to the cost of the substring from the ii-th query.

输出 qq 个整数:第 ii 个整数应等于第 ii 个查询所对应的子串的代价。

输入输出样例

  • 输入#1

    11 7
    00111100000
    1 8
    1 1
    1 2
    1 4
    3 6
    2 7
    7 11

    输出#1

    4
    3
    2
    0
    4
    2
    7

说明/提示

null

输入解题思路,AI测评打分。不知道怎么写?

首页