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. php基础和数据库

    服务器和客户端 客户端 程序: 通过浏览器直接运行 服务器 程序: 通过安装某种服务器软件   程序才可以运行              apache   php文件              tom ...

  2. 报错:ORA-02264

    创建表时报错ORA-02264:名称已被一个现有约束条件占用 查询约束名称“PK_DATASOUCE”,然后删除. SELECT a.* FROM user_constraints a where c ...

  3. [Flutter] Image.File 加载图像时文件内容变化显示不变解决

    在Flutter中,我们可以用下面的代码从文件中加载图像: Image.file(File(_fileName)); 这个时候,当_fileName这个文件名称和路径不变,文件内容变化时,Flutte ...

  4. hadoop命令帮助

    安装完hadoop后,在hadoop的bin目录下有一系列命令: container-executor hadoop hadoop.cmd hdfs hdfs.cmd mapred mapred.cm ...

  5. 31. Studio获取新的ID值方法

    var fun = ABS_LOADBEAN("com.plug.FunctionHelper");var vid1 = fun.utilHelper.getNextID(&quo ...

  6. shiro与threamleaf的整合

    1.添加依赖 2.在配置类中添加shiroDialect

  7. Activity服务类-6 ManagementService服务类

    一共含有17个方法 // 获取包含了Activiti数据库模式的{表名.行计数}项的映射.Map<String, Long> getTableCount();//获取诸如任务.执行之类的A ...

  8. eclipse从svn导入maven项目变成普通项目解决办法

    右击项目-->configure-->Convert to Maven Project

  9. python之建完model之后操作admin

    1)建完model 之后,运行./manage.py migrate 2)建立管理员:./manage.py createsuperuser 3)输入用户名和命令上提示的信息,在点击网址,输入admi ...

  10. ubuntu 安装u盘恢复

    XP下进入CMD命令窗体,Vista及7/8下右键以管理员方式运行DOS窗体(win8.1:开始屏幕-windows系统-命令提示符) 输入DISKPART,会显示计算机名,及DISKPART> ...