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

#PS0000111. Đĩa giữa những ngọn nến (Plates Between Candles)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đĩa giữa những ngọn nến (Plates Between Candles)

Plates Between Candles

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

A row of plates and candles on a table is represented by a zero-indexed string ss: * denotes a plate and | a candle. For a query (l,r)(l,r), consider only positions ll through rr, inclusive. Count plates having a candle somewhere on each side, with both candles inside the queried substring.

Input

The first line contains integers n,qn,q, the string length and number of queries.

The second line contains ss.

Each of the following qq lines contains zero-based indices l,rl,r.

Output

Print qq lines, each containing the count for the corresponding query.

Subtasks

  • Subtask 1 (20 points): 3≤n≤10003\le n\le 1000; 1≤q≤10001\le q\le 1000; ∣s∣=n|s|=n; Each character is * or |; 0≤l≤r<n0\le l\le r<n.
  • Subtask 2 (30 points): 3≤n≤1000003\le n\le 100000; 1≤q≤1000001\le q\le 100000; ∣s∣=n|s|=n; Each character is * or |; 0≤l≤r<n0\le l\le r<n; r−l+1≤10r-l+1\le 10.
  • Subtask 3 (50 points): 3≤n≤1000003\le n\le 100000; 1≤q≤1000001\le q\le 100000; ∣s∣=n|s|=n; Each character is * or |; 0≤l≤r<n0\le l\le r<n.

Examples 1

Input

10 2
**|**|***|
2 5
5 9

Output

2
3

Explanation

Candles occur at indices 2, 5, 9. Substring [2,5][2,5] is |**|, containing plates at 3 and 4. Substring [5,9][5,9] is |***|, containing plates at 6, 7 and 8. Each counted plate is enclosed by candles within its own query.

Examples 2

Input

21 5
***|**|*****|**||**|*
1 17
4 5
14 17
5 11
15 16

Output

9
0
0
0
0

Explanation

Candles are at 3, 6, 12, 15, 16, 19. In [1,17][1,17], the outermost candles are at 3 and 16, enclosing 2+5+2=92+5+2=9 plates. In [4,5][4,5] there is no candle; [14,17][14,17] has adjacent candles 15 and 16; [5,11][5,11] has only candle 6; [15,16][15,16] has only two adjacent candles. All four counts are zero.