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. 使用SchemaSpy逆向工程生成数据库依赖关系使用SchemaSpy工具可以快速的从数据库中得到

    使用SchemaSpy逆向工程生成数据库依赖关系    使用SchemaSpy工具可以快速的从数据库中得到表的依赖关系,同时生成一个生动的“表图”结合的报告.方便快速了解数据库中的数据库对象间关系,类 ...

  2. Android圆形图片--ImageView

    [ RoundImageView.java ] package com.dxd.roundimageview; import android.content.Context; import andro ...

  3. cocoa中获取时间

    头文件#import "Foundation/NSCalendarDate.h" + (id)calendarDate; - (int)yearOfCommonEra;- (int ...

  4. 用ASP.Net写一个发送ICQ信息的程序

    用ASP.Net写一个发送ICQ信息的程序 这里我给大家提供一个很实用的例子,就是在线发送ICQ信息.想一想我们在网页上直接给朋友发送ICQ信息,那是多么美妙的事情啊.呵呵,在吹牛啊,其实ICQ本来就 ...

  5. 【LeetCode】165 - Compare Version Numbers

    Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 &l ...

  6. the hard thing about hard things 书摘

    1. from communist to VC 领导力是什么,书后面还举了乔布斯的例子,比如NEXT公司时期就是如此,是什么吸引了那些人在前景不明时还跟随乔布斯?   作者用自己与妻子的相遇说明,不要 ...

  7. scala 连接 mysql

    code: import java.sql.{ResultSet, DriverManager} import com.mysql.jdbc.Connection object hoursAvg { ...

  8. JS简单入门教程

    JS简单教程 使用方法:放到任意html页面的head标签下 Test1方法弹出当前时间对话框 Test2方法for循环输出 Test3方法for(…in…)输出数组内容 <script typ ...

  9. [Lua]入门教程

    什么是Lua Lua 是一个小巧的脚本语言.是巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个研究小组,由Rober ...

  10. font-size的探究

    整理网上的资料 font字体,用px,em,100%,rem?分什么情况考虑? 我们逐渐意识到,我们用px作为文字大小的单位,已经出现很多问题.最主要是体现在用户不能灵活的控制文字的大小. 对于大多数 ...