C. New Year and Domino
 

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 numbered1 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 r1ic1ir2ic2i (1 ≤ r1i ≤ r2i ≤ h, 1 ≤ c1i ≤ c2i ≤ w) — the i-th query. Numbers r1i andc1i 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.

题意:  给你一个n*m的图,有 . , #  ,#号不可走

    q个询问

    每个询问对于一个矩形,问的就是 在这个矩形内 有多少条 路径为长度2 的路

题解:我们能求出每一个点 是否可以向下向右走的 即值为 2,1,0,

    那么从 1,1到 x,y 这个矩形内可以用二维前缀数组求出来

    每次询问 答案就是  简单容斥了,注意  边界及 只有 两个方向的情况

//meek
#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include<map>
#include<queue>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair const int N=+;
const ll INF = 1ll<<;
const int inf = ;
const int MOD= ; char mp[N][N];
int p[N][N],V[N][N];
int n,m;
int sss[][] ={,,,};
struct ss{
int x,y;
};
int check(int x,int y) {
if(x<=||y<=||x>n||y>m) return ;
return ;
}
int v[N][N];
void bfs(int x,int y) {
for(int i=;i<=n;i++) {
for(int j=;j<=m;j++) {
if(mp[i][j]!='#')
for(int k=;k<;k++) {
int xx = i+sss[k][];
int yy = j+sss[k][];
if(check(xx,yy)) continue;
if(mp[xx][yy]=='#') continue;
p[i][j]++;
} }
}
}
int main() {
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) {
getchar();
for(int j=;j<=m;j++) {
scanf("%c",&mp[i][j]);
}
}
bfs(,); for(int i=;i<=n;i++) {
for(int j=;j<=m;j++) {
V[i][j] = V[i][j]+V[i-][j]+V[i][j-]-V[i-][j-]+p[i][j];
}
}
int q,x,y,x2,y2;
scanf("%d",&q);
for(int i=;i<=q;i++) {
scanf("%d%d%d%d",&x,&y,&x2,&y2);
int ans = V[x2][y2]-V[x2][y-]-V[x-][y2]+V[x-][y-];
for(int j=x;j<x2;j++) {
if(mp[j][y2]=='.'&&mp[j][y2+]=='.') ans--;
}
ans-=p[x2][y2];
for(int j=y;j<y2;j++) {
if(mp[x2][j]=='.'&&mp[x2+][j]=='.') ans--;
}
printf("%d\n",ans);
}
}

代码

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

  1. New Year and Domino 二维前缀和

    C. New Year and Domino time limit per test 3 seconds memory limit per test 256 megabytes input stand ...

  2. Codeforces Good Bye 2015 C. New Year and Domino 前缀和

    C. New Year and Domino 题目连接: http://www.codeforces.com/contest/611/problem/C Description They say &q ...

  3. Good Bye 2015 C - New Year and Domino

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

  4. TTTTTTTTTTTTT CF Good Bye 2015 C- New Year and Domino(CF611C) 二维前缀

    题目 题意:给你一个n*m由.和#组成的矩阵,.代表可以放,#代表不可以,问在左上角(px,py)到(右下角qx,qy)这样的一个矩阵中,放下一个长度为2宽度为1的牌有多少种放法: #include ...

  5. New Year and Domino:二维前缀和

    题目描述: They say "years are like dominoes, tumbling one after the other". But would a year f ...

  6. Codeforces 611C New Year and Domino(二维前缀和)

    题目大概说给一个n*m个格子,格子'.'表示可以放东西,多次询问矩形区域(x1,y1)-(x2,y2)有几种放一张1*2的骨牌的方案数. 分别考虑横着竖着放,预处理出二维的前缀和,即sum[x][y] ...

  7. CODESOFT 2015中的二维码该怎样生成

    由于二维条码具有储存量大.保密性高.追踪性高.抗损性强.备援性大.成本便宜等特性,其应用 渐趋广泛,因此二维码的制作对于CODESOFT条码设计软件的用户来讲可谓司空见惯.我们最常见的二维码要数QR码 ...

  8. Good Bye 2015 D. New Year and Ancient Prophecy

    D. New Year and Ancient Prophecy time limit per test 2.5 seconds memory limit per test 512 megabytes ...

  9. Good Bye 2015 B. New Year and Old Property 计数问题

    B. New Year and Old Property   The year 2015 is almost over. Limak is a little polar bear. He has re ...

随机推荐

  1. Android的一些常用命令提示符(cmd)指令

    在<Android基础之用Eclipse搭建Android开发环境和创建第一个Android项目>中我曾介绍过如何给Android SDK配置环境变量,现在它就有用武之地了,我们可以直接在 ...

  2. jquery 源码学习(一)

    从上边的注释看,jQuery的源码结构相当清晰.条理,不像代码那般晦涩和让人纠结   1. 总体架构 1.1 自调用匿名函数 self-invoking anonymous function 打开jQ ...

  3. 8 C#中的字符串输出

    我们在前面已经用Console.WriteLine("*********")往dos窗口中输出过字符串.我们还定义过字符串的变量. string words ="我喜欢D ...

  4. [SSH服务]——一个SSH无密码登陆实验

    实验拓扑图 实验描述 机房内有两台服务器: (1)B服务器10.0.10.158,充当Web服务器,有普通用户user_00 (2)C服务器10.0.10.191,充当Mysql服务器,有普通用户us ...

  5. MATLAB GUI程序设计中使文本框接收多行输入的方法

    对于文本框来说 Max属性于Min属性数值之差小于等于1时,仅接收单行输入 大于1时,接受多行输入 对于多行情况,set/get到的String应为cell 本系列文章允许转载,转载请保留全文! [说 ...

  6. online training

    https://www.skillfeed.com/browse http://teamtreehouse.com/features http://www.pluralsight.com/ https ...

  7. 移植net-snmp到开发板(mini210)

    1.安装交叉编译工具arm-linux-gcc 2.下载net-snmp源码安装包 3.解压安装包 4../configure --build=i686-linux --host=arm-linux ...

  8. linux free 命令

    命 令: free 功能说明:显示内存状态. 语 法: free [-bkmotV][-s <间隔秒数>] 补充说明:free指令会显示内存的使用情况,包括实体内存,虚拟的交换文件内存,共 ...

  9. Careercup - Google面试题 - 4877486110277632

    2014-05-08 05:16 题目链接 原题: Given a circle with N defined points and a point M outside the circle, fin ...

  10. Mybatis错误调试(二)

    错误日志信息:  Caused by: java.sql.BatchUpdateException: ORA-00911: 无效字符 at oracle.jdbc.driver.OraclePrepa ...