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

#FT0000168. Thay hàng, thay cột và tổng ký tự (Bored of Suffixes and Prefixes)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Thay hàng, thay cột và tổng ký tự (Bored of Suffixes and Prefixes)

Bored of Suffixes and Prefixes

Source: SPOJ

Version: Phước Hưng OJ Extended.

Problem Statement

You are given an N×NN\times N matrix of uppercase English letters, with A worth 11, B worth 22, ..., Z worth 2626. Process QQ operations:

  • 0 x y s: if x=0x=0, replace row yy with the string ss; if x=1x=1, replace column yy with the characters of ss from top to bottom. The string always has length NN.
  • 1 x1 y1 x2 y2: output the sum of letter values in the inclusive rectangle from (x1,y1)(x1,y1) to (x2,y2)(x2,y2).

Rows and columns are 0-indexed. Replacement operations immediately affect all following queries.

Input

The first line contains N,QN,Q, followed by NN uppercase strings of length NN. The next QQ lines contain either 0 x y s or 1 x1 y1 x2 y2.

Output

Print the sum of letter values for each type-1 operation, one answer per line.

Subtask

  • Subtask 1 (20 points): 1≤N≤81\le N\le 8; 1≤Q≤501\le Q\le 50; every matrix row and replacement string has exactly NN letters A–Z; x∈{0,1}x\in\{0,1\}, 0≤y<N0\le y<N in replacement operations; 0≤x1≤x2<N0\le x1\le x2<N, 0≤y1≤y2<N0\le y1\le y2<N in queries.
  • Subtask 2 (30 points): 1≤N≤801\le N\le 80; 1≤Q≤15001\le Q\le 1500; every matrix row and replacement string has exactly NN letters A–Z; x∈{0,1}x\in\{0,1\}, 0≤y<N0\le y<N in replacement operations; 0≤x1≤x2<N0\le x1\le x2<N, 0≤y1≤y2<N0\le y1\le y2<N in queries.
  • Subtask 3 (50 points): 1≤N≤5001\le N\le 500; 1≤Q≤100001\le Q\le 10000; every matrix row and replacement string has exactly NN letters A–Z; x∈{0,1}x\in\{0,1\}, 0≤y<N0\le y<N in replacement operations; 0≤x1≤x2<N0\le x1\le x2<N, 0≤y1≤y2<N0\le y1\le y2<N in queries.

Examples

Example 1

Input

4 3
ABCD
ABCD
ABCD
ABCD
1 0 0 3 3
0 0 2 PQRS
1 0 2 3 3

Output

40
58

Explanation

Initially each row totals 1+2+3+4=101+2+3+4=10, so the full matrix totals 4040. Replacing row 2 by PQRS changes its last two letters to R and S, values 18 and 19. The other three rows contribute 3(3+4)=213(3+4)=21 over columns 2–3; the new row contributes 3737, yielding 5858.

Example 2

Input

3 6
AAA
BBB
CCC
1 0 0 2 2
0 1 0 XYZ
1 0 0 2 0
0 0 1 AZA
1 1 0 1 2
1 0 1 2 2

Output

18
75
28
35

Explanation

The original matrix totals 3(1+2+3)=183(1+2+3)=18. Replacing column 0 from top to bottom with XYZ makes that column sum 24+25+26=7524+25+26=75. Replacing row 1 with AZA gives 1+26+1=281+26+1=28. The last rectangle spans columns 1–2 in all three rows and totals 1+1+26+1+3+3=351+1+26+1+3+3=35.