Horse Habitat

시간 제한25초메모리 제한2048 MB

요약
최대 900만 칸 격자와 10만 개 질의가 주어질 때, 각 h×w 크기의 점만으로 이루어진 부분 직사각형 위치 수를 구한다.
난이도

어려움10점 중 8점

유형
동적 계획법, 누적 합, 분할 정복, 구현
정답자
아직 제출이 없습니다

문제

Harold has inherited a huge habitat with hundreds of horses! He wants to train a handful of horses for the Bareback Arizona Phoenix Cowboys, which is a half-yearly happening honouring Arizonan horse riding history. Hence, Harold signed his horses up for the Hurdle Hopping event and he has requested your help handling the training program.

Hurdle Hopping courses have many possible layouts, each requiring a different rectangular area. However, not all of the land in the habitat is suitable for courses. Horses, moreover, need to train courses on multiple different grounds in order to learn to adapt to possible circumstances. Handling the training program, it is thus relevant that courses can be rebuilt in many different locations.

Handed to you is a map showing the habitat as a grid of unit squares with each square indicating whether the land is suitable for courses or not. Help Harold by answering a list of questions, each question asking the total number of possible locations in the habitat for a Hurdle Hopping course with a particular size.

입력

The input consists of:

  • One line with three integers rr, cc, and qq (1≤r,c≤9⋅1061 \leq r,c \leq 9 \cdot 10^6, r⋅c≤9⋅106r \cdot c \leq 9 \cdot 10^6, 1≤q≤1051 \leq q \leq 10^5), the number of rows and columns of the grid, and the number of questions.
  • rr lines with cc characters, each character being either '.' if the corresponding square indicates land suitable for courses or '#' otherwise.
  • qq lines, each with two integers hh and ww (1≤h≤r1 \leq h \leq r, 1≤w≤c1 \leq w \leq c), indicating a question from Harold about the number of Hurdle Hopping courses with height hh (number of rows in the grid) and width ww (number of columns in the grid).

출력

For each of the qq questions, output the number of possible locations for a grid-aligned Hurdle Hopping course of the requested height hh (number of rows in the grid) and width ww (number of columns in the grid).

예제4

  1. 예제 1

    입력
    1 7 1
    #....#.
    1 2
    
    예상 출력
    3
    
  2. 예제 2

    입력
    3 3 6
    ..#
    #..
    ...
    1 1
    1 2
    2 1
    3 1
    2 2
    3 3
    
    예상 출력
    7
    4
    3
    1
    1
    0
    
  3. 예제 3

    입력
    2 3 6
    ...
    ...
    1 1
    1 2
    2 1
    2 2
    1 3
    2 3
    
    예상 출력
    6
    4
    3
    2
    2
    1
    
  4. 예제 4

    입력
    3 5 5
    .....
    ..#..
    .....
    2 2
    1 1
    1 5
    3 1
    1 3
    
    예상 출력
    4
    14
    2
    4
    6