CF Good Bye 2015 C- New Year and Domino(CF611C)

来源:互联网 发布:java防止接口频繁调用 编辑:程序博客网 时间:2024/05/16 04:57

http://codeforces.com/contest/611/problem/C

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 1through 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....#..#.#......##.#....##..#.##........41 1 2 34 1 4 11 2 4 52 5 5 8
output
401015
input
7 39........................................###..###..#..###.....###..###..#..###....#..#.#..#..#.........#..#.#..#..#....###..#.#..#..###.....###..#.#..#..###..#....#.#..#....#.....#....#.#..#..#.#..###..###..#..###.....###..###..#..###........................................61 1 3 202 10 6 302 10 7 302 2 7 71 7 7 71 8 7 8
output
53891202302
Note

A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways.

题意:给你一张图,让你数有多少对能连起来的点(任何一个点都能和上下左右的点连起来)

思路:q有100000,那爆搜肯定要超时,然后最直接想到的就是能不能打表来降低时间复杂度,第一思路是用一个二维数组a[i][j]来存前i行j列有多少个点,然后我们假设全部都是点,算会连多少条线,接着我们只需要输出正方形四条边的上有几个#就行了,因为每个不在边上的#会使答案减8,在边上的#会使答案减6,在顶点的#会使答案减4,这样子时间复杂度就只有O(HQ)了,但毕竟q是100000,可能还是要超,后来又想了想,为什么不直接用数组来存每片区域有多少对点呢?用两个数组来存,一个存竖着的,一个存横着的,之后再进行简单的类似于面积计算就行了

code:

#include <iostream>#include <cstdio>#include <queue>#include <stack>#include <map>using namespace std;char m[505][505];int a[505][505];int b[505][505];int main(){    int h,w;    cin>>h>>w;    for(int i=1;i<=h;i++)    {        int n=0;        for(int j=1;j<=w;j++)        {            cin>>m[i][j];        }    }    for(int i=1;i<=h;i++)    {        for(int j=1;j<=w;j++)        {            if(m[i][j]=='.' && m[i-1][j]=='.') a[i][j]++;            if(m[i][j]=='.' && m[i][j-1]=='.') b[i][j]++;            a[i][j]+=a[i-1][j]+a[i][j-1]-a[i-1][j-1];            b[i][j]+=b[i-1][j]+b[i][j-1]-b[i-1][j-1];        }    }    int x1,y1,x2,y2,q;    cin>>q;    while(q--)    {        cin>>x1>>y1>>x2>>y2;        cout<< a[x2][y2]-a[x1][y2]-a[x2][y1-1]+a[x1][y1-1] + b[x2][y2]-b[x1-1][y2]-b[x2][y1]+b[x1-1][y1]<<endl;    }    return 0;}


0 0