C. New Year and Domino
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

They say "years are 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 is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through h from top to bottom. Columns are numbered 1 through w from left to right.

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

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

Input

The first line of the input contains two integers h and w (1 ≤ h, w ≤ 500) – the 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 is either '.' or '#' — denoting an empty or forbidden cell, respectively.

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

Each of the next q lines contains four integers r1i, c1i, r2i, c2i (1 ≤ r1i ≤ r2i ≤ h, 1 ≤ c1i ≤ c2i ≤ w) — the i-th query. Numbers r1i and c1i denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers r2i and c2i denote the row and the column (respectively) of the bottom right cell of the rectangle.

Output

Print q integers, i-th should be equal to the number of ways to put a single domino inside the 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 be placed in 4 possible ways.

题意:输入h,w表示行和列的数目,接下来h行描述一个网格,每一行有一个长度为w的字符串,每个字符要么是'.',要么是'#',分别表示空的和禁止的。

  输入一个q,接下来得q行有4个数,表示一个小网格的左上角的横纵坐标和右下角的横纵坐标。

  输出每个小方格内有几个2个连在一起的空格。

思路:网格存在gg[MAXN][MAXN]中,用两个二位数组row[MAXN][MAXN]和column[MAXN][MAXN];row[i][j]表示第i行从开始到j这个位置有几个2个连在一起的空格,column[i][j]表示第j列从开始到i这个位置有几个2个连在一起的空格。

row举例来说,如果gg[i][j]这个位置是'.'并且它前面gg[i][j-1]这个位置也是'.'的话row[i][j]=row[i][j-1]+1;否则的话,row[i][j]=row[i][j-1]。

要注意越界的问题。

if(gg[i][j]=='.'&&j>1&&gg[i][j-1]=='.') row[i][j]=row[i][j-1]+1;
else row[i][j]=row[i][j-1];

if(gg[i][j]=='.'&&i>1&&gg[i-1][j]=='.') column[i][j]=column[i-1][j]+1;
else column[i][j]=column[i-1][j];

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char gg[][];
int row[][],column[][];
int x1[],y1[],x2[],y2[];
int main()
{
int h,w,q;
scanf("%d %d",&h,&w);
getchar();
memset(row,,sizeof(row));
memset(column,,sizeof(column));
int i,j;
for(i=; i<=h; i++)
{
for(j=; j<=w; j++)
{
scanf("%c",&gg[i][j]);
if(gg[i][j]=='.'&&j>&&gg[i][j-]=='.') row[i][j]=row[i][j-]+;
else row[i][j]=row[i][j-];
if(gg[i][j]=='.'&&i>&&gg[i-][j]=='.') column[i][j]=column[i-][j]+;
else column[i][j]=column[i-][j];
}
getchar();
}
/*
for(i=1; i<=h; i++)
{
for(j=1; j<=w; j++)
cout<<row[i][j]<<" ";
cout<<endl;
}
cout<<endl<<endl;
for(i=1; i<=h; i++)
{
for(j=1; j<=w; j++)
cout<<column[i][j]<<" ";
cout<<endl;
}
*/
scanf("%d",&q);
for(i=; i<q; i++)
scanf("%d%d%d%d",&x1[i],&y1[i],&x2[i],&y2[i]);
__int64 ans;
for(i=; i<q; i++)
{
ans=;
for(j=x1[i]; j<=x2[i]; j++)
{
//cout<<row[j][y2[i]]<<" "<<row[j][y1[i]]<<endl;
ans+=row[j][y2[i]]-row[j][y1[i]];
}
for(j=y1[i]; j<=y2[i]; j++)
{
//cout<<column[x2[i]][j]<<" "<<column[x1[i]][j]<<endl;
ans+=column[x2[i]][j]-column[x1[i]][j];
}
cout<<ans<<endl;
} return ;
}

Codeforces 611C. New Year and Domino 动态规划的更多相关文章

  1. CodeForces 611C New Year and Domino (动态规划,DP)

    题意:给定一个h*w的网格,里面只有.和#,.表示空的,#表示禁止的,然后有q个询问,询问中给你两个坐标,分别是左上和右下,求在这两者中间的有多少种(竖着和横着)两个相邻的点. 析:一看到这个题目,肯 ...

  2. Codeforces 611C New Year and Domino(二维前缀和)

    题目大概说给一个n*m个格子,格子'.'表示可以放东西,多次询问矩形区域(x1,y1)-(x2,y2)有几种放一张1*2的骨牌的方案数. 分别考虑横着竖着放,预处理出二维的前缀和,即sum[x][y] ...

  3. Codeforces 611C New Year and Domino DP+容斥

    "#"代表不能放骨牌的地方,"."是可以放 500*500的矩阵,q次询问 开两个dp数组,a,b,a统计横着放的方案数,b表示竖着放,然后询问时O(1)的,容 ...

  4. 【CodeForces 611C】New Year and Domino

    题 题意 h行w列的矩形格子,“." 代表空的,"#" 代表满的,多米诺是 1*2 的长方体,现在放进格子,给你子矩形的左上角和右上角,问在子矩形里共有多少种放一块多米诺 ...

  5. Codeforces 835F Roads in the Kingdom - 动态规划

    题目传送门 传送点I 传送点II 传送点III 题目大意 给定一颗基环树,要求删去其中一条边,使得剩下的图形是一棵树,并且最长路的长度最短,求最长路的最短长度. 路径可以分为两部分:跨过环 和 在树内 ...

  6. Codeforces 581F Zublicanes and Mumocrates - 树形动态规划

    It's election time in Berland. The favorites are of course parties of zublicanes and mumocrates. The ...

  7. 【Codeforces 949D】Shake It! 【动态规划】

    参考: http://blog.csdn.net/gjghfd/article/details/77824901 所求的是满足条件的图中“不同构”的数量,意味着操作的顺序是可以忽略的.考虑若干次操作后 ...

  8. Codeforces 500 E. New Year Domino

    \(>Codeforces \space 500 E. New Year Domino<\) 题目大意 : 数轴上有序排列着 \(n\) 块多米诺骨牌, 第 \(i\) 块骨牌坐标为 \( ...

  9. Codeforces Round #335 Sorting Railway Cars 动态规划

    题目链接: http://www.codeforces.com/contest/606/problem/C 一道dp问题,我们可以考虑什么情况下移动,才能移动最少.很明显,除去需要移动的车,剩下的车, ...

随机推荐

  1. Node.js 介绍及安装

    Node.js是一个Javascript运行环境(runtime environment),发布于2009年5月,由Ryan Dahl开发,实质是对Chrome V8引擎进行了封装.本文详细介绍了No ...

  2. selenium ie 模拟request pahonjs

  3. smyfony2-curd-数据库创建

    1创建类

  4. 62. 用流程自带的打印功能,IE浏览器打印出来是空白

    用流程自带的打印功能,IE浏览器打印出来是空白的这个问题确认是由于IE启用了兼容模式导致的了把IE的兼容模式关掉就行了

  5. HTML5 Canvas ( 文字的度量 ) measureText

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. leetcode217

    public class Solution { public bool ContainsDuplicate(int[] nums) { var list = nums.Distinct(); if ( ...

  7. Linux批量查询替换字符串

    Linux 批量查询替换文本文件中的字符串: 1.批量查找某个目下文件的包含的内容,例如: #   grep -rn "要找查找的文本" ./ 2.批量查找并替换文件内容. #   ...

  8. CategoryPanelGroup动态生成节点

    afw TCategoryPanel *cp; ; i < TreeView1->Items->Count; i++) { ) { cp = new TCategoryPanel(C ...

  9. hashlib 加密模块使用说明

    import hashlib  #hashilib 模块 m = hashlib.md5() m.update('hello 天王盖地虎'.encode(encoding = 'utf-8)) m.h ...

  10. FD 设置字体大小

    英文版: 依次选择菜单 Tools ->Syntax Coloring 中文版本: 如依次选择菜单 工具 ->语法配色器