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. VB6.0编程笔记——(1)篇外篇&目录

    从计算机专业毕业到进入IT行业,说来也有些年头了.相比较而言算是幸运,也有很多的同学进入了其他行业,也有一些朋友又想进入这个行业.现在回想自己的一路历程,总结一下,也是一份记忆. 基于以上的原因,希望 ...

  2. bjfu1277 简单递归

    比较简单的递归问题.对于第k时刻的图形,可以平均分成四块,左上,右上,左下这三块的图形是一模一样的,右下的那一块不包含红毛僵尸,所以把那三块里的加起来就是结果了. /* * Author : ben ...

  3. Unreal Engine 虚幻引擎宣布对开发者免费

    虚幻引擎4现在可供每个人免费使用,而且所有未来的更新都将免费!您可以下载引擎并将其用于游戏开发的各个方面,包括教育.建筑以及可视化,甚至虚拟现 实.电影和动画. 当您发布游戏或应用时,在您的每个游戏在 ...

  4. 14个最受欢迎的Python开源框架

    本文从GitHub中整理出的14个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python Web应用开发框架 Dja ...

  5. Java WEB —— XML

    XML语言(可扩展标记语言W3C):描述一系列有关系的数据,允许自定义标签,它常用作软件配置文件,以描述程序模块之间的关系. XML语法:文档声明,元素,注释(DATA区,特殊字符,处理指令(proc ...

  6. Mellanox vma

    1,Mellanox offical vma Installation guide personal reading summarize VMA是一个消息加速器messaging accelerato ...

  7. [翻译]创建ASP.NET WebApi RESTful 服务(10)

    通过URI实现版本管理 另一种实现版本管理的方式就是通过URI来进行处理,类似于http://localhost:{your_port}/api/v1/students/.这种方式的好处是使用者可以清 ...

  8. android学习笔记(入门篇)

    +号只是当你第一次定义一个资源ID的时候需要, 告诉SDK此资源ID需要被创建出来 对于所有的View默认的权重是0,如果你只设置了一个View的权重大于0,那么这个View将占据除去别的View本身 ...

  9. C++11类型推导

    [C++11类型推导] auto 关键字.这会依据该初始化子(initializer)的具体类型产生参数: 除此之外,decltype 能够被用来在编译期决定一个表示式的类型. 参考:http://z ...

  10. psutil documentation

    Quick links Home page Blog Download Forum What’s new About From project’s home page: psutil (python ...