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:简单实现ViewPager+TabHost+TabWidget实现导航栏导航和滑动切换

    viewPager是v4包里的一个组件,可以实现滑动显示多个界面. android也为viewPager提供了一个adapter,此adapter最少要重写4个方法: public int getCo ...

  2. Android线程---UI线程和非UI线程之间通信

        近期自学到了线程这一块,用了一上午的时间终于搞出来了主.子线程间的相互通信.当主线程sendMessage后,子线程便会调用handleMessage来获取你所发送的Message.我的主线程 ...

  3. 7.Knockout.Js(Mapping插件)

    前言 Knockout设计成允许你使用任何JavaScript对象作为view model.必须view model的一些属性是observable的,你可以使用KO绑定他们到你的UI元素上,当这些o ...

  4. qu

    离骚 (3481人评分) 8.5   朝代:先秦 作者:屈原 原文: 帝高阳之苗裔兮,朕皇考曰伯庸.摄提贞于孟陬兮,惟庚寅吾以降.皇览揆余初度兮,肇锡余以嘉名:名余曰正则兮,字余曰灵均.纷吾既有此内美 ...

  5. GNU make 总结 (四)

    一.执行make程序 make的退出状态: 0 --- 表示执行成功 1 --- 表示执行make时使用了“-q”参数,而且当前工程中存在过时的目标文件 2 --- 执行过程中出现了错误,同时会提示错 ...

  6. 2.Modelsim打开时出现的Error

    Modelsim之error “unable to check out a viewer license necessary for use of the modelsim graph.Vsim is ...

  7. cocos2dx中导演的职责有哪些?

    1.一个游戏里面只有一个导演,因此采用了单例的设计模式,用getInstance方法来获得 2.游戏中导演负责openGL ES的初始化,场景的切换,游戏的暂停继续(相当于拍电影的ka),节点坐标与世 ...

  8. 用telnet和php的curl库测试http

    一.telnet测试http telnet简介     Telnet协议是TCP/IP协议族的其中之一,是Internet远端登录服务的标准协议和主要方式,常用于网页服务器的远端控制,可供使用者在本地 ...

  9. adb怎么判断是否有root权限,并更改system/app内容

    一.首先判断root权限: adb root 结果: C:\signapp>adb root restarting adbd as root # 说明有root权限 ,若是adbd cannot ...

  10. IIS8中 出现ashx 401:未授权,uploadify上传文件失败

    环境:阿里云服务器 windows2012  + IIS8 +asp.net 访问IIS 出现能正常访问aspx页面,但是通过ajax访问ashx上传文件的时候就出现ashx  Status Code ...