C. New Year and Domino

题目连接:

http://www.codeforces.com/contest/611/problem/C

Description

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 Input

5 8

....#..#

.#......

.#....

..#.##

........

4

1 1 2 3

4 1 4 1

1 2 4 5

2 5 5 8

Sample Output

4

0

10

15

Hint

题意:

给你一个500 500的矩形区域,然后有一些位置是不能放的。有Q次询问,每次询问问你在(x1,y1,x2,y2)矩形区域中,能放入多少个1*2的矩形块。

题解

我们横着和竖着的都分开考虑。横着和竖着能放多少个呢?我们维护前缀和去解决就好了,如果上一位是空位,并且这一位置也是空位,就可以+1。n^2预处理,O(500)的单次询问。

代码

#include<bits/stdc++.h>
using namespace std; int h,w;
char s[505][505];
int num[505][505];
int A[505][505];
int B[505][505];
int main()
{
cin>>h>>w;
for(int i=1;i<=h;i++)
scanf("%s",s[i]+1);
for(int i=1;i<=h;i++)
for(int j=1;j<=w;j++)
if(s[i][j]=='.')num[i][j]=1;
else num[i][j]=0;
for(int i=1;i<=h;i++)
{
for(int j=1;j<=w;j++)
{
if(num[i][j]&&num[i][j+1])A[i][j]=A[i][j-1]+1;
else A[i][j]=A[i][j-1];
}
}
for(int i=1;i<=w;i++)
{
for(int j=1;j<=h;j++)
{
if(num[j][i]&&num[j+1][i])B[j][i]=B[j-1][i]+1;
else B[j][i]=B[j-1][i];
}
}
int q;cin>>q;
while(q--)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
int ans = 0;
for(int i=x1;i<=x2;i++)
ans+=A[i][y2-1]-A[i][y1-1];
for(int i=y1;i<=y2;i++)
ans+=B[x2-1][i]-B[x1-1][i];
printf("%d\n",ans);
}
}

Codeforces Good Bye 2015 C. New Year and Domino 前缀和的更多相关文章

  1. Codeforces Good bye 2015 B. New Year and Old Property dfs 数位DP

    B. New Year and Old Property 题目连接: http://www.codeforces.com/contest/611/problem/B Description The y ...

  2. Codeforces Good Bye 2015 A. New Year and Days 水题

    A. New Year and Days 题目连接: http://www.codeforces.com/contest/611/problem/A Description Today is Wedn ...

  3. codeforces Good Bye 2015 B. New Year and Old Property

    题目链接:http://codeforces.com/problemset/problem/611/B 题目意思:就是在 [a, b] 这个范围内(1 ≤ a ≤ b ≤ 10^18)统计出符合二进制 ...

  4. Codeforces Good Bye 2015 D. New Year and Ancient Prophecy 后缀数组 树状数组 dp

    D. New Year and Ancient Prophecy 题目连接: http://www.codeforces.com/contest/611/problem/C Description L ...

  5. Good Bye 2015 C. New Year and Domino 二维前缀

    C. New Year and Domino   They say "years are like dominoes, tumbling one after the other". ...

  6. Good Bye 2015 C - New Year and Domino

    题意:计算给定矩形面积(r1,c1),(r2,c2)内长度为2的有多少个?向右或向下计算. 思路:预处理字符.分别向右和向下处理.注意边界情况,可能算多了.用容斥原理计算长度为二的单位. #inclu ...

  7. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  8. [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】

    [CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...

  9. Codeforces:Good Bye 2018(题解)

    Good Bye 2018! 题目链接:https://codeforces.com/contest/1091 A. New Year and the Christmas Ornament 题意: 给 ...

随机推荐

  1. smtp邮件营销吧

    SPF 设置说明: 首先你必须有自己的域名.没有的话是不可能设置 SPF 的. SPF 是域名的一条 TXT 记录. 如果你的邮箱服务器是企业邮箱服务商的,可以在自己的 SPF 中直接包含服务商 SP ...

  2. 浏览器的DNS缓存

    通过设置hosts文件可以强制指定域名对应的IP,当修改hosts文件,想要浏览器生效,最直接的方法关闭浏览器后重新开启:如果不想重启浏览器,只需要清空浏览器的DNS缓存即可.清空DNS缓存在chro ...

  3. 让 PowerDesigner 支持 SQLite!

    让 PowerDesigner 支持 SQLite!   PowerDesigner是一个功能强大的数据库设计软件,最近正在用其设计新系统的数据库,但由于在项目初级阶段,希望使用轻量级的 SQLite ...

  4. Java之--Java基础知识

    一.软件基础知识 1.软件:一系列按特定顺序组织的计算机数据和指令的集合. 1)系统软件:DOS.windows.linux. 2)应用软件:QQ.扫雷等. 2.软件开发就是制作软件. 3.人与计算机 ...

  5. IOS播放音乐和音效

    1.播放音效 1.1 首先获取到音效文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:soundFileName ofType: ...

  6. (转载)OC学习篇之---Foundation框架中的NSObject对象

    前一篇文章讲到了OC中的代理模式,而且前几篇文章就介绍了OC中的类相关知识,从这篇文章开始我们开始介绍Foundation框架. OC中的Foundation框架是系统提供了,他就相当于是系统的一套a ...

  7. 生成500个0-1000的随机数&&数组查找—小练习

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. kali系统安装图文教程

    工具和原料 1.虚拟机:Oracle VM VirtualBox 下载地址:https://www.virtualbox.org/wiki/Downloads 根据你自己的计算机操作系统下载,其中如果 ...

  9. Spark shell的原理

    Spark shell是一个特别适合快速开发Spark原型程序的工具,可以帮助我们熟悉Scala语言.即使你对Scala不熟悉,仍然可以使用这个工具.Spark shell使得用户可以和Spark集群 ...

  10. c/c++,输入一个字符 2014-11-20 07:00 30人阅读 评论(0) 收藏

    getch().getche()和getchar()函数     (1) getch()和getche()函数     这两个函数都是从键盘上读入一个字符.其调用格式为:      getch(); ...