A - 455

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

配点 : 100

問題文

整数 A, B, C が与えられます。A \neq B かつ B = C であるならば Yes を、そうでないならば No を出力してください。

制約

  • 1 \leq A, B, C \leq 9
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

A B C

出力

問題文中の指示に従って Yes, No のいずれかを出力せよ。


入力例 1

4 5 5

出力例 1

Yes

4 \neq 5 かつ 5 = 5 なので Yes を出力します。


入力例 2

1 3 7

出力例 2

No

1 \neq 3 ですが 3 = 7 ではないので No を出力します。


入力例 3

6 6 6

出力例 3

No

Score : 100 points

Problem Statement

You are given integers A, B, C. If A \neq B and B = C, output Yes; otherwise, output No.

Constraints

  • 1 \leq A, B, C \leq 9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

A B C

Output

Output Yes or No according to the instructions in the problem statement.


Sample Input 1

4 5 5

Sample Output 1

Yes

Since 4 \neq 5 and 5 = 5, output Yes.


Sample Input 2

1 3 7

Sample Output 2

No

Although 1 \neq 3, we don't have 3 = 7, so output No.


Sample Input 3

6 6 6

Sample Output 3

No
B - Spiral Galaxy

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

配点 : 200

問題文

HW 列のグリッドがあります。このグリッドの上から i 行目、左から j 列目のマスをマス (i, j) と表記します。

グリッドの各マスは白または黒で塗られています。グリッドの情報は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H によって与えられ、S_ij 文字目が . のときマス (i, j) は白で、S_ij 文字目が # のときマス (i, j) は黒で塗られています。

グリッドの長方形領域であって、点対称に塗られているものの個数を求めてください。

より形式的には、以下の条件をすべて満たす整数の組 (h_1, h_2, w_1, w_2) の個数を求めてください。

  • 1 \leq h_1 \leq h_2 \leq H
  • 1 \leq w_1 \leq w_2 \leq W
  • h_1 \leq i \leq h_2 かつ w_1 \leq j \leq w_2 を満たすすべての整数 i, j についてマス (i, j) とマス (h_1 + h_2 - i, w_1 + w_2 - j) は同じ色で塗られている

制約

  • 1 \leq H, W \leq 10
  • H, W は整数
  • S_i., # からなる長さ W の文字列

入力

入力は以下の形式で標準入力から与えられる。

H W
S_1
S_2
\vdots
S_H

出力

答えを出力せよ。


入力例 1

3 2
.#
#.
##

出力例 1

10

上の図のように、答えは 10 となります。


入力例 2

4 5
.#.#.
####.
##..#
....#

出力例 2

54

Score : 200 points

Problem Statement

There is a grid with H rows and W columns. The cell at the i-th row from the top and j-th column from the left is denoted as cell (i, j).

Each cell of the grid is colored white or black. The information of the grid is given by H strings S_1, S_2, \ldots, S_H each of length W: cell (i, j) is white if the j-th character of S_i is ., and black if it is #.

Find the number of rectangular regions of the grid that are point-symmetrically colored.

More formally, find the number of integer tuples (h_1, h_2, w_1, w_2) satisfying all of the following conditions:

  • 1 \leq h_1 \leq h_2 \leq H
  • 1 \leq w_1 \leq w_2 \leq W
  • For all integers i, j satisfying h_1 \leq i \leq h_2 and w_1 \leq j \leq w_2, cell (i, j) and cell (h_1 + h_2 - i, w_1 + w_2 - j) have the same color.

Constraints

  • 1 \leq H, W \leq 10
  • H and W are integers.
  • S_i is a string of length W consisting of . and #.

Input

The input is given from Standard Input in the following format:

H W
S_1
S_2
\vdots
S_H

Output

Output the answer.


Sample Input 1

3 2
.#
#.
##

Sample Output 1

10

As shown in the figure above, the answer is 10.


Sample Input 2

4 5
.#.#.
####.
##..#
....#

Sample Output 2

54
C - Vanish

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

配点 : 300

問題文

整数列 A = (A_1, A_2, \ldots, A_N) が与えられます。

以下の操作をちょうど K 回行った後の A の各要素の和として考えられる最小値を求めてください。

  • 整数 x を選ぶ。A_i = x なる各 i について A_i の値を 0 に置き換える。

制約

  • 1 \leq K \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N K
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

6 2
7 2 7 2 2 9

出力例 1

6

はじめ、A = (7, 2, 7, 2, 2, 9) です。

x = 9 として操作を行うと、A = (7, 2, 7, 2, 2, 0) となります。

次に x = 7 として操作を行うと、A = (0, 2, 0, 2, 2, 0) となります。

このとき、A の各要素の和は 0 + 2 + 0 + 2 + 2 + 0 = 6 となります。


入力例 2

8 6
1 2 3 4 1 2 3 4

出力例 2

0

入力例 3

10 2
3 3 4 1 1 3 3 1 5 1

出力例 3

8

Score : 300 points

Problem Statement

You are given an integer sequence A = (A_1, A_2, \ldots, A_N).

Find the minimum possible sum of all elements of A after performing the following operation exactly K times.

  • Choose an integer x. For each i such that A_i = x, replace the value of A_i with 0.

Constraints

  • 1 \leq K \leq N \leq 3 \times 10^5
  • 1 \leq A_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N K
A_1 A_2 \ldots A_N

Output

Output the answer.


Sample Input 1

6 2
7 2 7 2 2 9

Sample Output 1

6

Initially, A = (7, 2, 7, 2, 2, 9).

Performing the operation with x = 9 gives A = (7, 2, 7, 2, 2, 0).

Next, performing the operation with x = 7 gives A = (0, 2, 0, 2, 2, 0).

At this point, the sum of all elements of A is 0 + 2 + 0 + 2 + 2 + 0 = 6.


Sample Input 2

8 6
1 2 3 4 1 2 3 4

Sample Output 2

0

Sample Input 3

10 2
3 3 4 1 1 3 3 1 5 1

Sample Output 3

8
D - Card Pile Query

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

配点 : 400

問題文

カードが N 枚とカードの山が N 個あります。

カードとカードの山にはそれぞれ 1, 2, \ldots, N の番号が付けられており、はじめ、山 i にはカード i のみが積まれています。

あなたは、各 i = 1, 2, \ldots, Q に対して以下の操作をこの順に行います。

  • カード C_i およびカード C_i の上に積まれているカードを順序を保ってカード P_i の上に移動させる。ただし、操作を行う直前の時点でカード C_i とカード P_i は異なる山にあり、カード P_i はある山の一番上に積まれていることが保証される。

すべての操作を終えた後、各山に積まれているカードの枚数を求めてください。

制約

  • 1 \leq N, Q \leq 3 \times 10^5
  • 1 \leq C_i, P_i \leq N
  • 操作を順に行ったとき、各操作を行う直前の時点でカード C_i とカード P_i は異なる山にある
  • 操作を順に行ったとき、各操作を行う直前の時点でカード P_i はある山の一番上に積まれている
  • 入力される値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N Q
C_1 P_1
C_2 P_2
\vdots
C_Q P_Q

出力

最終的に山 i に積まれているカードの枚数を A_i として A_1, A_2, \ldots, A_N をこの順に空白区切りで出力せよ。


入力例 1

5 4
1 3
4 5
1 4
4 2

出力例 1

0 3 1 0 1

一連の操作は以下の図のように行われます。


入力例 2

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

出力例 2

2 0 0 4 0 0 1

Score : 400 points

Problem Statement

There are N cards and N piles of cards.

The cards and the piles are numbered 1, 2, \ldots, N. Initially, pile i contains only card i.

Perform the following operation for each i = 1, 2, \ldots, Q in order:

  • Move card C_i and all cards stacked on top of card C_i, preserving their order, on top of card P_i. It is guaranteed that immediately before performing the operation, cards C_i and P_i are in different piles, and card P_i is on top of some pile.

Find the number of cards in each pile after all operations are completed.

Constraints

  • 1 \leq N, Q \leq 3 \times 10^5
  • 1 \leq C_i, P_i \leq N
  • When the operations are performed in order, immediately before each operation, cards C_i and P_i are in different piles.
  • When the operations are performed in order, immediately before each operation, card P_i is on top of some pile.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N Q
C_1 P_1
C_2 P_2
\vdots
C_Q P_Q

Output

Let A_i be the number of cards in pile i at the end. Output A_1, A_2, \ldots, A_N in this order, separated by spaces.


Sample Input 1

5 4
1 3
4 5
1 4
4 2

Sample Output 1

0 3 1 0 1

The sequence of operations proceeds as shown in the figure below.


Sample Input 2

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

Sample Output 2

2 0 0 4 0 0 1
E - Unbalanced ABC Substrings

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

配点 : 450

問題文

A, B, C3 種類の文字のみからなる長さ N の文字列 S が与えられます。
S の空でない部分文字列は \frac{N(N+1)}{2} 個ありますが、そのうち以下の条件を満たすものがいくつあるか求めてください。

  • A, B, C の出現回数が相異なる。

ただし、2 つの部分文字列は、S から取り出す場所が異なれば文字列として同じでも区別して数えることに注意してください。

部分文字列とは? S部分文字列とは、S の先頭から 0 文字以上、末尾から 0 文字以上削除して得られる文字列のことをいいます。 例えば、ABABC の部分文字列ですが、ACABC の部分文字列ではありません。

制約

  • 1 \leq N \leq 2 \times 10^5
  • |S|=N
  • N は整数
  • SA, B, C3 種類の文字のみからなる文字列

入力

入力は以下の形式で標準入力から与えられる。

N
S

出力

答えを 1 行で出力せよ。


入力例 1

6
AABBCC

出力例 1

4

S の先頭から 0 文字、末尾から 3 文字を削除すると AAB となり、これは条件を満たします。
S の先頭から 1 文字、末尾から 2 文字を削除すると ABB となり、これは条件を満たします。
S の先頭から 2 文字、末尾から 1 文字を削除すると BBC となり、これは条件を満たします。
S の先頭から 3 文字、末尾から 0 文字を削除すると BCC となり、これは条件を満たします。
また、これら以外の部分文字列は条件を満たしません。


入力例 2

6
ABCABC

出力例 2

0

どの部分文字列も条件を満たしません。


入力例 3

10
ACABCAABAB

出力例 3

17

Score : 450 points

Problem Statement

You are given a string S of length N consisting of the three characters A, B, and C.
There are \frac{N(N+1)}{2} non-empty substrings of S; find how many of them satisfy the following condition:

  • The numbers of occurrences of A, B, and C are all distinct from each other.

Count two substrings separately if they are taken from different positions in S, even if they are identical as strings.

What is a substring? A substring of S is a string obtained by deleting zero or more characters from the beginning and zero or more characters from the end of S. For example, AB is a substring of ABC, but AC is not a substring of ABC.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • |S|=N
  • N is an integer.
  • S is a string consisting of the three characters A, B, and C.

Input

The input is given from Standard Input in the following format:

N
S

Output

Output the answer on a single line.


Sample Input 1

6
AABBCC

Sample Output 1

4

Deleting 0 characters from the beginning and 3 characters from the end of S gives AAB, which satisfies the condition.
Deleting 1 character from the beginning and 2 characters from the end of S gives ABB, which satisfies the condition.
Deleting 2 characters from the beginning and 1 character from the end of S gives BBC, which satisfies the condition.
Deleting 3 characters from the beginning and 0 characters from the end of S gives BCC, which satisfies the condition.
No other substrings satisfy the condition.


Sample Input 2

6
ABCABC

Sample Output 2

0

No substring satisfies the condition.


Sample Input 3

10
ACABCAABAB

Sample Output 3

17
F - Merge Slimes 2

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

配点 : 525

問題文

長さ N の非負整数列 A があり、最初はすべての要素が 0 です。 Q 個のクエリが与えられるので順に処理してください。
q 番目のクエリでは 1 \leq l_q \leq r_q \leq N を満たす整数 l_q,r_q と正整数 a_q が与えられるので、以下を順に行ってください。

  • A_{l_q}, A_{{l_q}+1}, \dots, A_{r_q}a_q を足す。
  • その後、M=r_q-l_q+1, \; B=(B_1,B_2,\dots,B_M)=(A_{l_q}, A_{l_q+1}, \dots, A_{r_q}) として、以下の問題の答えを求める。

M 個のスライム 1,2,\dots,M があって、m 個目のスライムの重さは B_m です。
2 個のスライムを選び、合成させるという操作を M-1 回繰り返します。
重さが x,y のスライムを合成させると重さ x+y のスライムが出現し、元の 2 個のスライムは消えます。このとき x \times y のコストがかかります。
M-1 回の操作のコストの総和としてあり得る値の最小値を 998244353 で割った余りを求めてください。

各クエリでの A に対する変更内容はその後にも引き継がれることに注意してください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq l_q \leq r_q \leq N
  • 1 \leq a_q \leq 10^9
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N Q
l_1 r_1 a_1
l_2 r_2 a_2
\vdots
l_Q r_Q a_Q

出力

答えを合計 Q 行で出力せよ。 q 行目には、q 番目のクエリの答えを出力せよ。


入力例 1

5 4
1 3 22
3 4 13
5 5 455
1 5 1000000000

出力例 1

1452
455
0
21421644

1 番目のクエリの後、A=(22,22,22,0,0) となり、B=(22,22,22) です。1 回目に 1 つ目のスライムと 3 つ目のスライムを合成すると、そのコストは 22 \times 22=484 です。次に残った 2 つのスライムを合成するとそのコストは 22 \times 44=968 です。このときコストの合計は 484+968=1452 です。また、コストの合計をこれよりも小さくすることはできません。
2 番目のクエリの後、A=(22,22,35,13,0) となり、B=(35,13) です。求める答えは 35 \times 13 = 455 です。

Score : 525 points

Problem Statement

There is a sequence A of N non-negative integers, all initially 0. Process Q queries given in order.
For the q-th query, you are given integers l_q, r_q satisfying 1 \leq l_q \leq r_q \leq N and a positive integer a_q. Perform the following in order:

  • Add a_q to each of A_{l_q}, A_{{l_q}+1}, \dots, A_{r_q}.
  • Then, letting M=r_q-l_q+1 and B=(B_1,B_2,\dots,B_M)=(A_{l_q}, A_{l_q+1}, \dots, A_{r_q}), find the answer to the following problem:

There are M slimes 1,2,\dots,M, where the m-th slime has weight B_m.
Repeat the operation of choosing two slimes and merging them M-1 times.
When slimes of weights x and y are merged, a slime of weight x+y appears and the original two slimes disappear. This incurs a cost of x \times y.
Find, modulo 998244353, the minimum possible total cost of the M-1 operations.

Note that the changes made on A in each query carry over to subsequent queries.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq Q \leq 10^5
  • 1 \leq l_q \leq r_q \leq N
  • 1 \leq a_q \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N Q
l_1 r_1 a_1
l_2 r_2 a_2
\vdots
l_Q r_Q a_Q

Output

Output the answers over Q lines in total. The q-th line should contain the answer to the q-th query.


Sample Input 1

5 4
1 3 22
3 4 13
5 5 455
1 5 1000000000

Sample Output 1

1452
455
0
21421644

After the first query, A=(22,22,22,0,0) and B=(22,22,22). Merging the first and third slimes first incurs a cost of 22 \times 22=484. Then merging the remaining two slimes incurs a cost of 22 \times 44=968. The total cost is 484+968=1452. Moreover, the total cost cannot be made smaller than this.
After the second query, A=(22,22,35,13,0) and B=(35,13). The answer is 35 \times 13 = 455.

G - Balanced Subarrays

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

配点 : 625

問題文

長さ N の正整数列 A が与えられます。
A の空でない連続部分列は \frac{N(N+1)}{2} 個ありますが、そのうち以下の条件を満たす数列 X をバランスの良い数列と呼ぶことにします。

  • Xに含まれる各整数は X 内に同じ回数登場する

k=1,2,\dots,K について、以下の 2 種類の値をそれぞれ求めてください。

  • バランスの良い数列のうち、各整数が B_k 回ずつ登場するものの個数
  • バランスの良い数列のうち、B_k 種類の整数が登場するものの個数

ただし、2 つの連続部分列は、A から取り出す場所が異なれば数列として同じでも区別して数えることに注意してください。

1 つの入力につき、T 個のテストケースを解いてください。

制約

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq \min(N,10)
  • 1 \leq A_i \leq N
  • 1 \leq B_k \leq N
  • B_1,B_2,\dots,B_K は互いに相異なる
  • 全てのテストケースにおける N の総和は 2 \times 10^5 以下
  • 入力される値は全て整数

入力

入力は以下の形式で標準入力から与えられる。

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各ケースは以下の形式で与えられる。

N K  
A_1 A_2 \dots A_N  
B_1 B_2 \dots B_K  

出力

答えを以下の形式で出力せよ。

\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

各ケースでは、バランスの良い数列のうち、各整数が B_k 回ずつ登場するものの個数を c_{k,1}B_k 種類の整数が登場するものの個数を c_{k,2} として、以下の形式で出力せよ。

c_{1,1} c_{1,2}  
c_{2,1} c_{2,2}  
\vdots
c_{K,1} c_{K,2}  

入力例 1

3
4 2
1 2 1 2
2 1
1 1
1
1
7 7
1 5 5 1 5 1 2
1 2 3 4 5 6 7

出力例 1

1 4
7 4
1 1
13 8
3 8
1 1
0 0
0 0
0 0
0 0

1 つ目のテストケースについて、Al 番目から r 番目までの連続部分列がバランスの良い数列であるような (l,r) の組は (1,1),(1,2),(1,4),(2,2),(2,3),(3,3),(3,4),(4,4)8 つです。
そのうち、各整数が 2 回ずつ登場するのは (1,4)1 つで、2 種類の整数が登場するのは (1,2),(1,4),(2,3),(3,4)4 つです。
したがって、k=1 のときの答えとしては 1 4 を出力してください。
また、各整数が 1 回ずつ登場するのは (1,1),(1,2),(2,2),(2,3),(3,3),(3,4),(4,4)7 つで、1 種類の整数が登場するのは (1,1),(2,2),(3,3),(4,4)4 つです。
したがって、k=2 のときの答えとしては 7 4 を出力してください。

Score : 625 points

Problem Statement

You are given a sequence A of N positive integers.
Among the \frac{N(N+1)}{2} non-empty (contiguous) subarrays of A, we call a sequence X a balanced sequence if and only if it satisfies the following condition:

  • Every integer appearing in X appears the same number of times in X.

For each k=1,2,\dots,K, find the following two values:

  • The number of balanced sequences in which each integer appears exactly B_k times.
  • The number of balanced sequences in which exactly B_k distinct integers appear.

Count two subarrays separately if they are taken from different positions in A, even if they are identical as sequences.

Solve T test cases per input.

Constraints

  • 1 \leq T \leq 2 \times 10^5
  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq \min(N,10)
  • 1 \leq A_i \leq N
  • 1 \leq B_k \leq N
  • B_1,B_2,\dots,B_K are pairwise distinct.
  • The sum of N over all test cases is at most 2 \times 10^5.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

T
\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

Each case is given in the following format:

N K  
A_1 A_2 \dots A_N  
B_1 B_2 \dots B_K  

Output

Output the answers in the following format:

\mathrm{case}_1
\mathrm{case}_2
\vdots
\mathrm{case}_T

For each case, letting c_{k,1} be the number of balanced sequences in which each integer appears exactly B_k times, and c_{k,2} be the number of balanced sequences in which exactly B_k distinct integers appear, output in the following format:

c_{1,1} c_{1,2}  
c_{2,1} c_{2,2}  
\vdots
c_{K,1} c_{K,2}  

Sample Input 1

3
4 2
1 2 1 2
2 1
1 1
1
1
7 7
1 5 5 1 5 1 2
1 2 3 4 5 6 7

Sample Output 1

1 4
7 4
1 1
13 8
3 8
1 1
0 0
0 0
0 0
0 0

For the first test case, the pairs (l,r) such that the subarray of A from the l-th through the r-th element is a balanced sequence are (1,1),(1,2),(1,4),(2,2),(2,3),(3,3),(3,4),(4,4), giving eight instances in total.
Among these, the ones in which each integer appears exactly twice are (1,4), giving one instance, and the ones in which exactly two distinct integers appear are (1,2),(1,4),(2,3),(3,4), giving four instances.
Thus, for k=1, output 1 4.
Also, the ones in which each integer appears exactly once are (1,1),(1,2),(2,2),(2,3),(3,3),(3,4),(4,4), giving seven instances, and the ones in which exactly one distinct integer appears are (1,1),(2,2),(3,3),(4,4), giving four instances.
Thus, for k=2, output 7 4.