#abc260g. G - Scalene Triangle Area

G - Scalene Triangle Area

Score : 600600 points

问题描述

我们有一个 N×NN \times N 的网格。在这个网格中,从上到下第 ii 行、从左到右第 jj 列的方格被称为 (i,j)(i,j)

每个网格方格最多有一个棋子。

网格的状态由 NN 个字符串 SiS_i 给出:

  • 如果 SiS_i 的第 jj 个字符是 O,表示 (i,j)(i,j) 上有一个棋子;
  • 如果 SiS_i 的第 jj 个字符是 X,表示 (i,j)(i,j) 上没有棋子。

给定一个整数 MM,我们定义放置在 (s,t)(s,t) 的棋子 PP 覆盖了方格 (u,v)(u,v),当且仅当以下所有条件都满足:

  • suNs \le u \le N
  • tvNt \le v \le N
  • (us)+(vt)2<M(u - s) + \frac{(v - t)}{2} < M

对于 QQ 个不同的方格 (Xi,Yi)(X_i,Y_i),找出覆盖这些方格的棋子数量。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have an N×NN \times N grid. The square at the ii-th row from the top and jj-th column from the left in this grid is called (i,j)(i,j).
Each square of the grid has at most one piece.
The state of the grid is given by NN strings SiS_i:

  • if the jj-th character of SiS_i is O, then (i,j)(i,j) has a piece on it;
  • if the jj-th character of SiS_i is X, then (i,j)(i,j) has no piece on it.

You are given an integer MM. Using this MM, we define that a piece PP placed at (s,t)(s,t) covers a square (u,v)(u,v) if all of the following conditions are satisfied:

  • suNs \le u \le N
  • tvNt \le v \le N
  • (us)+(vt)2<M(u - s) + \frac{(v - t)}{2} < M

For each of QQ squares (Xi,Yi)(X_i,Y_i), find how many pieces cover the square.

Constraints

  • NN, MM, XiX_i, YiY_i, and QQ are integers.
  • 1N20001 \le N \le 2000
  • 1M2×N1 \le M \le 2 \times N
  • SiS_i consists of O and X.
  • 1Q2×1051 \le Q \le 2 \times 10^5
  • 1Xi,YiN1 \le X_i,Y_i \le N

Input

Input is given from Standard Input in the following format:

NN MM

S1S_1

S2S_2

\vdots

SNS_N

QQ

X1X_1 Y1Y_1

X2X_2 Y2Y_2

\vdots

XQX_Q YQY_Q

Output

Print QQ lines.
The ii-th line ( 1iQ1 \le i \le Q ) should contain the number of pieces that covers (Xi,Yi)(X_i,Y_i) as an integer.

Sample Input 1

4 2
OXXX
XXXX
XXXX
XXXX
6
1 1
1 4
2 2
2 3
3 1
4 4

Sample Output 1

1
1
1
0
0
0

Only Square (1,1)(1,1) contains a piece, which covers the following # squares:

####
##..
....
....

Sample Input 2

5 10
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
5
1 1
2 3
3 4
4 2
5 5

Sample Output 2

1
6
12
8
25

Sample Input 3

8 5
OXXOXXOX
XOXXOXOX
XOOXOOXO
OXOOXOXO
OXXOXXOX
XOXXOXOX
XOOXOOXO
OXOOXOXO
6
7 2
8 1
4 5
8 8
3 4
1 7

Sample Output 3

5
3
9
14
5
3

update @ 2024/3/10 11:02:33