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

#FT0000165. Đếm số ô có màu chỉ định (Counting Problem)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đếm số ô có màu chỉ định (Counting Problem)

Counting Problem

Source: Luogu

Version: Phuoc Hung OJ – standard input format, original problem semantics preserved.

Problem Statement

An N×MN\times M grid initially assigns a color Ax,y∈[1,100]A_{x,y}\in[1,100] to each cell. Row and column indices are one-based. Process QQ operations:

  • 1 x y c: change the color of cell (x,y)(x,y) to cc, replacing its former color.
  • 2 x1 x2 y1 y2 c: count cells with color exactly cc among rows x1x1 through x2x2 and columns y1y1 through y2y2, inclusive.

For type 2, the two row bounds precede the two column bounds.

Input

The first line gives N,MN,M. The next NN lines contain MM initial colors each. The next line gives QQ, followed by QQ operations of the form 1 x y c or 2 x1 x2 y1 y2 c.

Output

For every type-2 query, print the number of cells of color cc in the rectangle, one line per query.

Subtasks

  • Subtask 1 (30 points): 1≤N≤301\le N\le 30; 1≤M≤301\le M\le 30; 1≤Q≤500001\le Q\le 50000; all colors Ax,y,cA_{x,y},c lie in [1,100][1,100]; type-1 coordinates 1≤x≤N1\le x\le N, 1≤y≤M1\le y\le M; type-2 bounds 1≤x1≤x2≤N1\le x1\le x2\le N, 1≤y1≤y2≤M1\le y1\le y2\le M.
  • Subtask 2 (70 points): 1≤N≤3001\le N\le 300; 1≤M≤3001\le M\le 300; 1≤Q≤2000001\le Q\le 200000; all colors Ax,y,cA_{x,y},c lie in [1,100][1,100]; type-1 coordinates 1≤x≤N1\le x\le N, 1≤y≤M1\le y\le M; type-2 bounds 1≤x1≤x2≤N1\le x1\le x2\le N, 1≤y1≤y2≤M1\le y1\le y2\le M.

Examples

Example 1

Input

2 3
1 2 1
2 3 1
6
2 1 2 1 3 1
1 1 2 1
2 1 1 1 3 1
2 1 2 2 3 2
1 2 1 1
2 1 2 1 3 1

Output

3
3
0
5

Explanation

Initially color 1 appears at (1,1), (1,3), (2,3), count 3. Recoloring (1,2) makes all three cells in the first row color 1. No color-2 cells remain in columns 2–3, giving 0. Recoloring (2,1) adds another color-1 cell, resulting in five overall.

Example 2

Input

1 1
100
4
2 1 1 1 1 100
1 1 1 3
2 1 1 1 1 100
2 1 1 1 1 3

Output

1
0
1

Explanation

The single cell initially has color 100, so the first query returns 1. Recoloring it to 3 removes the only occurrence of color 100 (answer 0) and creates one color-3 cell (answer 1).