Problem:


Score : 400 points

Problem Statement

We have a grid with H rows and W columns. At first, all cells were painted white.

Snuke painted N of these cells. The i-th ( 1 \leq i \leq N ) cell he painted is the cell at the a_i-th row and b_i-th column.

Compute the following:

For each integer j ( 0 \leq j \leq 9 ), how many subrectangles of size 3×3 of the grid contains exactly j black cells, after Snuke painted N cells?

Constraints

3 \leq H \leq 10^9

3 \leq W \leq 10^9

0 \leq N \leq min(10^5,H×W)

1 \leq a_i \leq H (1 \leq i \leq N)

1 \leq b_i \leq W (1 \leq i \leq N)

(a_i, b_i) \neq (a_j, b_j) (i \neq j)

Input

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

H W N
a_1 b_1
:
a_N b_N

Output

Print 10 lines.
The (j+1)-th ( 0 \leq j \leq 9 ) line should contain the number of the subrectangles of size 3×3 of the grid that contains exactly j black cells.

Sample Input 1

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

Sample Output 1

0
0
0
2
4
0
0
0
0
0

There are six subrectangles of size 3×3. Two of them contain three black cells each, and the remaining four contain four black cells each.

Sample Input 2

10 10 20
1 1
1 4
1 9
2 5
3 10
4 2
4 7
5 9
6 4
6 6
6 7
7 1
7 3
7 7
8 1
8 5
8 10
9 2
10 4
10 9

Sample Output 2

4
26
22
10
2
0
0
0
0
0

Sample Input 3

1000000000 1000000000 0

Sample Output 3

999999996000000004
0
0
0
0
0
0
0
0
0



Sample input:
4 5 8
1 1
1 4
1 5
2 3
3 1
3 2
3 4
4 4


Sample output:
0
0
0
2
4
0
0
0
0
0

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
import time
input = sys.stdin.readline

H,W,N=map(int,input().split())
ab=[tuple(map(int,input().split())) for i in range(N)]
ab.sort()

r_b=[0]

r_b_app=r_b.append
for i in range(1,N):
  if ab[i][0]!=ab[i-1][0]:
    r_b_app(i)

if N>0:
  r_b_app(N)
rows=tuple(ab[b][0] for b in r_b if b<N)

cols=tuple(tuple(ab[j][1] for j in range(r_b[i],r_b[i+1])) for i in range(len(r_b)-1))

rN=len(cols)


ched_box0=[np.zeros((3,3),dtype=bool) for _ in range((len(cols[0]) if 0<rN else 0))]
ched_box1=[np.zeros((3,3),dtype=bool) for _ in range((len(cols[1]) if 1<rN else 0))]
ched_box2=[np.zeros((3,3),dtype=bool) for _ in range((len(cols[2]) if 2<rN else 0))]

dic_cn0=dict(zip(cols[0],list(range(len(cols[0]))))) if 0<rN else {}
dic_cn1=dict(zip(cols[1],list(range(len(cols[1]))))) if 1<rN else {}
dic_cn2=dict(zip(cols[2],list(range(len(cols[2]))))) if 2<rN else {}

area_n=[0]*9

count=[[1]*3 for i in range(3)]
find_cn=[0]*12

for rn in range(rN):
  cN=len(cols[rn])
  row=rows[rn]

  if rn!=0:
    ched_box0=ched_box1
    dic_cn0=dic_cn1
    ched_box1=ched_box2
    dic_cn1=dic_cn2
    if rn<rN-2:
      cN2_range=range(len(cols[rn+2]))
      ched_box2=np.zeros((len(cols[rn+2]),3,3),dtype=bool)
      dic_cn2=dict(zip(cols[rn+2],list(cN2_range)))

  if (rows[rn+1]==row+1 if rn<rN-1 else False):
    row1=row+1
    if (rows[rn+2]==row+2 if rn<rN-2 else False):
      row2=row+2
    else:
      row2=None
  elif (rows[rn+1]==row+2 if rn<rN-1 else False):
    row1=row+2
    row2=None
  else:
    row1=None
    row2=None
  
  for cn in range(cN):
    col=cols[rn][cn]
    ched_box=ched_box0[cn]

    
    for box_row,box_col in zip(*np.where(ched_box==False)):
      if col-2+box_col<1 or col+box_col>W or row-2+box_row<1 or row+box_row>H:
        ched_box[box_row][box_col]=True    

    find_rowcol=np.zeros((5,5),dtype=int)
    find_cn=[[0]*5 for _ in range(3)]

    find_rowcol[2,2]=1
    find_cn[0][2]=cn
    if (col+1 in dic_cn0 if col+1<=W else False):
      find_rowcol[2,3]=1
      find_cn[0][3]=dic_cn0[col+1]
    if (col+2 in dic_cn0 if col+2<=W else False):
      find_rowcol[2,4]=1
      find_cn[0][4]=dic_cn0[col+2]
    if (row1<=H if row1 is not None else False):
      for ch_col in range(max(col-2,1),min(col+3,W+1)):
        if ch_col in dic_cn1:
          find_rowcol[row1-row+2,ch_col-col+2]=1
          find_cn[row1-row][ch_col-col+2]=dic_cn1[ch_col]
    if (row2<=H if row2 is not None else False):
      for ch_col in range(max(col-2,1),min(col+3,W+1)):
        if ch_col in dic_cn2:
          find_rowcol[row2-row+2,ch_col-col+2]=1
          find_cn[row2-row][ch_col-col+2]=dic_cn2[ch_col]

             
   
    for box_row,box_col in zip(*np.where(ched_box==False)):
      area_n[find_rowcol[box_row:box_row+3,box_col:box_col+3].sum()-1]+=1
    for ch_row,ch_col in zip(*np.where(find_rowcol)):
      for box_row in range(5-ch_row):
        for box_col in range((0 if ch_col>=2 else 2-ch_col),(5-ch_col if ch_col>=2 else 3)):
          if ch_row==2:
            ched_box0[find_cn[ch_row-2][ch_col]][box_row][box_col]=True
          elif ch_row==row1-row+2:
            ched_box1[find_cn[ch_row-2][ch_col]][box_row][box_col]=True
          elif ch_row==row2-row+2:
            ched_box2[find_cn[ch_row-2][ch_col]][box_row][box_col]=True




print((W-2)*(H-2)-sum(area_n))
for i in range(9):
  print(area_n[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
input = sys.stdin.readline

H,W,N=map(int,input().split())
ab=[tuple(map(int,input().split())) for i in range(N)]
ab.sort()

r_b=[0]

r_b_app=r_b.append
for i in range(1,N):
  if ab[i][0]!=ab[i-1][0]:
    r_b_app(i)

if N>0:
  r_b_app(N)
rows=tuple(ab[b][0] for b in r_b if b<N)

cols=tuple(tuple(ab[j][1] for j in range(r_b[i],r_b[i+1])) for i in range(len(r_b)-1))

rN=len(cols)


ched_box0=[np.zeros((3,3),dtype=bool) for _ in range((len(cols[0]) if 0<rN else 0))]
ched_box1=[np.zeros((3,3),dtype=bool) for _ in range((len(cols[1]) if 1<rN else 0))]
ched_box2=[np.zeros((3,3),dtype=bool) for _ in range((len(cols[2]) if 2<rN else 0))]

dic_cn0=dict(zip(cols[0],list(range(len(cols[0]))))) if 0<rN else {}
dic_cn1=dict(zip(cols[1],list(range(len(cols[1]))))) if 1<rN else {}
dic_cn2=dict(zip(cols[2],list(range(len(cols[2]))))) if 2<rN else {}

area_n=[0]*9

count=[[1]*3 for i in range(3)]
find_cn=[0]*12

for rn in range(rN):
  cN=len(cols[rn])
  row=rows[rn]

  if rn!=0:
    ched_box0=ched_box1
    dic_cn0=dic_cn1
    ched_box1=ched_box2
    dic_cn1=dic_cn2
    if rn<rN-2:
      cN2_range=range(len(cols[rn+2]))
      ched_box2=[np.zeros((3,3),dtype=bool) for j in cN2_range]
      dic_cn2=dict(zip(cols[rn+2],list(cN2_range)))

  if (rows[rn+1]==row+1 if rn<rN-1 else False):
    row1=row+1
    if (rows[rn+2]==row+2 if rn<rN-2 else False):
      row2=row+2
    else:
      row2=None
  elif (rows[rn+1]==row+2 if rn<rN-1 else False):
    row1=row+2
    row2=None
  else:
    row1=None
    row2=None
  
  for cn in range(cN):
    col=cols[rn][cn]
    ched_box=ched_box0[cn]
    
    box_row_0_cols=[]
    box_row_1_cols=[]
    box_row_2_cols=[]
    
    for box_col in range(3):
      if not ched_box[0][box_col]:
        if col-2+box_col>=1 and col+box_col<=W and row-2>=1 and row<=H:
          box_row_0_cols.append(box_col)
        else:
          ched_box[0][box_col]=True
      if not ched_box[1][box_col]:
        if col-2+box_col>=1 and col+box_col<=W and row-1>=1 and row+1<=H:
          box_row_1_cols.append(box_col)
        else:
          ched_box[1][box_col]=True
      if not ched_box[2][box_col]:
        if col-2+box_col>=1 and col+box_col<=W and row>=1 and row+2<=H:
          box_row_2_cols.append(box_col)
        else:
          ched_box[2][box_col]=True
 
    
      
    find_rowcol=np.zeros((5,5),dtype=int)
    find_cn=[[0]*5 for _ in range(3)]

    find_rowcol[2,2]=1
    find_cn[0][2]=cn
    if (col+1 in dic_cn0 if col+1<=W else False):
      find_rowcol[2,3]=1
      find_cn[0][3]=dic_cn0[col+1]
    if (col+2 in dic_cn0 if col+2<=W else False):
      find_rowcol[2,4]=1
      find_cn[0][4]=dic_cn0[col+2]
    if (row1<=H if row1 is not None else False):
      for ch_col in range(max(col-2,1),min(col+3,W+1)):
        if ch_col in dic_cn1:
          find_rowcol[row1-row+2,ch_col-col+2]=1
          find_cn[row1-row][ch_col-col+2]=dic_cn1[ch_col]
    if (row2<=H if row2 is not None else False):
      for ch_col in range(max(col-2,1),min(col+3,W+1)):
        if ch_col in dic_cn2:
          find_rowcol[row2-row+2,ch_col-col+2]=1
          find_cn[row2-row][ch_col-col+2]=dic_cn2[ch_col]
    for box_row,box_col in zip(*np.where(ched_box==False)):
      area_n[find_rowcol[box_row:box_row+3,box_col:box_col+3].sum()-1]+=1
      
    for ch_row,ch_col in zip(*np.where(find_rowcol)):
      for box_row in range(5-ch_row):
        for box_col in range((0 if ch_col>=2 else 2-ch_col),(5-ch_col if ch_col>=2 else 3)):
          if ch_row==2:
            ched_box0[find_cn[ch_row-2][ch_col]][box_row][box_col]=True
          elif ch_row==row1-row+2:
            ched_box1[find_cn[ch_row-2][ch_col]][box_row][box_col]=True
          elif ch_row==row2-row+2:
            ched_box2[find_cn[ch_row-2][ch_col]][box_row][box_col]=True




print((W-2)*(H-2)-sum(area_n))
for i in range(9):
  print(area_n[i])

Submission status: Time Limit Exceeded

Solution code:
h,w,n=map(int,input().split())
s=set()
l=[[0,0],[1,1],[1,-1],[-1,1],[-1,-1],[0,1],[0,-1],[1,0],[-1,0]]
ans=[(h-2)*(w-2),0,0,0,0,0,0,0,0,0]
for i in range(n):
    a,b=[int(j) for j in input().split()]
    s.add((a,b))
    for i,j in l:
        if a+i>=1 and a+i<=h and b+j>=1 and b+j<=w:
            c=0
            for p,q in l:
                if a+i+p>=1 and a+i+p<=h and b+j+q>=1 and b+j+q<=w:
                    if (a+i+p,b+j+q) in s:
                        c+=1
                else:
                    c=0
                    break
            if c!=0:
                ans[c-1]-=1
                ans[c]+=1

for i in range(10):
    print(ans[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

H,W,N=map(int,input().split())
ab=[tuple(map(int,input().split())) for i in range(N)]
ab.sort()

r_b=[0]

r_b_app=r_b.append
for i in range(1,N):
  if ab[i][0]!=ab[i-1][0]:
    r_b_app(i)

if N>0:
  r_b_app(N)
rows=tuple(ab[b][0] for b in r_b if b<N)

cols=tuple(tuple(ab[j][1] for j in range(r_b[i],r_b[i+1])) for i in range(len(r_b)-1))

rN=len(cols)


ched_box0=[[[False]*3 for k in range(3)] for j in range((len(cols[0]) if 0<rN else 0))]
ched_box1=[[[False]*3 for k in range(3)] for j in range((len(cols[1]) if 1<rN else 0))]
ched_box2=[[[False]*3 for k in range(3)] for j in range((len(cols[2]) if 2<rN else 0))]

dic_cn0=dict(zip(cols[0],list(range(len(cols[0]))))) if 0<rN else {}
dic_cn1=dict(zip(cols[1],list(range(len(cols[1]))))) if 1<rN else {}
dic_cn2=dict(zip(cols[2],list(range(len(cols[2]))))) if 2<rN else {}

area_n=[0]*9

count=[[1]*3 for i in range(3)]
find_cn=[0]*12

for rn in range(rN):
  cN=len(cols[rn])
  row=rows[rn]

  if rn!=0:
    ched_box0=ched_box1
    dic_cn0=dic_cn1
    ched_box1=ched_box2
    dic_cn1=dic_cn2
    if rn<rN-2:
      cN2_range=range(len(cols[rn+2]))
      ched_box2=[[[False]*3 for k in range(3)] for j in cN2_range]
      dic_cn2=dict(zip(cols[rn+2],list(cN2_range)))

  if (rows[rn+1]==row+1 if rn<rN-1 else False):
    row1=row+1
    if (rows[rn+2]==row+2 if rn<rN-2 else False):
      row2=row+2
    else:
      row2=None
  elif (rows[rn+1]==row+2 if rn<rN-1 else False):
    row1=row+2
    row2=None
  else:
    row1=None
    row2=None
  
  for cn in range(cN):
    col=cols[rn][cn]
    ched_box=ched_box0[cn]
    
    box_row_0_cols=[]
    box_row_1_cols=[]
    box_row_2_cols=[]
    
    for box_col in range(3):
      if not ched_box[0][box_col]:
        if col-2+box_col>=1 and col+box_col<=W and row-2>=1 and row<=H:
          box_row_0_cols.append(box_col)
        else:
          ched_box[0][box_col]=True
      if not ched_box[1][box_col]:
        if col-2+box_col>=1 and col+box_col<=W and row-1>=1 and row+1<=H:
          box_row_1_cols.append(box_col)
        else:
          ched_box[1][box_col]=True
      if not ched_box[2][box_col]:
        if col-2+box_col>=1 and col+box_col<=W and row>=1 and row+2<=H:
          box_row_2_cols.append(box_col)
        else:
          ched_box[2][box_col]=True
 
    
      
    find_rowcol=[[0]*5 for _ in range(5)]
    find_cn=[[0]*5 for _ in range(5)]

    find_rowcol[2][2]=1
    find_cn[2][2]=cn
    if (col+1 in dic_cn0 if col+1<=W else False):
      find_rowcol[2][3]=1
      find_cn[2][3]=dic_cn0[col+1]
    if (col+2 in dic_cn0 if col+2<=W else False):
      find_rowcol[2][4]=1
      find_cn[2][4]=dic_cn0[col+2]
    if (row1<=H if row1 is not None else False):
      for ch_col in range(max(col-2,1),min(col+3,W+1)):
        if ch_col in dic_cn1:
          find_rowcol[row1-row+2][ch_col-col+2]=1
          find_cn[row1-row+2][ch_col-col+2]=dic_cn1[ch_col]
    if (row2<=H if row2 is not None else False):
      for ch_col in range(max(col-2,1),min(col+3,W+1)):
        if ch_col in dic_cn2:
          find_rowcol[row2-row+2][ch_col-col+2]=1
          find_cn[row2-row+2][ch_col-col+2]=dic_cn2[ch_col]
    for box_row in range(3):
      for box_col in range(3):
        if not ched_box[box_row][box_col]:
          area_n[sum([sum(find_rowcol[i][box_col:box_col+3]) for i in range(box_row,box_row+3)])-1]+=1
    for ch_row in range(2,5):
      for ch_col in range((0 if ch_row!=2 else 3),5):
        if find_rowcol[ch_row][ch_col]:
          for box_row in range(5-ch_row):
            for box_col in range((0 if ch_col>=2 else 2-ch_col),(5-ch_col if ch_col>=2 else 3)):
              if ch_row==2:
                ched_box0[find_cn[ch_row][ch_col]][box_row][box_col]=True
              elif ch_row==row1-row+2:
                ched_box1[find_cn[ch_row][ch_col]][box_row][box_col]=True
              elif ch_row==row2-row+2:
                ched_box2[find_cn[ch_row][ch_col]][box_row][box_col]=True




print((W-2)*(H-2)-sum(area_n))
for i in range(9):
  print(area_n[i])

Submission status: Time Limit Exceeded

Solution code:
h, w, n = map(int, input().split())
a_list = [[0 for _ in range(w)] for _ in range(h)]
for _ in range(n):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    a_list[a][b] = 1
z_list = []
for center_x in range(1, h - 1):
    for center_y in range(1, w - 1):
        temp = a_list[center_x - 1: center_x + 2]
        temp_num = 0
        for ttt in temp:
            temp_num += sum(ttt[center_y - 1: center_y + 2])
        z_list.append(temp_num)
for i in range(10):
    print(z_list.count(i))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

H,W,N=map(int,input().split())
ab=[tuple(map(int,input().split())) for i in range(N)]
ab.sort()

r_b=[0]

r_b_app=r_b.append
for i in range(1,N):
  if ab[i][0]!=ab[i-1][0]:
    r_b_app(i)

if N>0:
  r_b_app(N)
rows=tuple(ab[b][0] for b in r_b if b<N)

cols=tuple(tuple(ab[j][1] for j in range(r_b[i],r_b[i+1])) for i in range(len(r_b)-1))

rN=len(cols)


ched_box0=[[[False]*3 for k in range(3)] for j in range((len(cols[0]) if 0<rN else 0))]
ched_box1=[[[False]*3 for k in range(3)] for j in range((len(cols[1]) if 1<rN else 0))]
ched_box2=[[[False]*3 for k in range(3)] for j in range((len(cols[2]) if 2<rN else 0))]

dic_cn0=dict(zip(cols[0],list(range(len(cols[0]))))) if 0<rN else {}
dic_cn1=dict(zip(cols[1],list(range(len(cols[1]))))) if 1<rN else {}
dic_cn2=dict(zip(cols[2],list(range(len(cols[2]))))) if 2<rN else {}

area_n=[0]*9

count=[[1]*3 for i in range(3)]
find_cn=[0]*12

for rn in range(rN):
  cN=len(cols[rn])
  row=rows[rn]

  if rn!=0:
    ched_box0=ched_box1
    dic_cn0=dic_cn1
    ched_box1=ched_box2
    dic_cn1=dic_cn2
    if rn<rN-2:
      cN2_range=range(len(cols[rn+2]))
      ched_box2=[[[False]*3 for k in range(3)] for j in cN2_range]
      dic_cn2=dict(zip(cols[rn+2],list(cN2_range)))

  if (rows[rn+1]==row+1 if rn<rN-1 else False):
    row1=row+1
    if (rows[rn+2]==row+2 if rn<rN-2 else False):
      row2=row+2
    else:
      row2=None
  elif (rows[rn+1]==row+2 if rn<rN-1 else False):
    row1=row+2
    row2=None
  else:
    row1=None
    row2=None
  
  for cn in range(cN):
    col=cols[rn][cn]
    ched_box=ched_box0[cn]
    
    box_row_0_cols=[]
    box_row_1_cols=[]
    box_row_2_cols=[]
    
    for box_col in range(3):
      if not ched_box[0][box_col]:
        if col-2+box_col>=1 and col+box_col<=W and row-2>=1 and row<=H:
          box_row_0_cols.append(box_col)
        else:
          ched_box[0][box_col]=True
      if not ched_box[1][box_col]:
        if col-2+box_col>=1 and col+box_col<=W and row-1>=1 and row+1<=H:
          box_row_1_cols.append(box_col)
        else:
          ched_box[1][box_col]=True
      if not ched_box[2][box_col]:
        if col-2+box_col>=1 and col+box_col<=W and row>=1 and row+2<=H:
          box_row_2_cols.append(box_col)
        else:
          ched_box[2][box_col]=True
 
    
      
    find_rowcol=[]

    find_rowcol_app=find_rowcol.append
    find_cn_n=0

    if (col+1 in dic_cn0 if col+1<=W else False):
      find_rowcol_app((row,col+1))
      find_cn[find_cn_n]=dic_cn0[col+1]
      find_cn_n+=1
    if (col+2 in dic_cn0 if col+2<=W else False):
      find_rowcol_app((row,col+2))
      find_cn[find_cn_n]=dic_cn0[col+2]
      find_cn_n+=1
    if (row1<=H if row1 is not None else False):
      for ch_col in range(max(col-2,1),min(col+3,W+1)):
        if ch_col in dic_cn1:
          find_rowcol_app((row1,ch_col))
          find_cn[find_cn_n]=dic_cn1[ch_col]
          find_cn_n+=1
    if (row2<=H if row2 is not None else False):
      for ch_col in range(max(col-2,1),min(col+3,W+1)):
        if ch_col in dic_cn2:
          find_rowcol_app((row2,ch_col))
          find_cn[find_cn_n]=dic_cn2[ch_col]
          find_cn_n+=1

        
    for i,(find_row,find_col) in enumerate(find_rowcol):
      
      if find_row is row:
        ch_rn=0
        ch_cn=find_cn[i]
      elif find_row is row1:
        ch_rn=1
        ch_cn=find_cn[i]
      elif find_row is row2:
        ch_rn=2
        ch_cn=find_cn[i]
      box_row_min=find_row-row
      box_col_min=max(find_col-col,0)
      box_col_max=min(3,3+find_col-col)
      
      if box_row_min==0 and len(box_row_0_cols):
        box_row=0
        for box_col in range(max(box_col_min,min(box_row_0_cols)),min(box_col_max,1+max(box_row_0_cols))):
          count[box_row][box_col]+=1
          if ch_rn is 0:
            ched_box0[ch_cn][0][box_col-find_col+col]=True
          elif ch_rn is 1:
            ched_box1[ch_cn][0][box_col-find_col+col]=True
          else:
            ched_box2[ch_cn][0][box_col-find_col+col]=True

      if box_row_min<=1 and len(box_row_1_cols): 
        for box_col in range(max(box_col_min,min(box_row_1_cols)),min(box_col_max,1+max(box_row_1_cols))):
          count[1][box_col]+=1
          if ch_rn is 0:
            ched_box0[ch_cn][1-box_row_min][box_col-find_col+col]=True
          elif ch_rn is 1:
            ched_box1[ch_cn][1-box_row_min][box_col-find_col+col]=True
          else:
            ched_box2[ch_cn][1-box_row_min][box_col-find_col+col]=True

      if len(box_row_2_cols):
        for box_col in range(max(box_col_min,min(box_row_2_cols)),min(box_col_max,1+max(box_row_2_cols))):
          count[2][box_col]+=1
          if ch_rn is 0:
            ched_box0[ch_cn][2-box_row_min][box_col-find_col+col]=True
          elif ch_rn is 1:
            ched_box1[ch_cn][2-box_row_min][box_col-find_col+col]=True
          else:
            ched_box2[ch_cn][2-box_row_min][box_col-find_col+col]=True



            
    for box_col in box_row_0_cols:
      box_row=0
      area_n[count[box_row][box_col]-1]+=1
      count[box_row][box_col]=1
    for box_col in box_row_1_cols:
      box_row=1
      area_n[count[box_row][box_col]-1]+=1
      count[box_row][box_col]=1
    for box_col in box_row_2_cols:
      box_row=2
      area_n[count[box_row][box_col]-1]+=1
      count[box_row][box_col]=1     



print((W-2)*(H-2)-sum(area_n))
for i in range(9):
  print(area_n[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

H,W,N=map(int,input().split())
ab=[tuple(map(int,input().split())) for i in range(N)]
ab.sort()

r_b=[0]

r_b_app=r_b.append
for i in range(1,N):
  if ab[i][0]!=ab[i-1][0]:
    r_b_app(i)

if N>0:
  r_b_app(N)
rows=tuple(ab[b][0] for b in r_b if b<N)

cols=tuple(tuple(ab[j][1] for j in range(r_b[i],r_b[i+1])) for i in range(len(r_b)-1))

rN=len(cols)


ched_box0=[[[False]*3 for k in range(3)] for j in range((len(cols[0]) if 0<rN else 0))]
ched_box1=[[[False]*3 for k in range(3)] for j in range((len(cols[1]) if 1<rN else 0))]
ched_box2=[[[False]*3 for k in range(3)] for j in range((len(cols[2]) if 2<rN else 0))]

dic_cn0=dict(zip(cols[0],list(range(len(cols[0]))))) if 0<rN else {}
dic_cn1=dict(zip(cols[1],list(range(len(cols[1]))))) if 1<rN else {}
dic_cn2=dict(zip(cols[2],list(range(len(cols[2]))))) if 2<rN else {}

area_n=[0]*9

count=[[1]*3 for i in range(3)]
find_cn=[0]*12

for rn in range(rN):
  cN=len(cols[rn])
  row=rows[rn]

  if rn!=0:
    ched_box0=ched_box1
    dic_cn0=dic_cn1
    ched_box1=ched_box2
    dic_cn1=dic_cn2
    if rn<rN-2:
      cN2_range=range(len(cols[rn+2]))
      ched_box2=[[[False]*3 for k in range(3)] for j in cN2_range]
      dic_cn2=dict(zip(cols[rn+2],list(cN2_range)))

  if (rows[rn+1]==row+1 if rn<rN-1 else False):
    row1=row+1
    if (rows[rn+2]==row+2 if rn<rN-2 else False):
      row2=row+2
    else:
      row2=None
  elif (rows[rn+1]==row+2 if rn<rN-1 else False):
    row1=row+2
    row2=None
  else:
    row1=None
    row2=None
  
  for cn in range(cN):
    col=cols[rn][cn]
    ched_box=ched_box0[cn]
    
    box_row_0_cols=[]
    box_row_1_cols=[]
    box_row_2_cols=[]
    
    for box_col in range(3):
      if not ched_box[0][box_col]:
        if col-2+box_col>=1 and col+box_col<=W and row-2>=1 and row<=H:
          box_row_0_cols.append(box_col)
        else:
          ched_box[0][box_col]=True
      if not ched_box[1][box_col]:
        if col-2+box_col>=1 and col+box_col<=W and row-1>=1 and row+1<=H:
          box_row_1_cols.append(box_col)
        else:
          ched_box[1][box_col]=True
      if not ched_box[2][box_col]:
        if col-2+box_col>=1 and col+box_col<=W and row>=1 and row+2<=H:
          box_row_2_cols.append(box_col)
        else:
          ched_box[2][box_col]=True
 
    
      
    find_rowcol=[]

    find_rowcol_app=find_rowcol.append
    find_cn_n=0

    if (col+1 in dic_cn0 if col+1<=W else False):
      find_rowcol_app((row,col+1))
      find_cn[find_cn_n]=dic_cn0[col+1]
      find_cn_n+=1
    if (col+2 in dic_cn0 if col+2<=W else False):
      find_rowcol_app((row,col+2))
      find_cn[find_cn_n]=dic_cn0[col+2]
      find_cn_n+=1
    if (row1<=H if row1 is not None else False):
      for ch_col in range(max(col-2,1),min(col+3,W+1)):
        if ch_col in dic_cn1:
          find_rowcol_app((row1,ch_col))
          find_cn[find_cn_n]=dic_cn1[ch_col]
          find_cn_n+=1
    if (row2<=H if row2 is not None else False):
      for ch_col in range(max(col-2,1),min(col+3,W+1)):
        if ch_col in dic_cn2:
          find_rowcol_app((row2,ch_col))
          find_cn[find_cn_n]=dic_cn2[ch_col]
          find_cn_n+=1

        
    for i,(find_row,find_col) in enumerate(find_rowcol):
      
      if find_row is row:
        ch_rn=0
        ch_cn=find_cn[i]
      elif find_row is row1:
        ch_rn=1
        ch_cn=find_cn[i]
      elif find_row is row2:
        ch_rn=2
        ch_cn=find_cn[i]
      box_row_min=find_row-row
      box_col_min=max(find_col-col,0)
      box_col_max=min(3,3+find_col-col)
      
      if box_row_min==0 and len(box_row_0_cols):
        #box_row=0
        for box_col in range(max(box_col_min,min(box_row_0_cols)),min(box_col_max,1+max(box_row_0_cols))):
          count[0][box_col]+=1
          if ch_rn is 0:
            ched_box0[ch_cn][0-box_row_min][box_col-find_col+col]=True
          elif ch_rn is 1:
            ched_box1[ch_cn][0-box_row_min][box_col-find_col+col]=True
          else:
            ched_box2[ch_cn][0-box_row_min][box_col-find_col+col]=True

      if box_row_min<=1 and len(box_row_1_cols): 
        for box_col in range(max(box_col_min,min(box_row_1_cols)),min(box_col_max,1+max(box_row_1_cols))):
          count[1][box_col]+=1
          if ch_rn is 0:
            ched_box0[ch_cn][1-box_row_min][box_col-find_col+col]=True
          elif ch_rn is 1:
            ched_box1[ch_cn][1-box_row_min][box_col-find_col+col]=True
          else:
            ched_box2[ch_cn][1-box_row_min][box_col-find_col+col]=True

      if len(box_row_2_cols):
        for box_col in range(max(box_col_min,min(box_row_2_cols)),min(box_col_max,1+max(box_row_2_cols))):
          count[2][box_col]+=1
          if ch_rn is 0:
            ched_box0[ch_cn][2-box_row_min][box_col-find_col+col]=True
          elif ch_rn is 1:
            ched_box1[ch_cn][2-box_row_min][box_col-find_col+col]=True
          else:
            ched_box2[ch_cn][2-box_row_min][box_col-find_col+col]=True



            
    for box_col in box_row_0_cols:
      box_row=0
      area_n[count[box_row][box_col]-1]+=1
      count[box_row][box_col]=1
    for box_col in box_row_1_cols:
      box_row=1
      area_n[count[box_row][box_col]-1]+=1
      count[box_row][box_col]=1
    for box_col in box_row_2_cols:
      box_row=2
      area_n[count[box_row][box_col]-1]+=1
      count[box_row][box_col]=1     



print((W-2)*(H-2)-sum(area_n))
for i in range(9):
  print(area_n[i])

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

H,W,N=map(int,input().split())
ab=[tuple(map(int,input().split())) for i in range(N)]
ab.sort()

r_b=[0]

r_b_app=r_b.append
for i in range(1,N):
  if ab[i][0]!=ab[i-1][0]:
    r_b_app(i)

if N>0:
  r_b_app(N)
rows=tuple(ab[b][0] for b in r_b if b<N)

cols=tuple(tuple(ab[j][1] for j in range(r_b[i],r_b[i+1])) for i in range(len(r_b)-1))

rN=len(cols)


ched_box0=[[[False]*3 for k in range(3)] for j in range((len(cols[0]) if 0<rN else 0))]
ched_box1=[[[False]*3 for k in range(3)] for j in range((len(cols[1]) if 1<rN else 0))]
ched_box2=[[[False]*3 for k in range(3)] for j in range((len(cols[2]) if 2<rN else 0))]

dic_cn0=dict(zip(cols[0],list(range(len(cols[0]))))) if 0<rN else {}
dic_cn1=dict(zip(cols[1],list(range(len(cols[1]))))) if 1<rN else {}
dic_cn2=dict(zip(cols[2],list(range(len(cols[2]))))) if 2<rN else {}

area_n=[0]*9

count=[[1]*3 for i in range(3)]
find_cn=[0]*12

for rn in range(rN):
  cN=len(cols[rn])
  row=rows[rn]

  if rn!=0:
    ched_box0=ched_box1
    dic_cn0=dic_cn1
    ched_box1=ched_box2
    dic_cn1=dic_cn2
    if rn<rN-2:
      cN2_range=range(len(cols[rn+2]))
      ched_box2=[[[False]*3 for k in range(3)] for j in cN2_range]
      dic_cn2=dict(zip(cols[rn+2],list(cN2_range)))

  if (rows[rn+1]==row+1 if rn<rN-1 else False):
    row1=row+1
    if (rows[rn+2]==row+2 if rn<rN-2 else False):
      row2=row+2
    else:
      row2=None
  elif (rows[rn+1]==row+2 if rn<rN-1 else False):
    row1=row+2
    row2=None
  else:
    row1=None
    row2=None
  
  for cn in range(cN):
    col=cols[rn][cn]
    ched_box=ched_box0[cn]
    
    box_row_0_cols=[]
    box_row_1_cols=[]
    box_row_2_cols=[]
    
    for box_col in range(3):
      if not ched_box[0][box_col]:
        if col-2+box_col>=1 and col+box_col<=W and row-2>=1 and row<=H:
          box_row_0_cols.append(box_col)
        else:
          ched_box[0][box_col]=True
      if not ched_box[1][box_col]:
        if col-2+box_col>=1 and col+box_col<=W and row-1>=1 and row+1<=H:
          box_row_1_cols.append(box_col)
        else:
          ched_box[1][box_col]=True
      if not ched_box[2][box_col]:
        if col-2+box_col>=1 and col+box_col<=W and row>=1 and row+2<=H:
          box_row_2_cols.append(box_col)
        else:
          ched_box[2][box_col]=True
 
    
      
    find_rowcol=[]

    find_rowcol_app=find_rowcol.append
    find_cn_n=0

    if (col+1 in dic_cn0 if col+1<=W else False):
      find_rowcol_app((row,col+1))
      find_cn[find_cn_n]=dic_cn0[col+1]
      find_cn_n+=1
    if (col+2 in dic_cn0 if col+2<=W else False):
      find_rowcol_app((row,col+2))
      find_cn[find_cn_n]=dic_cn0[col+2]
      find_cn_n+=1
    if (row1<=H if row1 is not None else False):
      for ch_col in range(max(col-2,1),min(col+3,W+1)):
        if ch_col in dic_cn1:
          find_rowcol_app((row1,ch_col))
          find_cn[find_cn_n]=dic_cn1[ch_col]
          find_cn_n+=1
    if (row2<=H if row2 is not None else False):
      for ch_col in range(max(col-2,1),min(col+3,W+1)):
        if ch_col in dic_cn2:
          find_rowcol_app((row2,ch_col))
          find_cn[find_cn_n]=dic_cn2[ch_col]
          find_cn_n+=1

        
    for i,(find_row,find_col) in enumerate(find_rowcol):
      
      if find_row is row:
        ch_rn=0
        ch_cn=find_cn[i]
      elif find_row is row1:
        ch_rn=1
        ch_cn=find_cn[i]
      elif find_row is row2:
        ch_rn=2
        ch_cn=find_cn[i]
      box_row_min=find_row-row
      box_col_min=max(find_col-col,0)
      box_col_max=min(3,3+find_col-col)
      
      if box_row_min==0 and len(box_row_0_cols):
        box_row=0
        for box_col in range(max(box_col_min,min(box_row_0_cols)),min(box_col_max,1+max(box_row_0_cols))):
          count[box_row][box_col]+=1
          if ch_rn is 0:
            ched_box0[ch_cn][box_row-box_row_min][box_col-find_col+col]=True
          elif ch_rn is 1:
            ched_box1[ch_cn][box_row-box_row_min][box_col-find_col+col]=True
          else:
            ched_box2[ch_cn][box_row-box_row_min][box_col-find_col+col]=True

      if box_row_min<=1 and len(box_row_1_cols): 
        for box_col in range(max(box_col_min,min(box_row_1_cols)),min(box_col_max,1+max(box_row_1_cols))):
          count[1][box_col]+=1
          if ch_rn is 0:
            ched_box0[ch_cn][1-box_row_min][box_col-find_col+col]=True
          elif ch_rn is 1:
            ched_box1[ch_cn][1-box_row_min][box_col-find_col+col]=True
          else:
            ched_box2[ch_cn][1-box_row_min][box_col-find_col+col]=True

      if len(box_row_2_cols):
        for box_col in range(max(box_col_min,min(box_row_2_cols)),min(box_col_max,1+max(box_row_2_cols))):
          count[2][box_col]+=1
          if ch_rn is 0:
            ched_box0[ch_cn][2-box_row_min][box_col-find_col+col]=True
          elif ch_rn is 1:
            ched_box1[ch_cn][2-box_row_min][box_col-find_col+col]=True
          else:
            ched_box2[ch_cn][2-box_row_min][box_col-find_col+col]=True



            
    for box_col in box_row_0_cols:
      box_row=0
      area_n[count[box_row][box_col]-1]+=1
      count[box_row][box_col]=1
    for box_col in box_row_1_cols:
      box_row=1
      area_n[count[box_row][box_col]-1]+=1
      count[box_row][box_col]=1
    for box_col in box_row_2_cols:
      box_row=2
      area_n[count[box_row][box_col]-1]+=1
      count[box_row][box_col]=1     



print((W-2)*(H-2)-sum(area_n))
for i in range(9):
  print(area_n[i])