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

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
....#..#
.#......
##.#....
##..#.##
........
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有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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章