CF1473D.Program

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given a program that consists of nn instructions. Initially a single variable xx is assigned to 00. Afterwards, the instructions are of two types:

  • increase xx by 11;
  • decrease xx by 11.

You are given mm queries of the following format:

  • query ll rr — how many distinct values is xx assigned to if all the instructions between the ll-th one and the rr-th one inclusive are ignored and the rest are executed without changing the order?

你被给定一个包含 nn 条指令的程序。初始时,单个变量 xx 被赋值为 00。随后,指令分为两种类型:

  • 将 xx 的值增加 11;
  • 将 xx 的值减少 11。

你将收到 mm 个如下格式的查询:

  • 查询 ll rr —— 若忽略第 ll 条至第 rr 条(含)之间的所有指令,而其余指令按原顺序执行,则 xx 在整个执行过程中总共取过多少个不同的值?

输入格式

The first line contains a single integer tt (1≤t≤10001 \le t \le 1000) — the number of testcases.

Then the description of tt testcases follows.

The first line of each testcase contains two integers nn and mm (1≤n,m≤2⋅1051 \le n, m \le 2 \cdot 10^5) — the number of instructions in the program and the number of queries.

The second line of each testcase contains a program — a string of nn characters: each character is either '+' or '-' — increment and decrement instruction, respectively.

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

The sum of nn over all testcases doesn't exceed 2⋅1052 \cdot 10^5. The sum of mm over all testcases doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 测试用例的数量。

接下来是 tt 个测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(1≤n,m≤2⋅1051 \le n, m \le 2 \cdot 10^5)—— 程序中指令的数量和查询的数量。

每个测试用例的第二行包含一个长度为 nn 的字符串,表示程序:其中每个字符为 '+' 或 '-',分别表示自增和自减指令。

接下来的 mm 行中,每行包含两个整数 ll 和 rr(1≤l≤r≤n1 \le l \le r \le n)—— 查询的描述。

所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。所有测试用例的 mm 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each testcase print mm integers — for each query ll, rr print the number of distinct values variable xx is assigned to if all the instructions between the ll-th one and the rr-th one inclusive are ignored and the rest are executed without changing the order.

对于每个测试用例,输出 mm 个整数——对每个查询 l,rl, r,输出在忽略第 ll 条至第 rr 条(含)指令、其余指令保持原有顺序执行的情况下,变量 xx 被赋值的不同值的个数。

输入输出样例

  • 输入#1

    2
    8 4
    -+--+--+
    1 8
    2 8
    2 5
    1 1
    4 10
    +-++
    1 1
    1 2
    2 2
    1 3
    2 3
    3 3
    1 4
    2 4
    3 4
    4 4

    输出#1

    1
    2
    4
    4
    3
    3
    4
    2
    3
    2
    1
    2
    2
    2

说明/提示

The instructions that remain for each query of the first testcase are:

  1. empty program — xx was only equal to 00;
  2. "-" — xx had values 00 and −1-1;
  3. "---+" — xx had values 00, −1-1, −2-2, −3-3, −2-2 — there are 44 distinct values among them;
  4. "+--+--+" — the distinct values are 11, 00, −1-1, −2-2.

第一个测试用例中,每个查询所剩余的指令如下:

  1. 空程序 — xx 的值仅等于 00;
  2. "-" — xx 的值依次为 00 和 −1-1;
  3. "---+" — xx 的值依次为 00、−1-1、−2-2、−3-3、−2-2 — 其中包含 44 个不同的值;
  4. "+--+--+" — 不同的值为 11、00、−1-1、−2-2。

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

首页