A - バスの出発時刻

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 233

問題文

高橋君は遠足の引率教員として、すべての生徒がバスに乗り込んだ後に出発の合図を出す役割を担っています。

この遠足には N 人の生徒が参加しており、各生徒 i (1 \leq i \leq N) は時刻 T_i にバスへの乗り込みを完了します。ここで、時刻はある基準時点からの経過時間を分単位の非負整数で表したものです。

すべての生徒がバスに乗り込み終わるのは、T_1, T_2, \ldots, T_N の最大値の時刻です。高橋君は、その時刻からちょうど K 分後に出発の合図を出すことになっています。

高橋君が出発の合図を出す時刻を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq K \leq 10^4
  • 0 \leq T_i \leq 10^9 (1 \leq i \leq N)
  • T_1, T_2, \ldots, T_N は昇順とは限らず、同じ値が複数含まれることもある
  • 入力はすべて整数

入力

N K
T_1 T_2 \ldots T_N
  • 1 行目には、生徒の人数を表す整数 N と、すべての生徒が乗り込み終わってから出発の合図を出すまでの時間(分)を表す整数 K が、スペース区切りで与えられる。
  • 2 行目には、各生徒 i がバスへの乗り込みを完了する時刻を表す整数 T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。

出力

高橋君が出発の合図を出す時刻を、基準時点からの経過時間(分)を表す整数として 1 行に出力せよ。


入力例 1

3 5
10 25 15

出力例 1

30

入力例 2

5 10
100 50 200 150 75

出力例 2

210

入力例 3

8 1000
500000000 123456789 999999999 1 0 876543210 500000001 999999998

出力例 3

1000000999

Score : 233 pts

Problem Statement

Takahashi is a chaperone teacher on a field trip, and his role is to give the departure signal after all students have boarded the bus.

There are N students participating in this field trip, and each student i (1 \leq i \leq N) finishes boarding the bus at time T_i. Here, time is represented as a non-negative integer in minutes, measuring the elapsed time from a certain reference point.

All students finish boarding the bus at the time equal to the maximum value among T_1, T_2, \ldots, T_N. Takahashi is supposed to give the departure signal exactly K minutes after that time.

Find the time at which Takahashi gives the departure signal.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq K \leq 10^4
  • 0 \leq T_i \leq 10^9 (1 \leq i \leq N)
  • T_1, T_2, \ldots, T_N are not necessarily in ascending order, and may contain duplicate values
  • All input values are integers

Input

N K
T_1 T_2 \ldots T_N
  • The first line contains an integer N representing the number of students and an integer K representing the time in minutes from when all students finish boarding until the departure signal is given, separated by a space.
  • The second line contains integers T_1, T_2, \ldots, T_N representing the time at which each student i finishes boarding the bus, separated by spaces.

Output

Print the time at which Takahashi gives the departure signal, as an integer representing the elapsed time in minutes from the reference point, on a single line.


Sample Input 1

3 5
10 25 15

Sample Output 1

30

Sample Input 2

5 10
100 50 200 150 75

Sample Output 2

210

Sample Input 3

8 1000
500000000 123456789 999999999 1 0 876543210 500000001 999999998

Sample Output 3

1000000999
B - バスツアーの班分け

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

高橋君は旅行会社の添乗員です。今度のバスツアーには N 人の参加者が申し込んでおり、それぞれの参加者には希望する出発時刻があります。

各参加者 i (1 \leq i \leq N) の希望出発時刻は T_i です。旅行会社の方針では、効率的にツアーを運営するために、希望出発時刻が近い参加者たちを同じバスにまとめて出発させます。具体的には、同じバスに乗るどの 2 人の参加者 i, j についても、希望出発時刻の差の絶対値 |T_i - T_j|K 以下でなければなりません。なお、1 台のバスに乗せられる人数に上限はありません。

高橋君は、すべての参加者をちょうど 1 台ずつバスに振り分けて、必要なバスの台数を最小にしたいと考えています。

すべての参加者をバスに振り分けるために必要な最小のバスの台数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq 10^9
  • 0 \leq T_i \leq 10^9 (1 \leq i \leq N)
  • 入力はすべて整数

入力

N K
T_1 T_2 \ldots T_N
  • 1 行目には、参加者の人数を表す N と、同じバスに乗せられる希望出発時刻の差の絶対値の上限を表す K が、スペース区切りで与えられる。
  • 2 行目には、各参加者の希望出発時刻を表す T_1, T_2, \ldots, T_N が、スペース区切りで与えられる。

出力

すべての参加者をバスに振り分けるために必要な最小のバスの台数を 1 行で出力してください。


入力例 1

5 3
1 5 3 9 12

出力例 1

3

入力例 2

8 10
5 100 15 8 95 20 105 12

出力例 2

3

入力例 3

15 5
0 1 2 10 11 12 13 25 30 31 32 33 50 100 101

出力例 3

6

Score : 300 pts

Problem Statement

Takahashi is a tour conductor at a travel agency. For the upcoming bus tour, N participants have signed up, and each participant has a preferred departure time.

The preferred departure time of each participant i (1 \leq i \leq N) is T_i. According to the travel agency's policy, to operate tours efficiently, participants with similar preferred departure times are grouped together on the same bus. Specifically, for any two participants i, j on the same bus, the absolute difference of their preferred departure times |T_i - T_j| must be at most K. There is no upper limit on the number of people that can ride on a single bus.

Takahashi wants to assign every participant to exactly one bus and minimize the number of buses required.

Find the minimum number of buses needed to assign all participants to buses.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq 10^9
  • 0 \leq T_i \leq 10^9 (1 \leq i \leq N)
  • All inputs are integers

Input

N K
T_1 T_2 \ldots T_N
  • The first line contains N, the number of participants, and K, the maximum allowed absolute difference in preferred departure times for participants on the same bus, separated by a space.
  • The second line contains the preferred departure times T_1, T_2, \ldots, T_N of each participant, separated by spaces.

Output

Print the minimum number of buses needed to assign all participants to buses, on a single line.


Sample Input 1

5 3
1 5 3 9 12

Sample Output 1

3

Sample Input 2

8 10
5 100 15 8 95 20 105 12

Sample Output 2

3

Sample Input 3

15 5
0 1 2 10 11 12 13 25 30 31 32 33 50 100 101

Sample Output 3

6
C - 農園の収穫祭

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 366

問題文

高橋君は広大な農園を経営しています。

農園には N 個の区画が一列に並んでおり、各区画には 1 から N までの番号が付けられています。区画 i (1 \leq i \leq N) には果物の木が植えられており、そこから収穫できる果物の量は A_i キログラムです。

収穫祭の期間中、高橋君は M 回の収穫作業を行います。j 回目 (1 \leq j \leq M) の収穫作業では、区画 L_j から区画 R_j までの連続する全ての区画を対象に果物を収穫します。

各区画の果物は一度収穫するとなくなります。そのため、ある区画が複数回の収穫作業の対象に含まれていたとしても、その区画から得られる収穫量は最初の1回分、すなわち A_i キログラムのみです。

言い換えると、M 回の収穫作業のうち少なくとも1回は対象となった区画全ての A_i の合計が、高橋君の得る総収穫量となります。この総収穫量を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq L_j \leq R_j \leq N (1 \leq j \leq M)
  • 入力はすべて整数

入力

N M
A_1 A_2 \ldots A_N
L_1 R_1
L_2 R_2
\vdots
L_M R_M
  • 1 行目には、区画の数 N と収穫作業の回数 M が空白区切りで与えられる。
  • 2 行目には、各区画から収穫できる果物の量 A_1, A_2, \ldots, A_N が空白区切りで与えられる。
  • 続く M 行のうち j 行目 (1 \leq j \leq M) には、j 回目の収穫作業の対象となる区画の範囲の左端 L_j と右端 R_j が空白区切りで与えられる。

出力

高橋君が得る果物の総収穫量を 1 行で出力せよ。


入力例 1

5 3
10 20 30 40 50
1 3
2 4
4 5

出力例 1

150

入力例 2

7 4
5 12 8 3 15 9 6
1 2
4 6
2 5
1 7

出力例 2

58

入力例 3

10 6
100 250 180 90 320 150 200 80 170 260
1 3
5 7
2 4
6 9
1 1
8 10

出力例 3

1800

Score : 366 pts

Problem Statement

Takahashi manages a vast farm.

The farm has N plots arranged in a row, numbered from 1 to N. Plot i (1 \leq i \leq N) has a fruit tree planted in it, and the amount of fruit that can be harvested from it is A_i kilograms.

During the harvest festival, Takahashi performs M harvesting operations. In the j-th (1 \leq j \leq M) harvesting operation, he harvests fruit from all consecutive plots from plot L_j to plot R_j.

The fruit in each plot is gone once harvested. Therefore, even if a plot is included in multiple harvesting operations, the yield obtained from that plot is only from the first time, namely A_i kilograms.

In other words, the total harvest Takahashi obtains is the sum of A_i over all plots that were targeted by at least one of the M harvesting operations. Find this total harvest.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq L_j \leq R_j \leq N (1 \leq j \leq M)
  • All inputs are integers

Input

N M
A_1 A_2 \ldots A_N
L_1 R_1
L_2 R_2
\vdots
L_M R_M
  • The first line contains the number of plots N and the number of harvesting operations M, separated by a space.
  • The second line contains the amounts of fruit harvestable from each plot A_1, A_2, \ldots, A_N, separated by spaces.
  • In the following M lines, the j-th line (1 \leq j \leq M) contains the left endpoint L_j and right endpoint R_j of the range of plots targeted by the j-th harvesting operation, separated by a space.

Output

Output the total harvest of fruit that Takahashi obtains, in a single line.


Sample Input 1

5 3
10 20 30 40 50
1 3
2 4
4 5

Sample Output 1

150

Sample Input 2

7 4
5 12 8 3 15 9 6
1 2
4 6
2 5
1 7

Sample Output 2

58

Sample Input 3

10 6
100 250 180 90 320 150 200 80 170 260
1 3
5 7
2 4
6 9
1 1
8 10

Sample Output 3

1800
D - 制御パネルの操作順序

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

高橋君は、K 個のランプが一列に並んだ制御パネルに対して、N 種類の操作を 1 日に 1 種類ずつ、合計 N 日間かけてすべて実行しました。

それぞれの操作はちょうど 1 回だけ実行されましたが、どの順序で実行されたかは分からなくなりました。

各ランプは「点灯」または「消灯」のどちらかの状態をとります。

パネルの状態は長さ K0/1 文字列で表され、j 文字目(1 \leq j \leq K)の 1 はランプ j の点灯を、0 は消灯を表します。

パネルの初期状態は文字列 S で与えられます。

操作 i1 \leq i \leq N)には、長さ K0/1 文字列 A_i, B_i, X_i が定められています。

実行条件: 操作 i を実行できるのは、すべての j1 \leq j \leq K)について以下の条件をすべて満たすときに限ります。

  • A_ij 文字目が 1 のとき、ランプ j が点灯していなければならない。
  • B_ij 文字目が 1 のとき、ランプ j が消灯していなければならない。

A_ij 文字目と B_ij 文字目がともに 0 のとき、ランプ j の状態は問いません。

一方、A_ij 文字目と B_ij 文字目がともに 1 であることもありえます。その場合、ランプ j が点灯かつ消灯でなければならず、どのようなパネル状態でもこの条件を満たせないため、操作 i はいかなる状況でも実行できません。

効果: 操作 i を実行すると、各 j1 \leq j \leq K)について、

  • X_ij 文字目が 1 であるとき、ランプ j の点灯・消灯が反転する。
  • X_ij 文字目が 0 であるとき、ランプ j は変化しない。

青木君は、各日の操作後に点灯しているランプの個数だけを記録していました。

記録の初期値として、t 日目(1 \leq t \leq N)の操作後に点灯しているランプの個数 C_t が与えられます。

この記録には後から Q 回の訂正が入ります。

q 回目(1 \leq q \leq Q)の訂正では、その時点での記録のうち T_q 日目の値を Y_q に変更します。それまでの訂正による変更はすべて保持されたまま、新たな訂正が上書き適用されます。

各訂正の直後について、その時点での記録と矛盾しない操作の実行順序の個数を 998244353 で割った余りを求めてください。

ここで、操作の実行順序とは (1, 2, \ldots, N) の順列 (p_1, p_2, \ldots, p_N) であり、その順列が記録と矛盾しないとは、次のすべてを満たすことをいいます。

  • パネルの初期状態は S である。
  • t 日目(t = 1, 2, \ldots, N)には操作 p_t を実行する。
  • 操作 p_t を実行する直前のパネルの状態が、操作 p_t の実行条件を満たしている。
  • t 日目の操作後に点灯しているランプの個数が、記録の C_t と等しい。

なお、操作番号の順列として異なるものは、たとえ条件や効果がまったく同じ操作どうしを入れ替えただけであっても、異なる実行順序として数えます。

制約

  • 1 \leq N \leq 9
  • 1 \leq K \leq 9
  • 1 \leq Q \leq 10^5
  • S は長さ K の文字列であり、各文字は 0 または 1 である
  • 0 \leq C_t \leq K1 \leq t \leq N
  • A_i, B_i, X_i はそれぞれ長さ K の文字列であり、各文字は 0 または 1 である(1 \leq i \leq N
  • A_ij 文字目と B_ij 文字目がともに 1 であるケースもありうる
  • 1 \leq T_q \leq N1 \leq q \leq Q
  • 0 \leq Y_q \leq K1 \leq q \leq Q
  • 入力はすべて整数および指定された形式の文字列で与えられる

入力

N K Q
S
C_1 C_2 \ldots C_N
A_1 B_1 X_1
A_2 B_2 X_2
\vdots
A_N B_N X_N
T_1 Y_1
T_2 Y_2
\vdots
T_Q Y_Q
  • 1 行目には、操作の種類数 N、ランプの個数 K、訂正の回数 Q が、スペース区切りで与えられる。
  • 2 行目には、パネルの初期状態を表す長さ K0/1 文字列 S が与えられる。
  • 3 行目には、記録の初期値を表す整数列 C_1, C_2, \ldots, C_N が、スペース区切りで与えられる。
  • 続く N 行のうち i 行目には、操作 i の実行条件と効果を表す長さ K0/1 文字列 A_i, B_i, X_i が、スペース区切りで与えられる。
  • 続く Q 行のうち q 行目には、q 回目の訂正を表す整数 T_q, Y_q が、スペース区切りで与えられる。これは、その時点での記録の T_q 日目の値を Y_q に変更することを表す。

出力

Q 行出力せよ。

q 行目には、q 回目の訂正の直後における、記録と矛盾しない操作の実行順序の個数を 998244353 で割った余りを出力せよ。


入力例 1

3 3 4
010
2 1 2
010 000 100
100 000 001
000 001 010
2 3
2 1
1 0
1 2

出力例 1

0
1
0
1

入力例 2

2 2 3
00
1 2
00 00 10
10 10 01
2 1
1 0
2 0

出力例 2

0
0
0

入力例 3

6 5 8
10100
3 2 3 4 3 2
10000 00010 01001
00100 00000 10010
00000 01000 00101
01001 10000 00011
00010 00100 11000
00000 00001 01110
3 4
6 1
1 2
4 5
5 0
2 3
3 2
6 2

出力例 3

0
0
0
0
0
0
0
0

入力例 4

9 9 12
101011001
5 4 6 5 3 4 5 4 6
100000001 000100000 010010000
000010000 001000000 100000100
010000000 000000010 001001000
000000100 100000000 000110001
001000010 000010000 010000011
000100000 000001000 101000000
000000000 010000001 000001110
100010000 000000100 001000101
000001001 001000000 110000010
1 4
9 5
5 6
3 3
7 7
2 2
8 4
4 1
6 5
1 5
9 0
5 3

出力例 4

0
0
0
0
0
0
0
0
0
0
0
0

入力例 5

1 1 1
0
0
0 0 1
1 1

出力例 5

1

Score : 400 pts

Problem Statement

Takahashi performed N types of operations on a control panel with K lamps arranged in a row, executing one type per day over a total of N days.

Each operation was executed exactly once, but the order in which they were executed has been lost.

Each lamp is in one of two states: "on" or "off".

The state of the panel is represented by a 0/1 string of length K, where the j-th character (1 \leq j \leq K) being 1 indicates that lamp j is on, and 0 indicates it is off.

The initial state of the panel is given by the string S.

For operation i (1 \leq i \leq N), three 0/1 strings of length K: A_i, B_i, X_i are defined.

Execution condition: Operation i can be executed only when the following conditions are all satisfied for every j (1 \leq j \leq K):

  • If the j-th character of A_i is 1, then lamp j must be on.
  • If the j-th character of B_i is 1, then lamp j must be off.

When both the j-th character of A_i and the j-th character of B_i are 0, the state of lamp j does not matter.

On the other hand, it is possible that both the j-th character of A_i and the j-th character of B_i are 1. In that case, lamp j must be both on and off simultaneously, which cannot be satisfied by any panel state, so operation i can never be executed under any circumstances.

Effect: When operation i is executed, for each j (1 \leq j \leq K):

  • If the j-th character of X_i is 1, the on/off state of lamp j is toggled.
  • If the j-th character of X_i is 0, lamp j does not change.

Aoki recorded only the number of lamps that are on after the operation on each day.

As the initial record, the number of lamps on after the operation on day t (1 \leq t \leq N), C_t, is given.

This record is subject to Q corrections afterwards.

In the q-th correction (1 \leq q \leq Q), the value for day T_q in the current record is changed to Y_q. All changes from previous corrections are retained, and the new correction is applied as an overwrite.

After each correction, find the number of operation execution orders that are consistent with the record at that point, modulo 998244353.

Here, an operation execution order is a permutation (p_1, p_2, \ldots, p_N) of (1, 2, \ldots, N), and a permutation is consistent with the record if and only if all of the following are satisfied:

  • The initial state of the panel is S.
  • On day t (t = 1, 2, \ldots, N), operation p_t is executed.
  • The panel state immediately before executing operation p_t satisfies the execution condition of operation p_t.
  • The number of lamps on after the operation on day t equals C_t in the record.

Note that different permutations of operation numbers are counted as different execution orders, even if they only swap operations with identical conditions and effects.

Constraints

  • 1 \leq N \leq 9
  • 1 \leq K \leq 9
  • 1 \leq Q \leq 10^5
  • S is a string of length K, where each character is 0 or 1
  • 0 \leq C_t \leq K (1 \leq t \leq N)
  • A_i, B_i, X_i are each strings of length K, where each character is 0 or 1 (1 \leq i \leq N)
  • It is possible that both the j-th character of A_i and the j-th character of B_i are 1
  • 1 \leq T_q \leq N (1 \leq q \leq Q)
  • 0 \leq Y_q \leq K (1 \leq q \leq Q)
  • All inputs are integers or strings in the specified format

Input

N K Q
S
C_1 C_2 \ldots C_N
A_1 B_1 X_1
A_2 B_2 X_2
\vdots
A_N B_N X_N
T_1 Y_1
T_2 Y_2
\vdots
T_Q Y_Q
  • The first line contains the number of operation types N, the number of lamps K, and the number of corrections Q, separated by spaces.
  • The second line contains a 0/1 string S of length K representing the initial state of the panel.
  • The third line contains the integers C_1, C_2, \ldots, C_N representing the initial record values, separated by spaces.
  • The following N lines, where the i-th line contains the 0/1 strings A_i, B_i, X_i of length K representing the execution condition and effect of operation i, separated by spaces.
  • The following Q lines, where the q-th line contains integers T_q, Y_q representing the q-th correction, separated by spaces. This means the value for day T_q in the current record is changed to Y_q.

Output

Output Q lines.

On the q-th line, output the number of operation execution orders consistent with the record immediately after the q-th correction, modulo 998244353.


Sample Input 1

3 3 4
010
2 1 2
010 000 100
100 000 001
000 001 010
2 3
2 1
1 0
1 2

Sample Output 1

0
1
0
1

Sample Input 2

2 2 3
00
1 2
00 00 10
10 10 01
2 1
1 0
2 0

Sample Output 2

0
0
0

Sample Input 3

6 5 8
10100
3 2 3 4 3 2
10000 00010 01001
00100 00000 10010
00000 01000 00101
01001 10000 00011
00010 00100 11000
00000 00001 01110
3 4
6 1
1 2
4 5
5 0
2 3
3 2
6 2

Sample Output 3

0
0
0
0
0
0
0
0

Sample Input 4

9 9 12
101011001
5 4 6 5 3 4 5 4 6
100000001 000100000 010010000
000010000 001000000 100000100
010000000 000000010 001001000
000000100 100000000 000110001
001000010 000010000 010000011
000100000 000001000 101000000
000000000 010000001 000001110
100010000 000000100 001000101
000001001 001000000 110000010
1 4
9 5
5 6
3 3
7 7
2 2
8 4
4 1
6 5
1 5
9 0
5 3

Sample Output 4

0
0
0
0
0
0
0
0
0
0
0
0

Sample Input 5

1 1 1
0
0
0 0 1
1 1

Sample Output 5

1
E - 交互に並べられる区間

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 466

問題文

高橋君は、白い石と黒い石を 1 列に N 個並べています。

左から i 番目の石の色は文字列 Si 文字目で表され、白い石は W、黒い石は B に対応します。

連続区間 [l, r]1 \leq l \leq r \leq N)とは、左から l 番目から r 番目までの石の並びを指します。

連続区間に含まれるすべての石を取り出し、好きな順序で 1 列に並べ直すことを考えます。隣り合うどの 2 つの石も異なる色であるような並べ方が存在するとき、その連続区間を よい区間 と呼びます。

特に、長さ 1 の連続区間は常によい区間です。

青木君は Q 個の質問をします。

i 番目の質問では、整数 L_i, R_i が与えられます。

各質問について、L_i \leq l \leq r \leq R_i を満たす連続区間 [l, r] のうち、よい区間であるものの個数を求めてください。

制約

  • 1 \leq N \leq 5 \times 10^4
  • 1 \leq Q \leq 5 \times 10^4
  • SWB のみからなる長さ N の文字列である
  • 1 \leq L_i \leq R_i \leq N (1 \leq i \leq Q)
  • N, Q, L_i, R_i はすべて整数である

入力

N Q
S
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q

1 行目には、石の個数 N と質問の個数 Q がスペース区切りで与えられる。

2 行目には、各石の色を表す長さ N の文字列 S が与えられる。

続く Q 行のうち i 行目には、i 番目の質問を表す整数 L_i, R_i がスペース区切りで与えられる。

出力

Q 行出力せよ。

i 行目には、i 番目の質問に対するよい区間の個数を出力せよ。


入力例 1

5 4
WBWBW
1 5
2 4
1 1
3 5

出力例 1

15
6
1
6

入力例 2

6 5
WWWWWW
1 6
1 3
2 2
3 6
5 6

出力例 2

6
3
1
4
2

入力例 3

18 8
WWBBWBWBBWWBWBWBWW
1 18
1 6
3 10
5 15
8 18
2 2
7 13
11 17

出力例 3

145
18
28
61
55
1
25
28

入力例 4

50 15
WBWBWBBBWWBBWBWBWWWBBBWBWBBWBWWBWBWBBWWBWBWBWWBBBW
1 50
1 10
11 20
21 30
31 40
41 50
5 25
10 35
15 45
2 49
7 7
18 22
24 37
33 50
3 14

出力例 4

875
41
39
42
51
45
171
270
397
809
1
10
90
146
52

入力例 5

1 1
B
1 1

出力例 5

1

Score : 466 pts

Problem Statement

Takahashi has arranged N white and black stones in a row.

The color of the i-th stone from the left is represented by the i-th character of the string S, where W corresponds to a white stone and B corresponds to a black stone.

A contiguous interval [l, r] (1 \leq l \leq r \leq N) refers to the sequence of stones from the l-th to the r-th from the left.

Consider taking out all the stones in a contiguous interval and rearranging them in a row in any order you like. If there exists an arrangement such that every two adjacent stones have different colors, we call that contiguous interval a good interval.

In particular, a contiguous interval of length 1 is always a good interval.

Aoki asks Q questions.

In the i-th question, integers L_i, R_i are given.

For each question, find the number of good intervals among the contiguous intervals [l, r] satisfying L_i \leq l \leq r \leq R_i.

Constraints

  • 1 \leq N \leq 5 \times 10^4
  • 1 \leq Q \leq 5 \times 10^4
  • S is a string of length N consisting only of W and B
  • 1 \leq L_i \leq R_i \leq N (1 \leq i \leq Q)
  • N, Q, L_i, R_i are all integers

Input

N Q
S
L_1 R_1
L_2 R_2
\vdots
L_Q R_Q

The first line contains the number of stones N and the number of questions Q, separated by a space.

The second line contains a string S of length N representing the color of each stone.

The i-th of the following Q lines contains the integers L_i, R_i representing the i-th question, separated by a space.

Output

Print Q lines.

The i-th line should contain the number of good intervals for the i-th question.


Sample Input 1

5 4
WBWBW
1 5
2 4
1 1
3 5

Sample Output 1

15
6
1
6

Sample Input 2

6 5
WWWWWW
1 6
1 3
2 2
3 6
5 6

Sample Output 2

6
3
1
4
2

Sample Input 3

18 8
WWBBWBWBBWWBWBWBWW
1 18
1 6
3 10
5 15
8 18
2 2
7 13
11 17

Sample Output 3

145
18
28
61
55
1
25
28

Sample Input 4

50 15
WBWBWBBBWWBBWBWBWWWBBBWBWBBWBWWBWBWBBWWBWBWBWWBBBW
1 50
1 10
11 20
21 30
31 40
41 50
5 25
10 35
15 45
2 49
7 7
18 22
24 37
33 50
3 14

Sample Output 4

875
41
39
42
51
45
171
270
397
809
1
10
90
146
52

Sample Input 5

1 1
B
1 1

Sample Output 5

1