codeforces Good Bye 2015 C - New Year and Domino

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

Examples
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

题意:有一个矩阵由’.’和’#’组成,一个多米诺骨牌只能放在两个连续的.,给你一个子矩阵,求有多少种方式放一块多米诺骨牌

思路:先预处理出每一行和每一列前i个有多少种不同的方式放一块多米诺骨牌,

然后查询的时候枚举行数从[l,r],每一行有多少个可利用前缀和的思想求得,枚举列数的时候也是同理。

时间复杂度为O(q.500)

或者可以预处理出一个矩阵中的方案数,查询的时候可以做到时间复杂度为O(1)

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef __int64 ll;
char s[501][501];
int row[501][501],col[501][501];

int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        mem0(col);
        mem0(row);
        for(int i=0;i<n;i++)
            scanf("%s",s[i]);
        for(int i=0;i<n;i++)
            for(int j=1;j<m;j++){
                if(s[i][j]==s[i][j-1]&&s[i][j]=='.')
                    row[i][j]=row[i][j-1]+1;
                else
                    row[i][j]=row[i][j-1];
            }
        for(int i=0;i<m;i++)
            for(int j=1;j<n;j++){
                if(s[j][i]==s[j-1][i]&&s[j][i]=='.')
                    col[i][j]=col[i][j-1]+1;
                else
                    col[i][j]=col[i][j-1];
            }
        int q;
        int r1,c1,r2,c2;
        scanf("%d",&q);
        while(q--){
            scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
            r1--,c1--,r2--,c2--;
            int ans=0;
            for(int i=r1;i<=r2;i++)
                ans+=row[i][c2]-row[i][c1];
            for(int i=c1;i<=c2;i++)
                ans+=col[i][r2]-col[i][r1];
            printf("%d\n",ans);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值