C. New Year and Domino
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 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.

题意:输入一个矩阵  '.'代表空地  q个询问 每个询问 两组坐标 求其构成的长方形   中如上图占据两个空格  有多少种方法题解:C题耗了好久  重点在预处理 遍历一遍矩阵 只 向左 向右两个方向 寻找满足的情况 并累加同时更新 anss[i][j]=ans-anss[i-1][d]+anss[i-1][j]; anss[i][j] 代表(1,1)与(i,j)组成的矩阵中有多少种方法输出询问的结果re=anss[r2][c2]-anss[r1-1][c2]-anss[r2][c1-1 ]+anss[r1-1][c1-1]-flag;重点说一下 flag是什么? 为询问的矩阵的左边向左与上边向上 能构成一种方法的总数SAY goodbye 201524小时就睡了2小时 和基地友出去玩还是想花时间 码个日志!!!


#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#define LL __int64
using namespace std;
int h,w;
int q;
char mp[505][505];
int dis[2][2]={{0,-1},{-1,0}};
int gg[505][505];
int anss[505][505];
void fun(int a,int b,int c,int d)
{
int ans=0;
for(int i=a;i<=c;i++)
for(int j=b;j<=d;j++)
{
if(mp[i][j]=='.')
{
for(int k=0;k<2;k++)
{
int xx=i+dis[k][0];
int yy=j+dis[k][1];
if(xx>=a&&xx<=c&&yy>=b&&yy<=d&&mp[xx][yy]=='.')
ans++;
}
}
anss[i][j]=ans-anss[i-1][d]+anss[i-1][j];
}
}
int main()
{
scanf("%d%d",&h,&w); for(int i=1; i<=h; i++)
{
getchar();
for(int j=1; j<=w; j++)
scanf("%c",&mp[i][j]);
}
fun(1,1,h,w);
scanf("%d",&q);
for(int i=1;i<=q;i++)
{ int r1,c1,r2,c2;
scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
int flag=0;
for(int k=c1;k<=c2;k++)
{
if(mp[r1][k]=='.')
{
if(mp[r1-1][k]=='.')
flag++;
}
}
for(int k=r1;k<=r2;k++)
{
if(mp[k][c1]=='.')
{
if(mp[k][c1-1]=='.')
flag++;
}
}
//cout<<flag<<endl;
printf("%d\n",anss[r2][c2]-anss[r1-1][c2]-anss[r2][c1-1 ]+anss[r1-1][c1-1]-flag);
}
return 0;
}

  

Good Bye 2015 C的更多相关文章

  1. 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 ...

  2. 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 ...

  3. Good Bye 2015 A. New Year and Days 签到

    A. New Year and Days   Today is Wednesday, the third day of the week. What's more interesting is tha ...

  4. 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 ...

  5. 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 ...

  6. Good Bye 2015 B. New Year and Old Property —— dfs 数学

    题目链接:http://codeforces.com/problemset/problem/611/B B. New Year and Old Property time limit per test ...

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

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

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

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

  9. 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 ...

  10. 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 ...

随机推荐

  1. Python常用函数--return 语句

    在Python教程中return 语句是函数中常用的一个语句.return 语句用于从函数中返回,也就是中断函数.我们也可以选择在中断函数时从函数中返回一个值.案例(保存为 function_retu ...

  2. Python的包(Packages)

    包,Package,是一种Python模块的集合,从文件组织形式上看,包就是一个文件夹,里面放着各种模块(.py文件),也可以有子文件夹(子包).包名构建了一个Python模块的命名空间.比如,模块名 ...

  3. 牛客练习赛26:D-xor序列(线性基)

    链接:牛客练习赛26:D-xor序列(线性基) 题意:小a有n个数,他提出了一个很有意思的问题:他想知道对于任意的x, y,能否将x与这n个数中的任意多个数异或任意多次后变为y 题解:线性基 #inc ...

  4. Github协作图想

    首先 git pull 从远程拉下代码,并在本地与本地代码自动合并 在本地解决冲突后,可将本地代码进行远程推送 版本库的Repository中存储的是版本树状链,每一根链接线代表每一次的修改,每一个节 ...

  5. [Clr via C#读书笔记]Cp17委托

    Cp17委托 简单介绍 delegate回调函数机制,可以理解存储函数地址的变量类型: 类型安全: 引用类型支持逆变和协变: 回调 静态方法,实例方法 委托的本质 所有的委托都派生自System.Mu ...

  6. Python最长连续数列的O(n)解法

    题目 输入一个乱序的连续数列,输出其中最长连续数列长度,要求算法复杂度为 O(n) . 输入样例 100,4,200,1,3,2 54,55,300,12 1 5,4,3,2,1 1,2,3,4,5, ...

  7. 洛谷 P1706 全排列问题 :STL / dfs

    题目描述 输出自然数1到n所有不重复的排列,即n的全排列,要求所产生的任一数字序列中不允许出现重复的数字. 输入输出格式 输入格式: n(1≤n≤9) 输出格式: 由1-n组成的所有不重复的数字序列, ...

  8. Linux内核设计笔记14——块I/O层

    块I/O层 基本概念 系统中可以随机访问固定大小数据片的硬件设备称做块设备,这些固定大小的数据片称之为块.还有一种基本的设备称之为字符设备,其需要按照顺序访问,比如键盘. 扇区:块设备中最小的寻址单元 ...

  9. hadoop问题集(2)

      28. Sqoop: java.lang.NullPointerException sqoop import --connect jdbc:oracle:thin:@//xxxx:1521/aps ...

  10. 什么是Processing

    Processing是一种计算机语言,以JAVA语法为基础,可转化成JAVA程序,不过在语法上简易许多.所有的原始代码及开发环境开放,主要用于艺术.影像.影音的设计与处理. 其次为什么要介绍这款软件呢 ...