A - Test Grading

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233 点

問題文

高橋君は塾の講師として働いています。彼の仕事は、複数のクラスで実施したテストの結果を分析することです。

塾には N 個のクラスがあり、それぞれのクラスには 1 から N までの番号が付けられています。クラス i(1 \leq i \leq N)には M_i 人の生徒が在籍しており、その j 番目(1 \leq j \leq M_i)の生徒のテストの点数は S_{i,j} 点です。

高橋君は、各クラスについて「点数が基準点 K 点以上の生徒の人数」を求めたいと思っています。各クラス i について、テストの点数が K 点以上の生徒の人数 C_i を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 0 \leq K \leq 100
  • 1 \leq M_i \leq 100
  • すべてのクラスの生徒数の合計は 10^6 以下、すなわち \sum_{i=1}^{N} M_i \leq 10^6
  • 0 \leq S_{i,j} \leq 100
  • 入力はすべて整数である

入力

N K
M_1 S_{1,1} S_{1,2} \ldots S_{1,M_1}
M_2 S_{2,1} S_{2,2} \ldots S_{2,M_2}
\vdots
M_N S_{N,1} S_{N,2} \ldots S_{N,M_N}
  • 1 行目には、クラスの数 N と基準点 K がスペース区切りで与えられる。
  • 続く N 行のうち i 行目には、クラス i の生徒数 M_i と、各生徒の点数 S_{i,1}, S_{i,2}, \ldots, S_{i,M_i} がスペース区切りで与えられる。

出力

C_1
C_2
\vdots
C_N
  • N 行にわたって出力する。
  • i 行目には、クラス i において点数が K 点以上の生徒の人数 C_i を出力する。

入力例 1

3 60
5 45 72 60 88 55
3 100 59 60
4 30 40 50 20

出力例 1

3
2
0

入力例 2

5 75
6 80 65 90 75 70 85
4 74 75 76 100
3 50 60 70
8 75 75 75 75 74 74 74 74
2 0 100

出力例 2

4
3
0
4
1

入力例 3

10 50
10 45 55 60 40 70 80 35 50 65 48
8 100 99 98 97 96 95 94 93
5 0 25 50 75 100
12 10 20 30 40 50 60 70 80 90 100 49 51
3 50 50 50
7 49 49 49 49 49 49 49
6 51 52 53 54 55 56
4 0 0 0 0
9 100 100 100 100 100 100 100 100 100
15 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95

出力例 3

6
8
3
7
3
0
6
0
9
10

Score : 233 pts

Problem Statement

Takahashi works as an instructor at a cram school. His job is to analyze the results of tests administered across multiple classes.

The cram school has N classes, each numbered from 1 to N. Class i (1 \leq i \leq N) has M_i students enrolled, and the test score of the j-th student (1 \leq j \leq M_i) is S_{i,j} points.

Takahashi wants to determine, for each class, "the number of students whose score is at least the threshold score K." For each class i, find the number of students C_i whose test score is K points or higher.

Constraints

  • 1 \leq N \leq 10^5
  • 0 \leq K \leq 100
  • 1 \leq M_i \leq 100
  • The total number of students across all classes is at most 10^6, i.e., \sum_{i=1}^{N} M_i \leq 10^6
  • 0 \leq S_{i,j} \leq 100
  • All input values are integers

Input

N K
M_1 S_{1,1} S_{1,2} \ldots S_{1,M_1}
M_2 S_{2,1} S_{2,2} \ldots S_{2,M_2}
\vdots
M_N S_{N,1} S_{N,2} \ldots S_{N,M_N}
  • The first line contains the number of classes N and the threshold score K, separated by a space.
  • In the following N lines, the i-th line contains the number of students M_i in class i, followed by each student's score S_{i,1}, S_{i,2}, \ldots, S_{i,M_i}, separated by spaces.

Output

C_1
C_2
\vdots
C_N
  • Output N lines.
  • The i-th line should contain C_i, the number of students in class i whose score is K points or higher.

Sample Input 1

3 60
5 45 72 60 88 55
3 100 59 60
4 30 40 50 20

Sample Output 1

3
2
0

Sample Input 2

5 75
6 80 65 90 75 70 85
4 74 75 76 100
3 50 60 70
8 75 75 75 75 74 74 74 74
2 0 100

Sample Output 2

4
3
0
4
1

Sample Input 3

10 50
10 45 55 60 40 70 80 35 50 65 48
8 100 99 98 97 96 95 94 93
5 0 25 50 75 100
12 10 20 30 40 50 60 70 80 90 100 49 51
3 50 50 50
7 49 49 49 49 49 49 49
6 51 52 53 54 55 56
4 0 0 0 0
9 100 100 100 100 100 100 100 100 100
15 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95

Sample Output 3

6
8
3
7
3
0
6
0
9
10
B - Walking Intervals That Include a Favorite Place

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333 点

問題文

高橋君は N 個の区画が一列に並んだ散歩道を毎日歩いています。区画には左から順に 1, 2, \ldots, N の番号が付けられており、i 番目の区画の美しさは整数 A_i で表されます(美しさは負の値をとることもあります)。

高橋君には D 番目の区画というお気に入りの場所があります。高橋君は、散歩道の中から D 番目の区画を必ず含む 連続するちょうど K 個の区画からなる区間を 1 つ選び、その美しさの合計が最大となるようにしたいと考えています。

具体的には、区間の開始位置を表す整数 l を決めて、l 番目から l+K-1 番目までの連続する K 個の区画を選びます。このとき、l は以下の条件をすべて満たす必要があります。

  • 1 \leq l(区間が散歩道の左端からはみ出さない)
  • l + K - 1 \leq N(区間が散歩道の右端からはみ出さない)
  • l \leq D \leq l + K - 1(区間がお気に入りの D 番目の区画を含む)

このような l の選び方のうち、美しさの合計 A_l + A_{l+1} + \cdots + A_{l+K-1} が最大となるときの値を求めてください。

制約

  • 1 \leq N \leq 10^6
  • 1 \leq K \leq N
  • 1 \leq D \leq N
  • -10^9 \leq A_i \leq 10^9
  • 条件を満たす l が少なくとも 1 つ存在することが保証される。
  • 入力はすべて整数である。

入力

N K D
A_1 A_2 \ldots A_N
  • 1 行目には、区画の数 N、選ぶ連続区画の個数 K、お気に入りの区画の番号 D が、スペース区切りで与えられる。
  • 2 行目には、各区画の美しさ A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。A_i は i 番目の区画の美しさを表す。

出力

条件を満たす連続する K 個の区画の美しさの合計の最大値を 1 行で出力せよ。なお、答えが負の値になることもある。


入力例 1

7 3 4
2 -1 5 3 -2 4 1

出力例 1

7

入力例 2

5 2 3
-3 -5 -1 -4 -2

出力例 2

-5

入力例 3

15 5 8
1 -3 4 -2 5 1 -6 3 7 -1 2 8 -4 3 -5

出力例 3

19

入力例 4

20 7 12
3 -1 4 1 5 -9 2 6 -5 3 5 8 -3 7 2 -4 6 1 -2 3

出力例 4

21

入力例 5

1 1 1
-1000000000

出力例 5

-1000000000

Score : 333 pts

Problem Statement

Takahashi walks every day along a promenade consisting of N blocks arranged in a row. The blocks are numbered 1, 2, \ldots, N from left to right, and the beauty of the i-th block is represented by an integer A_i (the beauty can be negative).

Takahashi has a favorite place, which is the D-th block. He wants to choose an interval of exactly K consecutive blocks from the promenade that must include the D-th block, such that the total beauty is maximized.

Specifically, he decides an integer l representing the starting position of the interval, and selects K consecutive blocks from the l-th to the (l+K-1)-th. The value of l must satisfy all of the following conditions:

  • 1 \leq l (the interval does not extend beyond the left end of the promenade)
  • l + K - 1 \leq N (the interval does not extend beyond the right end of the promenade)
  • l \leq D \leq l + K - 1 (the interval includes the favorite D-th block)

Among all valid choices of l, find the maximum value of the total beauty A_l + A_{l+1} + \cdots + A_{l+K-1}.

Constraints

  • 1 \leq N \leq 10^6
  • 1 \leq K \leq N
  • 1 \leq D \leq N
  • -10^9 \leq A_i \leq 10^9
  • It is guaranteed that at least one valid l exists.
  • All input values are integers.

Input

N K D
A_1 A_2 \ldots A_N
  • The first line contains the number of blocks N, the number of consecutive blocks to choose K, and the favorite block number D, separated by spaces.
  • The second line contains the beauty of each block A_1, A_2, \ldots, A_N, separated by spaces. A_i represents the beauty of the i-th block.

Output

Print in one line the maximum total beauty of K consecutive blocks satisfying the conditions. Note that the answer may be negative.


Sample Input 1

7 3 4
2 -1 5 3 -2 4 1

Sample Output 1

7

Sample Input 2

5 2 3
-3 -5 -1 -4 -2

Sample Output 2

-5

Sample Input 3

15 5 8
1 -3 4 -2 5 1 -6 3 7 -1 2 8 -4 3 -5

Sample Output 3

19

Sample Input 4

20 7 12
3 -1 4 1 5 -9 2 6 -5 3 5 8 -3 7 2 -4 6 1 -2 3

Sample Output 4

21

Sample Input 5

1 1 1
-1000000000

Sample Output 5

-1000000000
C - Flow Control of Waterways

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 366 点

問題文

高橋君は N 個の分岐点からなる灌漑用の水路ネットワークを管理しています。分岐点には 1 から N までの番号が付けられており、分岐点 1 が最上流の水源です。分岐点 i( 2 \leq i \leq N )の親は分岐点 P_i であり、全体として分岐点 1 を根とする根付き木をなしています。水は親から子へと流れる構造になっています。

各分岐点 i には、現在 V_i リットルの水が溜まっています。

高橋君は「排水」操作を 0 回以上繰り返し行うことで、全ての分岐点の水量をちょうど 0 にしたいと考えています。操作の回数に上限はなく、また各操作で選ぶ分岐点は自由に決めてよく、同じ分岐点を何度選んでもかまいません。

1 回の「排水」操作は次のように行います。

  • 分岐点を 1 つ選ぶ(選んだ分岐点を u とする)。
  • u から根(分岐点 1 )までの木上のパスに含まれる全ての分岐点( u 自身および分岐点 1 を含む)の水量を、同時に 1 ずつ減少させる。

例えば u = 1 の場合は、分岐点 1 の水量のみが 1 減少します。

ただし、各操作の実行後において、全ての分岐点の水量が 0 以上でなければなりません。 すなわち、操作の結果いずれかの分岐点の水量が負になるような操作は行うことができません。

全ての分岐点の水量をちょうど 0 にすることが可能かどうかを判定してください。可能ならば Yes を、不可能ならば No を出力してください。

制約

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq P_i \leq i - 1( 2 \leq i \leq N )
  • 0 \leq V_i \leq 10^9( 1 \leq i \leq N )
  • 入力はすべて整数である。

入力

N
P_2 P_3 \ldots P_N
V_1 V_2 \ldots V_N
  • 1 行目には、分岐点の数を表す整数 N が与えられる。
  • 2 行目には、分岐点 2 から分岐点 N までの親を表す N-1 個の整数 P_2, P_3, \ldots, P_N がスペース区切りで与えられる。ただし N = 1 の場合、2 行目には何も書かれていない(空行である)。
  • 3 行目には、各分岐点の初期水量を表す N 個の整数 V_1, V_2, \ldots, V_N がスペース区切りで与えられる。

出力

全ての分岐点の水量をちょうど 0 にすることが可能ならば Yes を、不可能ならば No を 1 行で出力せよ。


入力例 1

5
1 1 2 2
7 4 2 1 2

出力例 1

Yes

入力例 2

3
1 1
1 1 1

出力例 2

No

入力例 3

12
1 1 2 2 3 3 4 4 6 6 10
20 8 10 3 3 10 0 2 1 5 4 2

出力例 3

Yes

入力例 4

25
1 1 1 2 2 3 3 4 4 5 5 6 7 7 8 9 10 10 12 12 14 16 16 20
35 10 15 6 7 2 9 4 1 5 1 4 2 6 3 4 0 4 1 3 2 5 1 2 3

出力例 4

No

入力例 5

1

1000000000

出力例 5

Yes

Score : 366 pts

Problem Statement

Takahashi manages an irrigation water channel network consisting of N junction points. The junction points are numbered from 1 to N, and junction point 1 is the uppermost water source. The parent of junction point i (2 \leq i \leq N) is junction point P_i, and the entire structure forms a rooted tree with junction point 1 as the root. Water flows from parent to child.

Each junction point i currently holds V_i liters of water.

Takahashi wants to make the water volume at all junction points exactly 0 by performing the "drain" operation zero or more times. There is no upper limit on the number of operations, and the junction point chosen for each operation can be freely decided. The same junction point may be chosen multiple times.

One "drain" operation is performed as follows:

  • Choose one junction point (let the chosen junction point be u).
  • Simultaneously decrease the water volume by 1 at all junction points on the path from u to the root (junction point 1) in the tree (including u itself and junction point 1).

For example, if u = 1, only the water volume at junction point 1 decreases by 1.

However, after each operation is performed, the water volume at all junction points must be 0 or greater. That is, an operation that would result in the water volume at any junction point becoming negative cannot be performed.

Determine whether it is possible to make the water volume at all junction points exactly 0. If possible, output Yes; if impossible, output No.

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq P_i \leq i - 1 (2 \leq i \leq N)
  • 0 \leq V_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

N
P_2 P_3 \ldots P_N
V_1 V_2 \ldots V_N
  • The first line contains an integer N representing the number of junction points.
  • The second line contains N-1 integers P_2, P_3, \ldots, P_N separated by spaces, representing the parents of junction points 2 through N. If N = 1, the second line is empty.
  • The third line contains N integers V_1, V_2, \ldots, V_N separated by spaces, representing the initial water volume at each junction point.

Output

Output Yes on a single line if it is possible to make the water volume at all junction points exactly 0, and No otherwise.


Sample Input 1

5
1 1 2 2
7 4 2 1 2

Sample Output 1

Yes

Sample Input 2

3
1 1
1 1 1

Sample Output 2

No

Sample Input 3

12
1 1 2 2 3 3 4 4 6 6 10
20 8 10 3 3 10 0 2 1 5 4 2

Sample Output 3

Yes

Sample Input 4

25
1 1 1 2 2 3 3 4 4 5 5 6 7 7 8 9 10 10 12 12 14 16 16 20
35 10 15 6 7 2 9 4 1 5 1 4 2 6 3 4 0 4 1 3 2 5 1 2 3

Sample Output 4

No

Sample Input 5

1

1000000000

Sample Output 5

Yes
D - Mountain Hiking

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

高橋君は山岳地帯でのハイキングを計画しています。この山岳地帯には N 個の地点があり、地点 i (1 \leq i \leq N) の標高は H_i です。

地点と地点の間には M 本の登山道が存在し、j 番目 (1 \leq j \leq M) の登山道は地点 U_j と地点 V_j を双方向に結んでいます。j 番目の登山道を1回通るときの疲労度は、両端の地点の標高差の絶対値 |H_{U_j} - H_{V_j}| です。

高橋君は地点 1(出発地点)から地点 N(目的地)まで、登山道のみを使って移動します。移動の際、同じ地点を複数回訪れたり、同じ登山道を複数回通ったりすることも許されます。同じ登山道を複数回通った場合は、通るたびにその登山道の疲労度が加算されます。

ただし、疲労度が K 以下の登山道のみ通ることができます。疲労度が K より大きい登山道は通ることができません。

通行可能な登山道のみを使って地点 1 から地点 N まで移動するとき、通った登山道の疲労度の合計の最小値を求めてください。

通行可能な登山道のみを使って地点 1 から地点 N まで到達できない場合は -1 を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq 10^9
  • 0 \leq H_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq U_j < V_j \leq N (1 \leq j \leq M)
  • 同じ地点の組を結ぶ登山道は高々 1 本である(多重辺はない)
  • 入力はすべて整数である

入力

N M K
H_1 H_2 \ldots H_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M
  • 1 行目には、地点の数 N、登山道の数 M、1 本の登山道あたりの疲労度の上限 K が、スペース区切りで与えられる。
  • 2 行目には、各地点の標高 H_1, H_2, \ldots, H_N が、スペース区切りで与えられる。
  • 3 行目から M+2 行目までの M 行では、登山道の情報が与えられる。
  • そのうち j 行目(3 行目から数えて j 番目)では、j 番目の登山道が結ぶ 2 つの地点の番号 U_j と V_j が、スペース区切りで与えられる。

出力

通行可能な登山道のみを使って地点 1 から地点 N まで移動できる場合、疲労度の合計の最小値を 1 行で出力してください。到達できない場合は -1 を出力してください。


入力例 1

5 6 5
10 14 8 12 16
1 2
1 3
2 4
3 4
2 5
4 5

出力例 1

6

入力例 2

4 4 3
0 10 20 30
1 2
2 3
3 4
1 4

出力例 2

-1

入力例 3

7 10 10
0 8 5 15 12 20 25
1 2
1 3
2 4
3 4
3 5
4 6
5 6
5 7
6 7
2 5

出力例 3

25

Score : 400 pts

Problem Statement

Takahashi is planning a hike in a mountainous area. There are N locations in this mountainous area, and the elevation of location i (1 \leq i \leq N) is H_i.

There are M trails between locations, and the j-th (1 \leq j \leq M) trail bidirectionally connects location U_j and location V_j. The fatigue cost of traversing the j-th trail once is the absolute difference in elevation between its two endpoints, |H_{U_j} - H_{V_j}|.

Takahashi will travel from location 1 (the starting point) to location N (the destination) using only trails. During the trip, he is allowed to visit the same location multiple times and traverse the same trail multiple times. If the same trail is traversed multiple times, its fatigue cost is added each time it is traversed.

However, he can only use trails whose fatigue cost is at most K. Trails with fatigue cost greater than K cannot be used.

Find the minimum total fatigue cost when traveling from location 1 to location N using only passable trails.

If it is impossible to reach location N from location 1 using only passable trails, output -1.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq 10^9
  • 0 \leq H_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq U_j < V_j \leq N (1 \leq j \leq M)
  • There is at most one trail connecting any pair of locations (no multiple edges)
  • All input values are integers

Input

N M K
H_1 H_2 \ldots H_N
U_1 V_1
U_2 V_2
\vdots
U_M V_M
  • The first line contains the number of locations N, the number of trails M, and the upper limit on fatigue cost per trail K, separated by spaces.
  • The second line contains the elevations H_1, H_2, \ldots, H_N of each location, separated by spaces.
  • The following M lines (from the 3rd line to the (M+2)-th line) contain information about the trails.
  • The j-th of these lines contains the numbers U_j and V_j of the two locations connected by the j-th trail, separated by spaces.

Output

If it is possible to travel from location 1 to location N using only passable trails, output the minimum total fatigue cost on a single line. If it is impossible to reach the destination, output -1.


Sample Input 1

5 6 5
10 14 8 12 16
1 2
1 3
2 4
3 4
2 5
4 5

Sample Output 1

6

Sample Input 2

4 4 3
0 10 20 30
1 2
2 3
3 4
1 4

Sample Output 2

-1

Sample Input 3

7 10 10
0 8 5 15 12 20 25
1 2
1 3
2 4
3 4
3 5
4 6
5 6
5 7
6 7
2 5

Sample Output 3

25
E - Pattern Search in DNA Sequences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433 点

問題文

高橋君は生物情報学の研究室で、遺伝子の発現レベルの解析を行っています。この研究では、各サンプルの各位置における発現レベルを H(高)、M(中)、L(低)の3種類の文字で表し、それらを並べた文字列として記録しています。

研究室には N 個のサンプルがあり、それぞれの発現レベル列が文字列 S_1, S_2, \ldots, S_N として記録されています。また、高橋君は特定の機能に関連すると考えられる基準パターン P を持っています。

青木君は高橋君の研究を手伝うことになり、基準パターンの一部を反転させた変形パターンが各サンプル中に何回出現するかを調べる解析プログラムを作ることにしました。

具体的には、Q 個のクエリが与えられます。j 番目(1 \leq j \leq Q)のクエリでは、サンプル番号 i_j、区間の左端 l_j、区間の右端 r_j の整数の組 (i_j, l_j, r_j) が与えられ、以下の手順で答えを求めます。

変形パターンの生成: 基準パターン P の l_j 文字目から r_j 文字目まで(1-indexed、両端含む)を反転し、それ以外の部分はそのまま保持することで、変形パターン P'_j を生成します。P'_j の長さは P の長さ |P| と等しくなります。

例えば、P = HMLLM のとき l_j = 2, r_j = 4 ならば、2文字目から4文字目の MLL を反転して LLM とし、P'_j = HLLMM となります。

出現回数の計算: サンプル S_{i_j} の中で、P'_j と一致する長さ |P| の連続部分文字列の個数を数えます。より正確には、S_{i_j} の a 文字目から a + |P| - 1 文字目までの部分文字列が P'_j と一致するような開始位置 a(1 \leq a \leq |S_{i_j}| - |P| + 1)の個数を求めます。|S_{i_j}| < |P| の場合、条件を満たす開始位置は存在しないため、出現回数は 0 です。開始位置が異なれば、出現箇所が重なっていてもそれぞれ別々に数えます。

なお、変形操作はクエリごとに独立であり、毎回元の基準パターン P に対して操作を行います。

各クエリについて、上記の出現回数を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq |P| \leq 5 \times 10^5
  • 1 \leq |S_i| \leq 5 \times 10^5(1 \leq i \leq N)
  • \sum_{i=1}^{N} |S_i| \leq 5 \times 10^5
  • P および各 S_i は H, M, L のみからなる
  • 1 \leq i_j \leq N(1 \leq j \leq Q)
  • 1 \leq l_j \leq r_j \leq |P|(1 \leq j \leq Q)
  • 入力はすべて整数または文字列で与えられる

入力

N Q
P
S_1
S_2
\vdots
S_N
i_1 l_1 r_1
i_2 l_2 r_2
\vdots
i_Q l_Q r_Q
  • 1 行目には、サンプル数を表す整数 N とクエリ数を表す整数 Q が、スペース区切りで与えられる。
  • 2 行目には、基準パターンを表す文字列 P が与えられる。P は H, M, L のみからなる。
  • 続く N 行には、各サンプルの発現レベル列を表す文字列 S_i(1 \leq i \leq N)が 1 行に 1 つずつ与えられる。各 S_i は H, M, L のみからなる。
  • 続く Q 行には、各クエリの情報が 1 行に 1 つずつ与えられる。j 番目のクエリでは、検索対象のサンプル番号 i_j、反転する区間の左端 l_j、右端 r_j が、それぞれ整数としてスペース区切りで与えられる(1 \leq j \leq Q)。

出力

Q 行出力せよ。j 行目(1 \leq j \leq Q)には、j 番目のクエリに対する答え、すなわち変形パターン P'_j がサンプル S_{i_j} 中に連続部分文字列として出現する回数を整数で出力せよ。


入力例 1

2 4
HMLLM
HLLMMHMLLMHMLLM
LLMHMHLLMM
1 2 4
1 1 5
2 1 3
2 5 5

出力例 1

1
1
0
0

入力例 2

3 5
HML
HMLHML
LMHLMH
HHHH
1 1 1
1 1 3
2 1 3
3 2 2
2 2 3

出力例 2

2
0
2
0
1

入力例 3

5 12
HMLLHMMHLH
HMLLHMMHLHHMLLHMMHLH
HLMMLHMHLHMLLHMMHLLH
LLLLHHHHMMMMHMLLHMMHLH
MLHMLHMLHMLHMLHMLHMLH
HHMLLHMMHLHLLHMMHLLMHH
1 1 10
1 3 7
2 2 9
2 5 5
3 1 4
3 7 10
4 1 3
4 4 8
5 2 6
5 1 10
1 6 10
2 1 1

出力例 3

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

入力例 4

8 20
HMLLMHHLMLMHHLLMMHLMHLML
HMLLMHHLMLMHHLLMMHLMHLMLHMLLMHHLMLMHHLLMMHLMHLML
LLHHMMLMLHHMLLMHMLHMLMLLHMMHHLLMMHLMHLMLHMLLMHH
MMMMHHHHLLLLHMLLMHHLMLMHHLLMMHLMHLMLLLLLHHHHMMMM
HMLHMLHMLHMLHMLHMLHMLHMLHMLHMLHMLHML
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
MLMLMLMLMLMLMLMLMLMLMLMLMLMLMLMLMLML
HMLLMHHLMLMHHLLMMHLMHLMLLMLHMHMLMMHLLHHMMLMLHH
1 1 24
1 6 18
1 12 12
2 1 5
2 10 24
3 4 20
3 15 24
4 1 3
4 2 23
5 1 24
5 7 7
6 1 1
6 20 24
7 3 22
7 8 16
8 1 24
8 5 14
8 11 21
2 6 6
3 1 24

出力例 4

0
0
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

入力例 5

1 1
H
M
1 1 1

出力例 5

0

Score : 433 pts

Problem Statement

Takahashi is working in a bioinformatics laboratory, analyzing gene expression levels. In this research, the expression level at each position of each sample is represented by one of three characters: H (high), M (medium), or L (low), and recorded as a string of these characters.

The laboratory has N samples, and their expression level sequences are recorded as strings S_1, S_2, \ldots, S_N. Takahashi also has a reference pattern P that is believed to be related to a specific function.

Aoki has decided to help Takahashi's research by creating an analysis program that counts how many times a modified pattern—obtained by reversing a part of the reference pattern—appears in each sample.

Specifically, Q queries are given. For the j-th query (1 \leq j \leq Q), a tuple of integers (i_j, l_j, r_j) is given, representing a sample number i_j, the left endpoint l_j, and the right endpoint r_j of an interval. The answer is determined by the following procedure.

Generation of the modified pattern: Reverse the substring of the reference pattern P from the l_j-th character to the r_j-th character (1-indexed, both endpoints inclusive), while keeping the remaining parts unchanged, to generate the modified pattern P'_j. The length of P'_j equals the length |P| of P.

For example, if P = HMLLM and l_j = 2, r_j = 4, then the substring from the 2nd to 4th characters MLL is reversed to LLM, resulting in P'_j = HLLMM.

Counting occurrences: Count the number of contiguous substrings of length |P| in sample S_{i_j} that match P'_j. More precisely, find the number of starting positions a (1 \leq a \leq |S_{i_j}| - |P| + 1) such that the substring of S_{i_j} from the a-th character to the (a + |P| - 1)-th character matches P'_j. If |S_{i_j}| < |P|, no valid starting position exists, so the occurrence count is 0. Even if occurrences overlap, they are counted separately as long as their starting positions differ.

Note that the modification operation is independent for each query; each time, the operation is applied to the original reference pattern P.

For each query, determine the above occurrence count.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq |P| \leq 5 \times 10^5
  • 1 \leq |S_i| \leq 5 \times 10^5 (1 \leq i \leq N)
  • \sum_{i=1}^{N} |S_i| \leq 5 \times 10^5
  • P and each S_i consist only of H, M, L
  • 1 \leq i_j \leq N (1 \leq j \leq Q)
  • 1 \leq l_j \leq r_j \leq |P| (1 \leq j \leq Q)
  • All inputs are given as integers or strings

Input

N Q
P
S_1
S_2
\vdots
S_N
i_1 l_1 r_1
i_2 l_2 r_2
\vdots
i_Q l_Q r_Q
  • The first line contains two space-separated integers N (the number of samples) and Q (the number of queries).
  • The second line contains the string P representing the reference pattern. P consists only of H, M, L.
  • The following N lines each contain a string S_i (1 \leq i \leq N) representing the expression level sequence of each sample. Each S_i consists only of H, M, L.
  • The following Q lines each contain the information for one query. For the j-th query, the sample number i_j, the left endpoint l_j, and the right endpoint r_j of the interval to reverse are given as space-separated integers (1 \leq j \leq Q).

Output

Output Q lines. The j-th line (1 \leq j \leq Q) should contain a single integer: the answer to the j-th query, i.e., the number of times the modified pattern P'_j appears as a contiguous substring in sample S_{i_j}.


Sample Input 1

2 4
HMLLM
HLLMMHMLLMHMLLM
LLMHMHLLMM
1 2 4
1 1 5
2 1 3
2 5 5

Sample Output 1

1
1
0
0

Sample Input 2

3 5
HML
HMLHML
LMHLMH
HHHH
1 1 1
1 1 3
2 1 3
3 2 2
2 2 3

Sample Output 2

2
0
2
0
1

Sample Input 3

5 12
HMLLHMMHLH
HMLLHMMHLHHMLLHMMHLH
HLMMLHMHLHMLLHMMHLLH
LLLLHHHHMMMMHMLLHMMHLH
MLHMLHMLHMLHMLHMLHMLH
HHMLLHMMHLHLLHMMHLLMHH
1 1 10
1 3 7
2 2 9
2 5 5
3 1 4
3 7 10
4 1 3
4 4 8
5 2 6
5 1 10
1 6 10
2 1 1

Sample Output 3

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

Sample Input 4

8 20
HMLLMHHLMLMHHLLMMHLMHLML
HMLLMHHLMLMHHLLMMHLMHLMLHMLLMHHLMLMHHLLMMHLMHLML
LLHHMMLMLHHMLLMHMLHMLMLLHMMHHLLMMHLMHLMLHMLLMHH
MMMMHHHHLLLLHMLLMHHLMLMHHLLMMHLMHLMLLLLLHHHHMMMM
HMLHMLHMLHMLHMLHMLHMLHMLHMLHMLHMLHML
LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
MLMLMLMLMLMLMLMLMLMLMLMLMLMLMLMLMLML
HMLLMHHLMLMHHLLMMHLMHLMLLMLHMHMLMMHLLHHMMLMLHH
1 1 24
1 6 18
1 12 12
2 1 5
2 10 24
3 4 20
3 15 24
4 1 3
4 2 23
5 1 24
5 7 7
6 1 1
6 20 24
7 3 22
7 8 16
8 1 24
8 5 14
8 11 21
2 6 6
3 1 24

Sample Output 4

0
0
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

Sample Input 5

1 1
H
M
1 1 1

Sample Output 5

0