Good Bye C. New year and Domino two-dimensional prefix

Source: Internet
Author: User

C. New Year and Domino

They say "years is like dominoes, tumbling one after the other". But would a year fit into a grid? I don ' t think so.

Limak is a little polar bear, who loves to play. He has recently got a rectangular grid with h rows and w columns. Each cell was a square, either empty (denoted by ') or forbidden (denoted by '# '). Rows is numbered 1 through H from top to bottom. Columns is numbered1 through w from left to right.

Also, Limak have a single domino. He wants to put it somewhere in a grid. A Domino would occupy exactly, adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must is inside a grid.

Limak needs more fun and thus he's going to consider some queries. In each query he chooses some rectangle and wonders, how many the is there to put a single domino inside of the Chosen re Ctangle?

Input

The first line of the input contains, integers h and w (1≤ H, w ≤500) , haven number of rows and the number of columns, respectively.

The next h lines describe a grid. Each line contains a string of the length w. Each character are either '. ' or '# '-denoting an empty or forbidden cell, respectively.

The next line contains a single integer q (1≤ q ≤100)-the number of queries.

Each of the nextQLines contains four integersR1I,C1I,R2I,C2 i   (1≤ R 1 I R 2 i h , 1≤ c 1 I c 2 i w )  -the  I -th Query. Numbers  R 1 i  and C 1 i  denote the row and the column ( respectively) of the upper left cell of the rectangle. Numbers  R 2 i  and  C 2 i  denote the row and the column ( respectively) of the bottom right cell of the rectangle.

Output

Print q integers, i-th should is equal to the number of ways to put a single domino inside the c5>i-th rectangle.

Sample Test (s) input
5 8
....#.. #
.#......
##.#....
##.. #.##
........
4
1 1 2 3
4 1 4 1
1 2 4 5
2 5 5 8
Output
4
0
10
15
input
7 39
.......................................
.###.. ###.. #.. ###.....###.. ###.. #.. ###.
...#.. #.#.. #.. #.........#.. #.#.. #.. #...
.###.. #.#.. #.. ###.....###.. #.#.. #.. ###.
.#....#.#.. #....#.....#....#.#.. #.. #.#.
.###.. ###.. #.. ###.....###.. ###.. #.. ###.
.......................................
6
1 1 3 20
2 10 6 30
2 10 7 30
2 2 7 7
1 7 7 7
1 8 7 8
Output
53
89
120
23
0
2
Note

A Red frame below corresponds to the first query of the first sample. A Domino can is placed in 4 possible ways.

Test instructions: Give you a picture of n*m, there. , #, #号不可走

Q a Query

Each query for a rectangle asks how many paths in this rectangle are the length 2

Puzzle: We can find out if each point can go down to the right value is 2,1,0,

Then from 1, 1 to x, y this rectangle can be obtained with a two-dimensional prefix array

The answer to each question is simple, with a focus on the boundary and only two directions.

//Meek#include <bits/stdc++.h>#include<iostream>#include<cstdio>#include<cmath>#include<string>#include<cstring>#include<algorithm>#include<map>#include<queue>using namespacestd; typedefLong Longll;#defineMem (a) memset (A,0,sizeof (a))#definePB Push_back#defineFi first#defineSe Second#defineMP Make_pairConst intn=550+ -;Constll INF = 1ll<< A;Const intINF =1000000007;Const intMod=1000007;CharMp[n][n];intP[n][n],v[n][n];intn,m;intsss[4][2] ={1,0,0,1};structss{intx, y;};intCheckintXinty) {if(x<=0|| y<=0|| x>n| | Y&GT;M)return 1; return 0;}intV[n][n];voidBFsintXinty) { for(intI=1; i<=n;i++) {         for(intj=1; j<=m;j++) {                if(mp[i][j]!='#')              for(intk=0;k<2; k++) {                 intxx = i+sss[k][0]; intyy = j+sss[k][1]; if(check (XX,YY))Continue; if(mp[xx][yy]=='#')Continue; P[I][J]++; }        }    }}intMain () {scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++) {GetChar ();  for(intj=1; j<=m;j++) {scanf ("%c",&Mp[i][j]); }} BFS (1,1);  for(intI=1; i<=n;i++) {         for(intj=1; j<=m;j++) {V[i][j]= v[i][j]+v[i-1][j]+v[i][j-1]-v[i-1][j-1]+P[i][j]; }     }    intQ,x,y,x2,y2; scanf ("%d",&q);  for(intI=1; i<=q;i++) {scanf ("%d%d%d%d",&x,&y,&x2,&y2); intAns = v[x2][y2]-v[x2][y-1]-v[x-1][y2]+v[x-1][y-1];  for(intj=x;j<x2;j++) {            if(mp[j][y2]=='.'&&mp[j][y2+1]=='.') ans--; } ans-=P[x2][y2];  for(intj=y;j<y2;j++) {            if(mp[x2][j]=='.'&&mp[x2+1][j]=='.') ans--; } printf ("%d\n", ans); }}
Code

Good Bye C. New year and Domino two-dimensional prefix

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.