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. 开发组件:ZeroMQ

    ZeroMQ https://blog.csdn.net/w174504744/article/details/73187697

  2. Mysql导出表结构、表数据

    导出 (cmd)     1.导出數據库為dbname的表结构(其中用戶名為root,密码為dbpasswd,生成的脚本名為db.sql)    mysqldump -u root -p dbpass ...

  3. OpenCL 图像卷积 3 使用 CPU

    ▶ CPU 图像卷积,共四种方法.分别为基本串行,使用模板,使用局部内存,使用AVX指令优化 ● 全部的代码,仅在主函数中选择调用的函数名即可. #include <stdio.h> #i ...

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

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

  5. TP5常量

    预定义常量 EXT 类库文件后缀(.php) THINK_VERSION 框架版本号 路径常量 DS 当前系统的目录分隔符 THINK_PATH 框架系统目录 ROOT_PATH 框架应用根目录 AP ...

  6. 前端-javascript-正则表达式

    1.概念 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  7. MySQLReport

    简介: MySQLReport 一.安装 shell > yum -y install mysqlreport perl-DBD-MySQL 二.使用 shell > mysqlrepor ...

  8. 四大域对象,和jsp的九大隐式对象,已经el表达式的11个隐式对象

    四大域对象 1.PageContext :页面范围的数据 2.ServletRequest:请求范围的数据 3.HttpSession:回话范围的数据 4.ServletContext:应用范围的数据 ...

  9. C++之继承与多态

    在程序设计领域,一个广泛认可的定义是“一种将不同的特殊行为和单个泛化记号相关联的能力”.和纯粹的面向对象程序设计语言不同,C++中的多态有着更广泛的含义.除了常见的通过类继承和虚函数机制生效于运行期的 ...

  10. hadoop+zookeeper(ha架构搭建)

    http://blog.csdn.net/baidu_25820069/article/details/52225293 [条件所限,待验证]