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

Examples

input

Copy

5 8....#..#.#......##.#....##..#.##........41 1 2 34 1 4 11 2 4 52 5 5 8

output

Copy

401015

input

Copy

7 39........................................###..###..#..###.....###..###..#..###....#..#.#..#..#.........#..#.#..#..#....###..#.#..#..###.....###..#.#..#..###..#....#.#..#....#.....#....#.#..#..#.#..###..###..#..###.....###..###..#..###........................................61 1 3 202 10 6 302 10 7 302 2 7 71 7 7 71 8 7 8

output

Copy

53891202302

Note

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

自闭的人生

#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#define  pi acos(-1.0)
#define  eps 1e-6
#define  fi first
#define  se second
#define  lson l,m,rt<<1
#define  rson m+1,r,rt<<1|1
#define  bug         printf("******\n")
#define  mem(a,b)    memset(a,b,sizeof(a))
#define  fuck(x)     cout<<"["<<x<<"]"<<endl
#define  f(a)        a*a
#define  sf(n)       scanf("%d", &n)
#define  sff(a,b)    scanf("%d %d", &a, &b)
#define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define  pf          printf
#define  FRE(i,a,b)  for(i = a; i <= b; i++)
#define  FREE(i,a,b) for(i = a; i >= b; i--)
#define  FRL(i,a,b)  for(i = a; i < b; i++)
#define  FRLL(i,a,b) for(i = a; i > b; i--)
#define  FIN         freopen("DATA.txt","r",stdin)
#define  gcd(a,b)    __gcd(a,b)
#define  lowbit(x)   x&-x
#pragma  comment (linker,"/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 2e5 + 10;
int n, m, sum[505][505], y[505][505], x[505][505];
char mp[505][505];
int main() {
    sff(n, m);
    for (int i = 1 ; i <= n ; i++)
        scanf("%s", mp[i] + 1);
    for (int i = 1 ; i <= n ; i++) {
        for (int j = 1 ; j <= m ; j++) {
            if (mp[i][j] == ‘.‘ && mp[i + 1][j] == ‘.‘ ) {
                sum[i][j]++;
                y[i][j] = 1;
            }
            if (mp[i][j] == ‘.‘ && mp[i][j + 1] == ‘.‘ ) {
                sum[i][j]++;
                x[i][j] = 1;
            }
            sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
        }
    }
    int q;
    sf(q);
    while(q--) {
        int x1, y1, x2, y2;
        sffff(x1, y1, x2, y2);
        int ans = sum[x2][y2] - sum[x1 - 1][y2] - sum[x2][y1 - 1] + sum[x1 - 1][y1 - 1];
        for (int i = x1 ; i <= x2 ; i++) ans -= x[i][y2];
        for (int i = y1 ; i <= y2 ; i++) ans -= y[x2][i];
        printf("%d\n", ans);
    }
    return  0;
}

原文地址:https://www.cnblogs.com/qldabiaoge/p/9452027.html

时间: 2024-10-22 16:32:51

New Year and Domino 二维前缀和的相关文章

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

openjudge1768 最大子矩阵[二维前缀和or递推|DP]

总时间限制:  1000ms 内存限制:  65536kB 描述 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵. 比如,如下4 * 4的矩阵 0 -2 -7 09 2 -6 2-4 1 -4 1-1 8 0 -2 的最大子矩阵是 9 2-4 1-1 8 这个子矩阵的大小是15. 输入 输入是一个N * N的矩阵.输入的第一行给出N (0 < N <= 100).再后面的若干行中,依次(首先从左到右给出第一行的N个整数,再从左到右给

计蒜客模拟赛D1T1 蒜头君打地鼠:矩阵旋转+二维前缀和

题目链接:https://nanti.jisuanke.com/t/16445 题意: 给你一个n*n大小的01矩阵,和一个k*k大小的锤子,锤子只能斜着砸,问只砸一次最多能砸到多少个1. 题解: 将原矩阵顺时针旋转45°,二维前缀和预处理,然后枚举每一个可能砸到的正方形之和并取最大. 注:枚举的正方形的四个顶点必须是从原矩阵璇转过来的点,代码中用vis数组判断. #include <iostream> #include <stdio.h> #include <string.

弱校联盟10.7 I. Special Squares(二维前缀和)

题目链接: I. Special Squares There are some points and lines parellel to x-axis or y-axis on the plane. If arbitrary chosen two lines parallel to x-axis and two lines parallel to y-axis, one rectangle, or sometimes a square, will be formed. If a square i

CDOJ 1256 二维前缀和处理

昊昊喜欢运动 他NN 天内会参加MM 种运动(每种运动用一个[1,m][1,m] 的整数表示) 舍友有QQ 个问题 问昊昊第ll 天到第rr 天参加了多少种不同的运动 Input 输入两个数NN , MM (1≤N≤20001≤N≤2000 , 1≤M≤1001≤M≤100 ); 输入NN 个数aiai 表示在第i天昊昊做了第aiai 类型的运动; 输入一个数QQ (1≤Q≤1061≤Q≤106 ); 输入QQ 行 每行两个数 ll , rr (1≤l≤r≤n1≤l≤r≤n ); Output

二维前缀和 - 算法学习 - 输入输出优化

2017-08-27 11:11:38 writer:pprp 二维前缀和主要用到了容斥定理,具体实现还是有点复杂的 详见代码: /* @theme:二维前缀和 @writer:pprp @declare:用到容斥定理 @date:2017/8/27 */ #include <bits/stdc++.h> using namespace std; const int maxn = 1010; int n, m, a[maxn][maxn]; //输入优化 inline int read() {

二维前缀和

一维前缀和 : 这个优化 , 可以在 O (1) 的时间内计算出一个序列的和 , 二维前缀和 : 对于一个矩阵 , 也可以在 O (1) 的时间内计算出矩阵 (x1~x2)( y1 ~ y2 ) 的和 . sum[ i ] [ j ] 表示矩阵 1 ~ i , 1 ~ j 的和 , 那么由容斥原理知 sum[ 0 ] [ j ] 和 sum [ i ] [ 0 ] 均为 0 . 则  s[ x1 ~ x2 ] [ y1 ~ y2 ] = sum[ x2 , y2 ] + sum [ x1 - 1

杭电2018多校第四场(2018 Multi-University Training Contest 4) 1005.Problem E. Matrix from Arrays (HDU6336) -子矩阵求和-规律+二维前缀和

6336.Problem E. Matrix from Arrays 不想解释了,直接官方题解: 队友写了博客,我是水的他的代码 ------>HDU 6336 子矩阵求和 至于为什么是4倍的,因为这个矩阵是左上半边有数,所以开4倍才能保证求的矩阵区域里面有数,就是图上的红色阴影部分,蓝色为待求解矩阵. 其他的就是容斥原理用一下,其他的就没什么了. 代码: 1 //1005-6336-矩阵求和-二维前缀和+容斥-预处理O(1)查询输出 2 #include<iostream> 3 #in

前缀和,二维前缀和!

前缀和 定义 用空间换取效率,做一个预处理,然后可以\(O(1)\)的查询某个区间的值的和. 实现 设\(s_i\)为第\(i\)个数\(a_i\)的前缀和,则\(s_i=s_i-1+a_i\) 当要查找区间的和时只要把对应的起点终点的元素相减即可. 例题 Educational Codeforces Round 30B Balanced Substring 翻译 给你一个长度至多为\(100000\)的\(01\)串,其中含有相同\(0\),\(1\)个数的子串被称为"平衡串",问你