CF1473D.Program

普及/提高-

通过率:0%

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?

输入格式

The first line contains a single integer tt ( 1t10001 \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 ( 1n,m21051 \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 ( 1lrn1 \le l \le r \le n ) — the description of the query.

The sum of nn over all testcases doesn't exceed 21052 \cdot 10^5 . The sum of mm over all testcases doesn't exceed 21052 \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.

输入输出样例

  • 输入#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 .
首页