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

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

题意:  给你一个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=550+100;
const ll INF = 1ll<<61;
const int inf = 1000000007;
const int MOD= 1000007;

char mp[N][N];
int p[N][N],V[N][N];
int n,m;
int sss[4][2] ={1,0,0,1};
struct ss{
  int x,y;
};
int check(int x,int y) {
   if(x<=0||y<=0||x>n||y>m) return 1;
   return 0;
}
int v[N][N];
void bfs(int x,int y) {
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) {
                if(mp[i][j]!=‘#‘)
             for(int k=0;k<2;k++) {
                 int xx = i+sss[k][0];
                 int yy = j+sss[k][1];
                 if(check(xx,yy)) continue;
                 if(mp[xx][yy]==‘#‘) continue;
                 p[i][j]++;
             }

        }
    }
}
int main() {
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++) {
        getchar();
        for(int j=1;j<=m;j++) {
            scanf("%c",&mp[i][j]);
        }
    }
     bfs(1,1);

     for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) {
            V[i][j] = V[i][j]+V[i-1][j]+V[i][j-1]-V[i-1][j-1]+p[i][j];
        }
     }
    int q,x,y,x2,y2;
    scanf("%d",&q);
    for(int i=1;i<=q;i++) {
        scanf("%d%d%d%d",&x,&y,&x2,&y2);
        int  ans = V[x2][y2]-V[x2][y-1]-V[x-1][y2]+V[x-1][y-1];
        for(int j=x;j<x2;j++) {
            if(mp[j][y2]==‘.‘&&mp[j][y2+1]==‘.‘) ans--;
        }
        ans-=p[x2][y2];
        for(int j=y;j<y2;j++) {
            if(mp[x2][j]==‘.‘&&mp[x2+1][j]==‘.‘) ans--;
        }
        printf("%d\n",ans);
    }
}

代码

时间: 2024-08-23 10:41:08

Good Bye 2015 C. New Year and Domino 二维前缀的相关文章

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 thin

Codeforces 611C New Year and Domino(二维前缀和)

题目大概说给一个n*m个格子,格子'.'表示可以放东西,多次询问矩形区域(x1,y1)-(x2,y2)有几种放一张1*2的骨牌的方案数. 分别考虑横着竖着放,预处理出二维的前缀和,即sum[x][y]表示(1,1)-(x,y)的横着或者竖着放的方案数,然后对于每一个询问就拆成几个前缀和容斥一下.. 细节要注意..骨牌是1*2的.. 1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 char map[555

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 that tomorrow is the last day of the year 2015. Limak is a little polar bear. He enjoyed this year a lot. Now, he is so eager to the coming year 2016. Lima

Good Bye 2015 A

Problem A:http://codeforces.com/problemset/problem/611/A A. New Year and Days 题意:某人要在2016年收集糖果,有两种不同的收集方式,按week或month.x表示week的周x或月的x号,只有到每周或每月的x时才会收集一颗糖果,问:在给定的月或周条件内,2016年能收集多少颗糖果. 思路:我是用数组存好,其实也可以更简洁,周的话除了5,6,7是53个,其他都是52个:月的话1-29都是12(闰年),30是11,31是

codeforces Good Bye 2015 B. New Year and Old Property

题目链接:http://codeforces.com/problemset/problem/611/B 题目意思:就是在 [a, b] 这个范围内(1 ≤ a ≤ b ≤ 10^18)统计出符合二进制数表示只有 1 个 0 的数量是多少. *********************** 首先贴出一段绝对会超时的代码,连提交的勇气都木有呢~因为第四组数据已经接近龟速了,所以...(大家可忽略) 1 #include <iostream> 2 #include <cstdio> 3 #

Good Bye 2015 B. New Year and Old Property

题目链接:http://codeforces.com/problemset/problem/611/B 解题思路: 直接暴力推出所有符合条件的. 由进制转换可以知道,二进制只有1个0也就是十进制减去前面任意一个2的次方 然后脑残一样的用了位运算,死都无法表示64位,只能32位,还以为电脑出问题了..换了pow秒过... 实现代码: #include<bits/stdc++.h> using namespace std; #define ll long long int main() { ll

58同城2015校招笔试、一面、二面经历

10.18 宣讲 58宣讲时间真是安排的晚...19.30开始,我6.30就到了..整整放了1个小时不重复的视频.....我听完他们CSO对行业和公司的介绍就走了.感觉58可能是o2o的下一个爆发点.感觉蛮有前景的.宣讲会也是和小米的宣讲差不多,过道上都挤满了人这种.我个人还是比较些向往去58的.个人感觉对于O2O,58算是赶了个早集..把最脏最累的活给做了..反而是美团,大众点评这种抓住了热点...当然,未来的大趋势也是O2O,就看58能不能赶上这趟快车了. 10.19 笔试 昨天的唯品会和中

HDU 5251 矩形面积(二维凸包旋转卡壳最小矩形覆盖问题) --2015百度之星题目

B - 矩形面积 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description 小度熊有一个桌面,小度熊剪了很多矩形放在桌面上,小度熊想知道能把这些矩形包围起来的面积最小的矩形的面积是多少. Input 第一行一个正整数 T,代表测试数据组数(),接下来 T 组测试数据. 每组测试数据占若干行,第一行一个正整数 ,代表矩形的数量.接下来 N 行,每行 8

2015.12.13 二维数组 函数指针 结构体

先说一下指针的用途:1.访问函数,在函数内部需要改变外部传入内部的值:2.函数中需得到一个连续存储空间的首地址:3.动态分配内存,需要记录分配内存的首地址.说穿了,指针的用途就是和地址相关的. 二维数组 定义方法 ①int temp1 [2][3] = {}; 第一个中括号是“行”,第二个中括号是“列”. ②int temp2 [][3] = {1,2,3,4,5,6}; “列数”不需要明确指出. ③int temp3 [2][3] = {1,2,3,4}; 后两个元素为0. char *nam