CF1672H.Zigu Zagu

省选/NOI-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You have a binary string aa of length nn consisting only of digits 00 and 11.

You are given qq queries. In the ii-th query, you are given two indices ll and rr such that 1≤l≤r≤n1 \le l \le r \le n.

Let s=a[l,r]s=a[l,r]. You are allowed to do the following operation on ss:

  1. Choose two indices xx and yy such that 1≤x≤y≤∣s∣1 \le x \le y \le |s|. Let tt be the substring t=s[x,y]t = s[x, y]. Then for all 1≤i≤∣t∣−11 \le i \le |t| - 1, the condition ti≠ti+1t_i \neq t_{i+1} has to hold. Note that x=yx = y is always a valid substring.
  2. Delete the substring s[x,y]s[x, y] from ss.

For each of the qq queries, find the minimum number of operations needed to make ss an empty string.

Note that for a string ss, s[l,r]s[l,r] denotes the subsegment sl,sl+1,…,srs_l,s_{l+1},\ldots,s_r.

你有一个长度为 nn 的二进制字符串 aa,仅由数字 00 和 11 组成。

你将收到 qq 个查询。在第 ii 个查询中,你将得到两个下标 ll 和 rr,满足 1≤l≤r≤n1 \le l \le r \le n。

令 s=a[l,r]s = a[l,r]。你被允许对 ss 执行以下操作:

  1. 选择两个下标 xx 和 yy,满足 1≤x≤y≤∣s∣1 \le x \le y \le |s|。令 tt 为子串 t=s[x,y]t = s[x, y]。则对所有 1≤i≤∣t∣−11 \le i \le |t| - 1,必须满足 ti≠ti+1t_i \neq t_{i+1}。注意:x=yx = y 总是合法的子串。
  2. 将子串 s[x,y]s[x, y] 从 ss 中删除。

对每个查询,求出使 ss 变为空字符串所需的最少操作次数。

注意:对于字符串 ss,s[l,r]s[l,r] 表示子段 sl,sl+1,…,srs_l, s_{l+1}, \ldots, s_r。

输入格式

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

The second line contains a binary string aa of length nn (ai∈0,1a_i \in {0, 1}).

Each of the next qq lines contains two integers ll and rr (1≤l≤r≤n1 \le l \le r \le n) — representing the substring of each query.

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

第二行包含一个长度为 nn 的二进制字符串 aa(其中 ai∈{0,1}a_i \in \{0, 1\})。

接下来的 qq 行,每行包含两个整数 ll 和 rr(1≤l≤r≤n1 \le l \le r \le n)—— 表示每次查询所对应的子串范围。

输出格式

Print qq lines, the ii-th line representing the minimum number of operations needed for the ii-th query.

输出 qq 行,其中第 ii 行表示第 ii 个查询所需的最少操作次数。

输入输出样例

  • 输入#1

    5 3
    11011
    2 4
    1 5
    3 5

    输出#1

    1
    3
    2
  • 输入#2

    10 3
    1001110110
    1 10
    2 5
    5 10

    输出#2

    4
    2
    3

说明/提示

In the first test case,

  1. The substring is 101\texttt{101}, so we can do one operation to make the substring empty.
  2. The substring is 11011\texttt{11011}, so we can do one operation on s[2,4]s[2, 4] to make 11\texttt{11}, then use two more operations to make the substring empty.
  3. The substring is 011\texttt{011}, so we can do one operation on s[1,2]s[1, 2] to make 1\texttt{1}, then use one more operation to make the substring empty.

在第一个测试用例中:

  1. 子串为 101\texttt{101},因此我们可以执行一次操作使该子串变为空。
  2. 子串为 11011\texttt{11011},因此我们可以对 s[2,4]s[2, 4] 执行一次操作得到 11\texttt{11},再执行两次操作使该子串变为空。
  3. 子串为 011\texttt{011},因此我们可以对 s[1,2]s[1, 2] 执行一次操作得到 1\texttt{1},再执行一次操作使该子串变为空。

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

首页