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

#PS0000228. Đếm ô theo từng màu (Rectangle Color Counts)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đếm ô theo từng màu (Rectangle Color Counts)

Rectangle Color Counts

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given a static m×nm\times n grid of colors A, B, C, indexed from 1, count the cells of each color in every inclusive rectangle (r1,c1,r2,c2)(r_1,c_1,r_2,c_2).

Input

The first line contains m,n,Qm,n,Q. The next mm lines each contain a string of exactly nn characters A, B, C without spaces. Each of the next QQ lines contains r1,c1,r2,c2r_1,c_1,r_2,c_2.

Output

Print QQ lines, each containing three integers: the counts of A, B, C in that order.

Subtasks

  • Subtask 1 (20 points): 1≤m,n≤121\le m,n\le 12; 1≤Q≤301\le Q\le 30; 1≤r1≤r2≤m1\le r_1\le r_2\le m; 1≤c1≤c2≤n1\le c_1\le c_2\le n; characters are A, B, C.
  • Subtask 2 (30 points): 1≤m,n≤5001\le m,n\le 500; 1≤Q≤1000001\le Q\le 100000; 1≤r1≤r2≤m1\le r_1\le r_2\le m; 1≤c1≤c2≤n1\le c_1\le c_2\le n; characters are A, B, C; all cells are A.
  • Subtask 3 (50 points): 1≤m,n≤5001\le m,n\le 500; 1≤Q≤1000001\le Q\le 100000; 1≤r1≤r2≤m1\le r_1\le r_2\le m; 1≤c1≤c2≤n1\le c_1\le c_2\le n; characters are A, B, C.

Example 1

Input

2 3 3
ABC
AAB
1 1 2 3
1 2 2 2
2 3 2 3

Output

3 2 1
1 1 0
0 1 0

Explanation

The full grid has 3 A, 2 B, 1 C. Column 2 contains B,A; the last cell is B.

Example 2

Input

1 1 1
C
1 1 1 1

Output

0 0 1

Explanation

There is one C cell.