A - サイコロの転がし操作

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

配点 : 266 点

問題文

高橋君は \(N\) 個のサイコロを一直線に並んだマス目の上で転がす実験を行っています。

マス目は、\(1\) から \(M\) までの番号が付いた \(M\) 個のマスが一列に並んだものです。マス番号は一方の端から順に \(1,2,\dots,M\) と振られています。

各サイコロ \(i\)(\(1 \leq i \leq N\))は、初期位置としてマス \(S_i\) に置かれています。複数のサイコロが同じマスに置かれることもありますが、サイコロ同士は互いに影響を及ぼさず、それぞれ独立に扱われます。

各サイコロは一般的なサイコロであり、\(6\) つの面にそれぞれ \(1\) から \(6\) の数が \(1\) つずつ書かれています。向かい合う \(2\) 面の数の和はすべて \(7\) です。

この問題では、サイコロの姿勢を上面の数と前面の数の組で管理します。前面とは、マス番号が増える方向を向いている面のことです。

サイコロ \(i\) の初期状態において、上面の数は \(U_i\)、前面の数は \(F_i\) です。上面と前面は同一の面でも向かい合う面でもないため、\(U_i \neq F_i\) かつ \(U_i+F_i \neq 7\) が保証されます。

高橋君は \(Q\) 回の操作を順番に行います。\(j\) 回目(\(1 \leq j \leq Q\))の操作では、サイコロ番号 \(d_j\) のサイコロを方向 \(c_j\)(+ または -)に \(1\) マスだけ転がします。

転がす直前のそのサイコロの上面の数を \(u\)、前面の数を \(f\) としたとき、位置と姿勢は以下の規則に従って変化します。

  • + 方向(マス番号が増える方向)に転がす場合:
  • 位置が \(1\) 増える。
  • 新しい上面の数は \(7-f\)、新しい前面の数は \(u\) となる。
  • これは、転がす前に前面だった面が下面、背面だった面が上面、上面だった面が前面になる回転です。
  • - 方向(マス番号が減る方向)に転がす場合:
  • 位置が \(1\) 減る。
  • 新しい上面の数は \(f\)、新しい前面の数は \(7-u\) となる。
  • これは、+ 方向と逆の回転であり、転がす前に背面だった面が下面、前面だった面が上面、上面だった面が背面になる回転です。

すべての操作を通じて、各サイコロの位置は常に \(1\) 以上 \(M\) 以下の範囲内に留まることが保証されます。

すべての \(Q\) 回の操作を終えた後、各サイコロの最終的な位置と上面の数を求めてください。前面の数を出力する必要はありません。

制約

  • \(1 \leq N \leq 2\times 10^5\)
  • \(1 \leq M \leq 10^9\)
  • \(1 \leq Q \leq 2\times 10^5\)
  • \(N+Q \leq 2\times 10^5\)
  • \(1 \leq S_i \leq M\)(\(1 \leq i \leq N\))
  • \(1 \leq U_i \leq 6\)(\(1 \leq i \leq N\))
  • \(1 \leq F_i \leq 6\)(\(1 \leq i \leq N\))
  • \(U_i \neq F_i\) かつ \(U_i+F_i \neq 7\)(\(1 \leq i \leq N\))
  • \(1 \leq d_j \leq N\)(\(1 \leq j \leq Q\))
  • \(c_j\) は + または -(\(1 \leq j \leq Q\))
  • 操作の結果、サイコロの位置が \(1\) 未満または \(M\) を超えることはない
  • \(c_j\) を除き、入力はすべて整数で与えられる

入力

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

\(N\) \(M\) \(Q\)
\(S_1\) \(U_1\) \(F_1\)
\(S_2\) \(U_2\) \(F_2\)
\(\vdots\)
\(S_N\) \(U_N\) \(F_N\)
\(d_1\) \(c_1\)
\(d_2\) \(c_2\)
\(\vdots\)
\(d_Q\) \(c_Q\)
  • \(1\) 行目には、サイコロの個数 \(N\)、マス目の数 \(M\)、操作の回数 \(Q\) がスペース区切りで与えられる。
  • 続く \(N\) 行のうち \(i\) 行目(\(1 \leq i \leq N\))には、サイコロ \(i\) の初期位置 \(S_i\)、初期の上面の数 \(U_i\)、初期の前面の数 \(F_i\) がスペース区切りで与えられる。
  • 続く \(Q\) 行のうち \(j\) 行目(\(1 \leq j \leq Q\))には、転がすサイコロの番号 \(d_j\) と転がす方向 \(c_j\)(+ または -)がスペース区切りで与えられる。

出力

\(N\) 行出力せよ。

\(i\) 行目(\(1 \leq i \leq N\))には、サイコロ \(i\) の最終的な位置と上面の数をスペース区切りで出力せよ。


入力例 1

1 5 3
2 1 2
1 +
1 +
1 -

出力例 1

3 5

入力例 2

3 6 7
1 1 2
6 6 5
3 3 1
1 +
2 -
3 -
3 +
1 +
2 +
1 -

出力例 2

2 5
6 6
3 3

入力例 3

5 20 15
1 1 3
20 2 4
10 3 2
5 4 1
15 5 3
1 +
2 -
3 +
4 -
5 +
1 +
3 +
2 -
5 -
4 +
1 -
3 -
2 +
5 +
4 -

出力例 3

2 4
19 4
11 5
4 1
16 4

入力例 4

10 1000000000 36
1 1 2
1000000000 6 4
500000000 2 3
999999999 3 1
123456789 4 5
987654321 5 1
2 1 3
999999998 2 4
400000000 3 6
600000000 6 2
1 +
2 -
7 -
8 +
4 +
3 +
5 -
6 +
9 +
10 -
1 -
2 +
7 +
8 +
4 -
3 +
5 +
6 -
9 -
10 +
1 +
2 -
7 -
8 -
4 +
3 -
5 -
6 +
9 +
10 -
1 +
2 -
7 +
8 +
4 -
3 +

出力例 4

3 6
999999998 1
500000002 5
999999999 3
123456788 5
987654322 6
2 1
1000000000 5
400000001 1
599999999 2

入力例 5

1 2 15
1 1 2
1 +
1 -
1 +
1 -
1 +
1 -
1 +
1 -
1 +
1 -
1 +
1 -
1 +
1 -
1 +

出力例 5

2 5

Score : 266 pts

Problem Statement

Takahashi is conducting an experiment by rolling \(N\) dice on a line of squares.

The squares are arranged in a row, numbered \(1\) through \(M\). The square numbers are assigned sequentially from one end as \(1,2,\dots,M\).

Each die \(i\) (\(1 \leq i \leq N\)) is initially placed on square \(S_i\). Multiple dice may be placed on the same square, but the dice do not affect each other and are treated independently.

Each die is a standard six-sided die, with numbers \(1\) through \(6\) written on its six faces, one per face. The sum of the numbers on opposite faces is always \(7\).

In this problem, the orientation of a die is represented by a pair of the top face number and the front face number. The front face is the face facing in the direction of increasing square numbers.

In the initial state of die \(i\), the top face number is \(U_i\) and the front face number is \(F_i\). Since the top and front faces are neither the same face nor opposite faces, it is guaranteed that \(U_i \neq F_i\) and \(U_i+F_i \neq 7\).

Takahashi performs \(Q\) operations in order. In the \(j\)-th operation (\(1 \leq j \leq Q\)), he rolls die \(d_j\) by \(1\) square in direction \(c_j\) (+ or -).

When rolling a die, let \(u\) be the top face number and \(f\) be the front face number immediately before the roll. The position and orientation change according to the following rules:

  • Rolling in the + direction (direction of increasing square numbers):
  • The position increases by \(1\).
  • The new top face number becomes \(7-f\), and the new front face number becomes \(u\).
  • This corresponds to a rotation where the face that was the front becomes the bottom, the face that was the back becomes the top, and the face that was the top becomes the front.
  • Rolling in the - direction (direction of decreasing square numbers):
  • The position decreases by \(1\).
  • The new top face number becomes \(f\), and the new front face number becomes \(7-u\).
  • This corresponds to the reverse rotation of the + direction: the face that was the back becomes the bottom, the face that was the front becomes the top, and the face that was the top becomes the back.

It is guaranteed that throughout all operations, the position of each die remains within the range of \(1\) to \(M\), inclusive.

After completing all \(Q\) operations, find the final position and top face number for each die. You do not need to output the front face number.

Constraints

  • \(1 \leq N \leq 2\times 10^5\)
  • \(1 \leq M \leq 10^9\)
  • \(1 \leq Q \leq 2\times 10^5\)
  • \(N+Q \leq 2\times 10^5\)
  • \(1 \leq S_i \leq M\) (\(1 \leq i \leq N\))
  • \(1 \leq U_i \leq 6\) (\(1 \leq i \leq N\))
  • \(1 \leq F_i \leq 6\) (\(1 \leq i \leq N\))
  • \(U_i \neq F_i\) and \(U_i+F_i \neq 7\) (\(1 \leq i \leq N\))
  • \(1 \leq d_j \leq N\) (\(1 \leq j \leq Q\))
  • \(c_j\) is + or - (\(1 \leq j \leq Q\))
  • As a result of operations, the position of any die will never be less than \(1\) or exceed \(M\).
  • All input values except \(c_j\) are integers.

Input

Input is given from Standard Input in the following format:

\(N\) \(M\) \(Q\)
\(S_1\) \(U_1\) \(F_1\)
\(S_2\) \(U_2\) \(F_2\)
\(\vdots\)
\(S_N\) \(U_N\) \(F_N\)
\(d_1\) \(c_1\)
\(d_2\) \(c_2\)
\(\vdots\)
\(d_Q\) \(c_Q\)
  • The 1st line contains the number of dice \(N\), the number of squares \(M\), and the number of operations \(Q\), separated by spaces.
  • The following \(N\) lines contain the initial configuration of die \(i\) (\(1 \leq i \leq N\)): the initial position \(S_i\), the initial top face number \(U_i\), and the initial front face number \(F_i\), separated by spaces.
  • The following \(Q\) lines contain the operations: the \(j\)-th line (\(1 \leq j \leq Q\)) contains the die index \(d_j\) to roll and the direction \(c_j\) (+ or -), separated by spaces.

Output

Print \(N\) lines.

In the \(i\)-th line (\(1 \leq i \leq N\)), print the final position and top face number of die \(i\), separated by a space.


Sample Input 1

1 5 3
2 1 2
1 +
1 +
1 -

Sample Output 1

3 5

Sample Input 2

3 6 7
1 1 2
6 6 5
3 3 1
1 +
2 -
3 -
3 +
1 +
2 +
1 -

Sample Output 2

2 5
6 6
3 3

Sample Input 3

5 20 15
1 1 3
20 2 4
10 3 2
5 4 1
15 5 3
1 +
2 -
3 +
4 -
5 +
1 +
3 +
2 -
5 -
4 +
1 -
3 -
2 +
5 +
4 -

Sample Output 3

2 4
19 4
11 5
4 1
16 4

Sample Input 4

10 1000000000 36
1 1 2
1000000000 6 4
500000000 2 3
999999999 3 1
123456789 4 5
987654321 5 1
2 1 3
999999998 2 4
400000000 3 6
600000000 6 2
1 +
2 -
7 -
8 +
4 +
3 +
5 -
6 +
9 +
10 -
1 -
2 +
7 +
8 +
4 -
3 +
5 +
6 -
9 -
10 +
1 +
2 -
7 -
8 -
4 +
3 -
5 -
6 +
9 +
10 -
1 +
2 -
7 +
8 +
4 -
3 +

Sample Output 4

3 6
999999998 1
500000002 5
999999999 3
123456788 5
987654322 6
2 1
1000000000 5
400000001 1
599999999 2

Sample Input 5

1 2 15
1 1 2
1 +
1 -
1 +
1 -
1 +
1 -
1 +
1 -
1 +
1 -
1 +
1 -
1 +
1 -
1 +

Sample Output 5

2 5
B - 十字照明

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

配点 : 333 点

問題文

高橋君は、H 行 W 列のグリッド状に区画されたイベント会場の照明担当です。各区画には明るさの値が設定されており、上から i 行目、左から j 列目の区画の明るさを G_{i,j} とします。なお、明るさの値は負になることもあります。

高橋君は会場内からちょうど1つの区画を選び、そこにスポットライトを設置します。上から r 行目、左から c 列目の区画 (r, c) にスポットライトを設置すると、区画 (r, c) 自身を含む r 行目のすべての区画、および区画 (r, c) 自身を含む c 列目のすべての区画が十字型に照らされます。

このとき、照らされた全区画の明るさの合計を、その設置場所の照明スコアと呼びます。ただし、区画 (r, c) は r 行目と c 列目の両方に属しますが、照明スコアの計算では明るさを1回だけ数えます。

すなわち、区画 (r, c) にスポットライトを設置したときの照明スコアは以下の式で求められます。

\sum_{j=1}^{W} G_{r,j} + \sum_{i=1}^{H} G_{i,c} - G_{r,c}

高橋君は照明スコアが最大となる区画にスポットライトを設置したいと考えています。

照明スコアの最大値を求めてください。

制約

  • 1 \leq H \leq 2000
  • 1 \leq W \leq 2000
  • -10^9 \leq G_{i,j} \leq 10^9
  • 入力はすべて整数である

入力

H W
G_{1,1} G_{1,2} \ldots G_{1,W}
G_{2,1} G_{2,2} \ldots G_{2,W}
\vdots
G_{H,1} G_{H,2} \ldots G_{H,W}
  • 1 行目には、グリッドの行数を表す H と列数を表す W が、スペース区切りで与えられる。
  • 2 行目から H + 1 行目では、グリッドの各行の値がスペース区切りで与えられる。
  • 1 + i 行目には、i 行目の区画の明るさ G_{i,1}, G_{i,2}, \ldots, G_{i,W} がスペース区切りで与えられる。

出力

照明スコアの最大値を1行で出力せよ。


入力例 1

3 3
1 2 3
4 5 6
7 8 9

出力例 1

33

入力例 2

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

出力例 2

-10

入力例 3

4 5
3 -1 4 1 5
9 2 -6 5 3
-5 8 7 -2 4
6 0 -3 9 1

出力例 3

30

入力例 4

6 7
10 -3 5 8 -2 7 1
-4 6 9 -1 3 0 12
7 2 -8 4 11 -5 6
1 -6 3 15 -7 8 2
0 9 -4 6 3 -1 5
-3 4 7 -2 8 10 -6

出力例 4

56

入力例 5

1 1
-1000000000

出力例 5

-1000000000

Score : 333 pts

Problem Statement

Takahashi is in charge of lighting at an event venue partitioned into a grid with H rows and W columns. Each cell has a brightness value, and the brightness of the cell at the i-th row from the top and j-th column from the left is G_{i,j}. Note that the brightness values can be negative.

Takahashi will choose exactly one cell in the venue to install a spotlight. If he installs a spotlight at cell (r, c), located at the r-th row from the top and c-th column from the left, all cells in the r-th row (including cell (r, c) itself) and all cells in the c-th column (including cell (r, c) itself) will be illuminated in a cross shape.

The total brightness of all illuminated cells is called the lighting score of that installation location. Although cell (r, c) belongs to both the r-th row and the c-th column, its brightness is counted only once in the calculation of the lighting score.

That is, the lighting score when a spotlight is installed at cell (r, c) is given by the following formula:

\sum_{j=1}^{W} G_{r,j} + \sum_{i=1}^{H} G_{i,c} - G_{r,c}

Takahashi wants to install the spotlight at a cell that maximizes the lighting score.

Find the maximum possible lighting score.

Constraints

  • 1 \leq H \leq 2000
  • 1 \leq W \leq 2000
  • -10^9 \leq G_{i,j} \leq 10^9
  • All input values are integers.

Input

H W
G_{1,1} G_{1,2} \ldots G_{1,W}
G_{2,1} G_{2,2} \ldots G_{2,W}
\vdots
G_{H,1} G_{H,2} \ldots G_{H,W}
  • The first line contains H, the number of rows of the grid, and W, the number of columns, separated by a space.
  • The second through (H + 1)-th lines give the values of each row of the grid, separated by spaces.
  • The (1 + i)-th line contains the brightness values of the cells in the i-th row, G_{i,1}, G_{i,2}, \ldots, G_{i,W}, separated by spaces.

Output

Print the maximum lighting score in a single line.


Sample Input 1

3 3
1 2 3
4 5 6
7 8 9

Sample Output 1

33

Sample Input 2

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

Sample Output 2

-10

Sample Input 3

4 5
3 -1 4 1 5
9 2 -6 5 3
-5 8 7 -2 4
6 0 -3 9 1

Sample Output 3

30

Sample Input 4

6 7
10 -3 5 8 -2 7 1
-4 6 9 -1 3 0 12
7 2 -8 4 11 -5 6
1 -6 3 15 -7 8 2
0 9 -4 6 3 -1 5
-3 4 7 -2 8 10 -6

Sample Output 4

56

Sample Input 5

1 1
-1000000000

Sample Output 5

-1000000000
C - 花壇の水やり

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

配点 : 366 点

問題文

高橋君は学校の園芸委員会の委員長です。学校には N 個の花壇があり、それぞれの花壇には 1 から N までの番号が付けられています。

各花壇 i (1 \leq i \leq N) には初期水分量 L_i が設定されています。花壇の水分量が K 以上であれば花は元気に育ちますが、K 未満になると花は枯れてしまいます。高橋君は花を元気に保つために、これから M 回の水やりを行います。

j 回目 (1 \leq j \leq M) の水やりでは、花壇 X_j から花壇 Y_j まで(X_j \leq Y_j)の連続した番号の花壇すべてに対して、水分量を Z_j だけ増加させます。水やり以外の要因で水分量が変化することはありません。

すべての水やりが終わった後、各花壇の最終的な水分量は、初期水分量にすべての水やりによる増加分を加えた値になります。最終的な水分量が K 以上である花壇の個数、すなわち高橋君が元気に保てた花壇の個数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq 10^9
  • 0 \leq L_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq X_j \leq Y_j \leq N (1 \leq j \leq M)
  • 1 \leq Z_j \leq 10^4 (1 \leq j \leq M)
  • 入力はすべて整数である

入力

N M K
L_1 L_2 \ldots L_N
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_M Y_M Z_M
  • 1 行目には、花壇の個数 N、水やりの回数 M、花が元気に育つために必要な水分量の基準値 K が、スペース区切りで与えられる。
  • 2 行目には、各花壇の初期水分量 L_1, L_2, \ldots, L_N が、スペース区切りで与えられる。
  • 続く M 行には、各水やりの情報が与えられる。M = 0 の場合、この部分は存在しない。
  • 2 + j 行目 (1 \leq j \leq M) には、j 回目の水やりにおける対象範囲の開始花壇番号 X_j、終了花壇番号 Y_j、水分量の増加量 Z_j が、スペース区切りで与えられる。

出力

すべての水やりが終わった後、最終的な水分量が K 以上である花壇の個数を 1 行で出力せよ。


入力例 1

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

出力例 1

3

入力例 2

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

出力例 2

6

入力例 3

10 6 100
50 80 30 90 45 70 25 60 85 40
1 5 20
3 8 30
2 6 15
6 10 25
1 3 10
8 10 20

出力例 3

7

Score : 366 pts

Problem Statement

Takahashi is the head of the school's gardening committee. The school has N flower beds, each numbered from 1 to N.

Each flower bed i (1 \leq i \leq N) has an initial moisture level L_i. If a flower bed's moisture level is at least K, the flowers grow healthily, but if it falls below K, the flowers wilt. To keep the flowers healthy, Takahashi will perform M watering operations.

In the j-th watering operation (1 \leq j \leq M), he increases the moisture level by Z_j for all consecutively numbered flower beds from flower bed X_j to flower bed Y_j (X_j \leq Y_j). The moisture levels do not change due to any factors other than watering.

After all watering operations are completed, the final moisture level of each flower bed is the initial moisture level plus the total increase from all watering operations. Find the number of flower beds whose final moisture level is at least K, that is, the number of flower beds that Takahashi was able to keep healthy.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 0 \leq K \leq 10^9
  • 0 \leq L_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq X_j \leq Y_j \leq N (1 \leq j \leq M)
  • 1 \leq Z_j \leq 10^4 (1 \leq j \leq M)
  • All input values are integers

Input

N M K
L_1 L_2 \ldots L_N
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_M Y_M Z_M
  • The first line contains the number of flower beds N, the number of watering operations M, and the threshold moisture level K required for flowers to grow healthily, separated by spaces.
  • The second line contains the initial moisture levels L_1, L_2, \ldots, L_N of each flower bed, separated by spaces.
  • The following M lines contain information about each watering operation. If M = 0, this part does not exist.
  • The (2 + j)-th line (1 \leq j \leq M) contains the starting flower bed number X_j, the ending flower bed number Y_j, and the moisture increase amount Z_j for the j-th watering operation, separated by spaces.

Output

Print in one line the number of flower beds whose final moisture level is at least K after all watering operations are completed.


Sample Input 1

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

Sample Output 1

3

Sample Input 2

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

Sample Output 2

6

Sample Input 3

10 6 100
50 80 30 90 45 70 25 60 85 40
1 5 20
3 8 30
2 6 15
6 10 25
1 3 10
8 10 20

Sample Output 3

7
D - 回転暗号の解読

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

配点 : 400 点

問題文

高橋君は、N 通の暗号文を受信しました。各暗号文は長さ M の英小文字からなる文字列ですが、通信中のノイズにより一部の位置が読み取れなくなっており、さらに読み取れた位置にも誤りが含まれている可能性があります。

一方、これらの暗号文の元になったはずの長さ M の原文が N 個与えられています。暗号化の際、すべての原文は共通のシフト量で巡回シフトされて暗号文になったと考えられていますが、正確なシフト量も、どの暗号文がどの原文に対応するかも分かっていません。

高橋君は、不一致数の合計が最小になるようなシフト量と対応を見つけたいと考えています。

用語の定義

文字列の各位置は 1 から M までの整数で表します。

巡回シフトと位置の対応: シフト量 r(0 \leq r < M の整数)で暗号文と原文を比較するとは、原文を左に r 文字だけ巡回シフトした文字列と暗号文を、同じ位置同士で比較することです。具体的には、暗号文の位置 p に対応する原文の位置は ((p + r - 1) \bmod M) + 1 です。

ノイズ情報: 各暗号文 i(1 \leq i \leq N)には、受信した文字列 S_i と、読み取り可否を表す文字列 C_i が対になっています。C_i の位置 p が 0 ならばその位置の文字は読み取れており、比較の対象とします。1 ならばその位置は読み取れなかったことを意味し、比較の対象外とします。

不一致数: 暗号文 i と原文 j をシフト量 r で対応させたときの不一致数を次のように定義します。C_i の位置 p が 0 であるすべての位置 p(1 \leq p \leq M)について、S_i の位置 p の文字と T_j の位置 ((p + r - 1) \bmod M) + 1 の文字を比較し、異なるものの個数を不一致数とします。

求めるもの

シフト量 r(0 \leq r < M)を1つ選び、N 通の暗号文と N 個の原文の間の1対1対応(すなわち、暗号文 1, 2, \ldots, N に原文を重複なく1つずつ割り当てる方法)を1つ定めます。シフト量 r はすべての対応ペアで共通です。

このとき、すべての対応ペアにおける不一致数の合計を考え、その最小値を求めてください。

変更クエリ

暗号文や原文に対して Q 回の変更が順に行われます。各変更を行った後の状態について、上記の最小値を求めてください。

制約

  • 1 \leq N \leq 5
  • 1 \leq M \leq 500
  • 1 \leq Q \leq 500
  • M Q \leq 5 \times 10^4
  • S_i は長さ M の英小文字からなる文字列である(1 \leq i \leq N)
  • C_i は長さ M の 0 と 1 からなる文字列である(1 \leq i \leq N)
  • T_j は長さ M の英小文字からなる文字列である(1 \leq j \leq N)
  • 各変更クエリは以下の3種類のいずれかである
  • 形式 1 i p x:1 \leq i \leq N、1 \leq p \leq M、x は英小文字
  • 形式 2 i p b:1 \leq i \leq N、1 \leq p \leq M、b は 0 または 1
  • 形式 3 j p x:1 \leq j \leq N、1 \leq p \leq M、x は英小文字

入力

N M Q
S_1
C_1
S_2
C_2
\vdots
S_N
C_N
T_1
T_2
\vdots
T_N
query_1
query_2
\vdots
query_Q

入力は以下の構成で与えられます。

  • 1 行目には、暗号文と原文の個数 N、各文字列の長さ M、変更の回数 Q が空白区切りで与えられる。
  • 続く 2N 行で、N 通の暗号文の初期状態が与えられる。暗号文 i(1 \leq i \leq N)について、受信文字列 S_i とノイズ情報 C_i がこの順に1行ずつ与えられる。
  • 続く N 行で、N 個の原文の初期状態が与えられる。j 番目(1 \leq j \leq N)の行に原文 T_j が与えられる。
  • 続く Q 行で、変更クエリが1行ずつ与えられる。各変更は次のいずれかの形式である。
  • 1 i p x:暗号文 i の受信文字列 S_i の位置 p の文字を x に変更する。
  • 2 i p b:暗号文 i のノイズ情報 C_i の位置 p の値を b に変更する。
  • 3 j p x:原文 T_j の位置 p の文字を x に変更する。

出力

Q 行出力せよ。k 行目(1 \leq k \leq Q)には、k 回目の変更を行った後の状態における、不一致数の合計の最小値を整数で出力せよ。


入力例 1

2 4 5
abca
0000
zzzz
1010
bcaa
zzzz
1 1 2 c
2 2 1 0
3 1 4 b
2 1 3 1
1 2 4 a

出力例 1

1
1
2
2
3

入力例 2

1 5 4
abcde
00101
cdeab
3 1 1 a
2 1 3 0
1 1 5 b
3 1 4 z

出力例 2

0
1
1
2

入力例 3

3 8 10
abcdefgh
00001111
hgfedcba
01010101
aaaabbbb
11110000
cdefghab
fedcbahg
bbbbaaaa
1 1 4 z
2 3 2 0
3 2 8 a
1 2 1 f
2 1 5 0
3 1 3 x
2 2 4 0
1 3 8 c
3 3 5 z
2 1 1 1

出力例 3

3
3
3
4
4
5
5
5
5
5

入力例 4

5 20 15
abcdefghijklmnopqrst
00000000000000000000
zyxwvutsrqponmlkjihg
01010101010101010101
aaaaaaaaaabbbbbbbbbb
11111000001111100000
qwertyuiopasdfghjklz
00110011001100110011
mnopqrstuvwxyzabcdef
10000000000000000001
defghijklmnopqrstabc
ghijklmnopqrstuvwxyz
bbbbbbbbbbaaaaaaaaaa
poiuytrewqlkjhgfdsaz
nopqrstuvwxyzabcdefg
1 1 20 a
2 2 2 0
3 3 10 c
1 4 5 z
2 5 1 0
3 1 1 q
1 3 11 a
2 4 19 1
3 5 20 y
1 2 7 b
2 1 10 1
3 4 4 a
1 5 14 m
2 3 6 1
3 2 15 d

出力例 4

41
42
42
42
42
42
42
42
42
42
41
41
42
42
43

入力例 5

1 1 5
a
0
b
3 1 1 a
1 1 1 z
2 1 1 1
3 1 1 z
2 1 1 0

出力例 5

0
1
0
0
0

Score : 400 pts

Problem Statement

Takahashi has received N encrypted messages. Each encrypted message is a string of length M consisting of lowercase English letters, but due to noise during transmission, some positions have become unreadable, and even the readable positions may contain errors.

Meanwhile, N original plaintext strings of length M are given, which are supposed to be the sources of these encrypted messages. It is believed that during encryption, all plaintexts were cyclically shifted by a common shift amount to produce the encrypted messages, but neither the exact shift amount nor the correspondence between encrypted messages and plaintexts is known.

Takahashi wants to find the shift amount and correspondence that minimize the total number of mismatches.

Definitions

Each position in a string is represented by an integer from 1 to M.

Cyclic shift and position correspondence: Comparing an encrypted message with a plaintext using shift amount r (an integer with 0 \leq r < M) means comparing the encrypted message with the string obtained by cyclically shifting the plaintext r characters to the left, at corresponding positions. Specifically, the plaintext position corresponding to position p in the encrypted message is ((p + r - 1) \bmod M) + 1.

Noise information: Each encrypted message i (1 \leq i \leq N) is paired with a received string S_i and a readability string C_i. If position p of C_i is 0, then the character at that position is readable and is subject to comparison. If it is 1, it means that position was unreadable and is excluded from comparison.

Number of mismatches: The number of mismatches when pairing encrypted message i with plaintext j using shift amount r is defined as follows. For all positions p (1 \leq p \leq M) where position p of C_i is 0, compare the character at position p of S_i with the character at position ((p + r - 1) \bmod M) + 1 of T_j. The number of positions where they differ is the number of mismatches.

Objective

Choose one shift amount r (0 \leq r < M), and determine a one-to-one correspondence between the N encrypted messages and the N plaintexts (i.e., assign one plaintext to each of encrypted messages 1, 2, \ldots, N without repetition). The shift amount r is common to all correspondence pairs.

Consider the total number of mismatches across all correspondence pairs, and find its minimum value.

Update Queries

Q updates are applied sequentially to the encrypted messages or plaintexts. After each update, find the above minimum value for the current state.

Constraints

  • 1 \leq N \leq 5
  • 1 \leq M \leq 500
  • 1 \leq Q \leq 500
  • M Q \leq 5 \times 10^4
  • S_i is a string of length M consisting of lowercase English letters (1 \leq i \leq N)
  • C_i is a string of length M consisting of 0 and 1 (1 \leq i \leq N)
  • T_j is a string of length M consisting of lowercase English letters (1 \leq j \leq N)
  • Each update query is one of the following three types:
  • Format 1 i p x: 1 \leq i \leq N, 1 \leq p \leq M, x is a lowercase English letter
  • Format 2 i p b: 1 \leq i \leq N, 1 \leq p \leq M, b is 0 or 1
  • Format 3 j p x: 1 \leq j \leq N, 1 \leq p \leq M, x is a lowercase English letter

Input

N M Q
S_1
C_1
S_2
C_2
\vdots
S_N
C_N
T_1
T_2
\vdots
T_N
query_1
query_2
\vdots
query_Q

The input is given in the following format:

  • The first line contains the number of encrypted messages and plaintexts N, the length of each string M, and the number of updates Q, separated by spaces.
  • The following 2N lines give the initial state of the N encrypted messages. For encrypted message i (1 \leq i \leq N), the received string S_i and noise information C_i are given in this order, one per line.
  • The following N lines give the initial state of the N plaintexts. The j-th line (1 \leq j \leq N) contains plaintext T_j.
  • The following Q lines give the update queries, one per line. Each update is in one of the following formats:
  • 1 i p x: Change the character at position p of the received string S_i of encrypted message i to x.
  • 2 i p b: Change the value at position p of the noise information C_i of encrypted message i to b.
  • 3 j p x: Change the character at position p of plaintext T_j to x.

Output

Output Q lines. The k-th line (1 \leq k \leq Q) should contain the minimum value of the total number of mismatches after the k-th update has been applied, as an integer.


Sample Input 1

2 4 5
abca
0000
zzzz
1010
bcaa
zzzz
1 1 2 c
2 2 1 0
3 1 4 b
2 1 3 1
1 2 4 a

Sample Output 1

1
1
2
2
3

Sample Input 2

1 5 4
abcde
00101
cdeab
3 1 1 a
2 1 3 0
1 1 5 b
3 1 4 z

Sample Output 2

0
1
1
2

Sample Input 3

3 8 10
abcdefgh
00001111
hgfedcba
01010101
aaaabbbb
11110000
cdefghab
fedcbahg
bbbbaaaa
1 1 4 z
2 3 2 0
3 2 8 a
1 2 1 f
2 1 5 0
3 1 3 x
2 2 4 0
1 3 8 c
3 3 5 z
2 1 1 1

Sample Output 3

3
3
3
4
4
5
5
5
5
5

Sample Input 4

5 20 15
abcdefghijklmnopqrst
00000000000000000000
zyxwvutsrqponmlkjihg
01010101010101010101
aaaaaaaaaabbbbbbbbbb
11111000001111100000
qwertyuiopasdfghjklz
00110011001100110011
mnopqrstuvwxyzabcdef
10000000000000000001
defghijklmnopqrstabc
ghijklmnopqrstuvwxyz
bbbbbbbbbbaaaaaaaaaa
poiuytrewqlkjhgfdsaz
nopqrstuvwxyzabcdefg
1 1 20 a
2 2 2 0
3 3 10 c
1 4 5 z
2 5 1 0
3 1 1 q
1 3 11 a
2 4 19 1
3 5 20 y
1 2 7 b
2 1 10 1
3 4 4 a
1 5 14 m
2 3 6 1
3 2 15 d

Sample Output 4

41
42
42
42
42
42
42
42
42
42
41
41
42
42
43

Sample Input 5

1 1 5
a
0
b
3 1 1 a
1 1 1 z
2 1 1 1
3 1 1 z
2 1 1 0

Sample Output 5

0
1
0
0
0
E - フリーマーケットの割り当て

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

配点 : 466 点

問題文

高橋君はフリーマーケットに出店し、M 個の商品を販売しようとしています。商品にはそれぞれ 1 から M の番号が付いており、商品 j の仕入れ値は C_j 円です。売れ残った商品は仕入れ元に返品できるため、売れ残りによる損失は発生しません。

フリーマーケットには N 人の来場者が訪れます。来場者にはそれぞれ 1 から N の番号が付いています。来場者 i は、商品 j に対して最大 V_{i,j} 円まで支払ってもよいと考えています(これを来場者 i の商品 j に対する評価額と呼びます)。

高橋君は各来場者の評価額を事前に把握しており、各商品を評価額と同額の価格で売ります。すなわち、商品 j を来場者 i に売る場合、売値は V_{i,j} 円となり、その取引から得られる利益は V_{i,j} - C_j 円です。

高橋君は、以下の条件をすべて満たすように商品の売り先を決めます。

  • 各商品は 1 個しかないため、1 つの商品を 2 人以上の来場者に売ることはできない。すなわち、各商品について、売る相手を 1 人だけ選ぶか、誰にも売らないかのいずれかである。
  • 各来場者は最大 1 つの商品しか購入しない。すなわち、各来場者について、購入する商品を 1 つだけ選ぶか、何も購入しないかのいずれかである。
  • 利益が負になる取引は行わない。すなわち、商品 j を来場者 i に売るためには V_{i,j} \geq C_j でなければならない。

これらの条件のもとで、高橋君が得られる利益の合計の最大値を求めてください。なお、どの商品も売らないという選択も可能であり、その場合の利益の合計は 0 です。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 14
  • N \times M \leq 2 \times 10^5
  • 1 \leq C_j \leq 10^9 (1 \leq j \leq M)
  • 0 \leq V_{i,j} \leq 10^9 (1 \leq i \leq N, 1 \leq j \leq M)
  • 入力はすべて整数である。

入力

N M
C_1 C_2 \ldots C_M
V_{1,1} V_{1,2} \ldots V_{1,M}
V_{2,1} V_{2,2} \ldots V_{2,M}
\vdots
V_{N,1} V_{N,2} \ldots V_{N,M}
  • 1 行目には、来場者の人数を表す整数 N と、商品の個数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目には、各商品の仕入れ値を表す整数 C_1, C_2, \ldots, C_M が、スペース区切りで与えられる。
  • 続く N 行にわたり、各来場者の各商品に対する評価額が与えられる。
  • 第 i+2 行 (1 \leq i \leq N) には、来場者 i の商品 1, 2, \ldots, M に対する評価額を表す整数 V_{i,1}, V_{i,2}, \ldots, V_{i,M} が、スペース区切りで与えられる。

出力

高橋君が得られる利益の合計の最大値を 1 行で出力せよ。


入力例 1

3 2
5 4
8 3
6 10
4 7

出力例 1

9

入力例 2

2 3
100 200 300
50 150 250
99 199 299

出力例 2

0

入力例 3

8 5
10 20 15 8 30
12 18 20 7 35
9 25 14 10 29
30 10 18 8 31
11 22 16 20 10
5 19 40 9 32
13 21 15 8 30
50 5 5 5 5
10 20 15 100 60

出力例 3

167

入力例 4

20 12
40 25 60 10 75 55 30 90 15 50 65 20
100 20 70 9 80 54 33 95 15 55 70 22
39 40 61 12 74 100 31 20 18 49 66 25
45 30 59 30 76 56 80 91 10 52 64 19
10 25 100 11 75 54 29 120 16 50 63 21
41 26 60 10 200 55 30 90 15 50 65 20
80 24 58 9 74 53 32 89 14 120 64 18
42 100 62 13 73 57 28 88 17 51 90 30
40 25 60 10 75 55 30 90 15 50 65 20
0 0 0 0 0 0 0 0 0 0 0 0
60 45 80 25 90 70 50 110 35 75 85 40
38 27 65 8 78 52 35 93 20 48 67 22
90 10 10 10 10 10 10 10 10 10 10 10
20 60 20 60 20 60 20 60 20 60 20 60
55 35 75 15 85 65 45 105 25 70 80 30
43 28 63 14 77 58 34 92 19 53 68 24
70 70 70 70 70 70 70 70 70 70 70 70
41 24 59 11 76 56 29 91 16 51 66 23
120 130 140 150 160 170 180 190 200 210 220 230
5 200 5 200 5 200 5 200 5 200 5 200
44 29 64 13 79 59 36 94 21 54 69 26

出力例 4

955

入力例 5

1 1
1000000000
1000000000

出力例 5

0

Score : 466 pts

Problem Statement

Takahashi is opening a stall at a flea market and plans to sell M items. The items are numbered 1 through M, and the cost price of item j is C_j yen. Unsold items can be returned to the supplier, so no loss is incurred from unsold items.

There are N visitors to the flea market, numbered 1 through N. Visitor i is willing to pay up to V_{i,j} yen for item j (this is called visitor i's valuation of item j).

Takahashi knows each visitor's valuation in advance and sells each item at a price equal to the valuation. That is, if item j is sold to visitor i, the selling price is V_{i,j} yen, and the profit obtained from this transaction is V_{i,j} - C_j yen.

Takahashi decides the buyers of the items so that all of the following conditions are met:

  • Since there is only 1 of each item, a single item cannot be sold to 2 or more visitors. That is, for each item, he either chooses at most one buyer or sells it to no one.
  • Each visitor purchases at most 1 item. That is, for each visitor, they either choose at most one item to purchase or purchase nothing.
  • No transaction with a negative profit is made. That is, to sell item j to visitor i, it must hold that V_{i,j} \geq C_j.

Under these conditions, find the maximum possible total profit Takahashi can obtain. Note that it is possible to choose not to sell any items, in which case the total profit is 0.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq M \leq 14
  • N \times M \leq 2 \times 10^5
  • 1 \leq C_j \leq 10^9 (1 \leq j \leq M)
  • 0 \leq V_{i,j} \leq 10^9 (1 \leq i \leq N, 1 \leq j \leq M)
  • All input values are integers.

Input

N M
C_1 C_2 \ldots C_M
V_{1,1} V_{1,2} \ldots V_{1,M}
V_{2,1} V_{2,2} \ldots V_{2,M}
\vdots
V_{N,1} V_{N,2} \ldots V_{N,M}
  • The first line contains the integer N, representing the number of visitors, and the integer M, representing the number of items, separated by a space.
  • The second line contains the integers C_1, C_2, \ldots, C_M, representing the cost prices of the items, separated by spaces.
  • The following N lines provide each visitor's valuation for each item.
  • The (i+2)-th line (1 \leq i \leq N) contains the integers V_{i,1}, V_{i,2}, \ldots, V_{i,M}, representing visitor i's valuations for items 1, 2, \ldots, M, separated by spaces.

Output

Print the maximum possible total profit Takahashi can obtain in a single line.


Sample Input 1

3 2
5 4
8 3
6 10
4 7

Sample Output 1

9

Sample Input 2

2 3
100 200 300
50 150 250
99 199 299

Sample Output 2

0

Sample Input 3

8 5
10 20 15 8 30
12 18 20 7 35
9 25 14 10 29
30 10 18 8 31
11 22 16 20 10
5 19 40 9 32
13 21 15 8 30
50 5 5 5 5
10 20 15 100 60

Sample Output 3

167

Sample Input 4

20 12
40 25 60 10 75 55 30 90 15 50 65 20
100 20 70 9 80 54 33 95 15 55 70 22
39 40 61 12 74 100 31 20 18 49 66 25
45 30 59 30 76 56 80 91 10 52 64 19
10 25 100 11 75 54 29 120 16 50 63 21
41 26 60 10 200 55 30 90 15 50 65 20
80 24 58 9 74 53 32 89 14 120 64 18
42 100 62 13 73 57 28 88 17 51 90 30
40 25 60 10 75 55 30 90 15 50 65 20
0 0 0 0 0 0 0 0 0 0 0 0
60 45 80 25 90 70 50 110 35 75 85 40
38 27 65 8 78 52 35 93 20 48 67 22
90 10 10 10 10 10 10 10 10 10 10 10
20 60 20 60 20 60 20 60 20 60 20 60
55 35 75 15 85 65 45 105 25 70 80 30
43 28 63 14 77 58 34 92 19 53 68 24
70 70 70 70 70 70 70 70 70 70 70 70
41 24 59 11 76 56 29 91 16 51 66 23
120 130 140 150 160 170 180 190 200 210 220 230
5 200 5 200 5 200 5 200 5 200 5 200
44 29 64 13 79 59 36 94 21 54 69 26

Sample Output 4

955

Sample Input 5

1 1
1000000000
1000000000

Sample Output 5

0