PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000176. Số giá trị sau khi bỏ thao tác (Program)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Số giá trị sau khi bỏ thao tác (Program)

Program

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

An integer variable xx starts at zero. A program contains nn commands: + adds one and - subtracts one. For each query (l,r)(l,r), skip commands ll through rr inclusive, execute the remaining commands in their original order, and start again at x=0x=0. Count distinct values visited, including the initial zero. Queries are independent and do not modify the original program.

Input

Each file contains one case. The first line contains n,qn,q. The second contains the length-nn string ss. Each of the next qq lines contains l,rl,r.

Output

Print qq lines, each giving the distinct-value count for its query.

Subtasks

  • Subtask 1 (20 points): 1≤n≤5001\le n\le 500; 1≤q≤5001\le q\le 500; 1≤l≤r≤n1\le l\le r\le n; the string contains only + and -.
  • Subtask 2 (30 points): 1≤n≤2000001\le n\le 200000; 1≤q≤2000001\le q\le 200000; 1≤l≤r≤n1\le l\le r\le n; the string contains only + and -; all commands are identical.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 1≤q≤2000001\le q\le 200000; 1≤l≤r≤n1\le l\le r\le n; the string contains only + and -.

Example 1

Input

8 4
-+--+--+
1 8
2 8
2 5
1 1

Output

1
2
4
4

Explanation

Deleting all commands leaves only zero. The next three sets are {0,-1}, {0,-1,-2,-3}, {0,1,-1,-2}.

Example 2

Input

4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4

Output

3
3
4
2
3
2
1
2
2
2

Explanation

For (2,2), the remaining program is +++, visiting 0,1,2,3. Query (1,4) leaves only zero. Evaluate the other queries analogously.