Codeforces 611C: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.

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.


把题目想复杂了,每次都不看输入数据规模,实际上这个题目复杂度O(q*(m+n))轻松过。。。

题意是每次给出一块矩形区域,往里面放一个1*2的多米诺骨牌,里面有"#"不能放。问每次的矩形有多少种放多米诺骨牌的方法。

递推+二维的前缀和。

每次查询就先算每一行有多少种,前缀和相减。之后算每一列有多少种,也是前缀和相减。

代码:

#pragma warning(disable:4996)  
#include <iostream>  
#include <algorithm>
#include <cstring>
#include <cstring>
#include <vector>  
#include <string>  
#include <cmath>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;

#define INF 0x3fffffffffffffff
const int maxn = 505;

int h, w, q;
char val[maxn][maxn];
int prerow[maxn][maxn];
int precol[maxn][maxn];

void input()
{
	int i, j;
	scanf("%d%d", &h, &w);

	for (i = 1; i <= h; i++)
	{
		scanf("%s", val[i] + 1);
		for (j = 1; j <= w; j++)
		{
			if (val[i][j] == '.'&&val[i][j - 1] == '.')
			{
				prerow[i][j] = 1 + prerow[i][j - 1];
			}
			else
			{
				prerow[i][j] = prerow[i][j - 1];
			}
		}
	}
	for (j = 1; j <= w; j++)
	{
		for (i = 1; i <= h; i++)
		{
			if (val[i][j] == '.'&&val[i - 1][j] == '.')
			{
				precol[i][j] = precol[i - 1][j] + 1;
			}
			else
			{
				precol[i][j] = precol[i - 1][j];
			}
		}
	}
}

void solve()
{
	int i, j;
	int r1, c1, r2, c2;
	
	scanf("%d", &q);
	
	ll res = 0;
	for (i = 1; i <= q; i++)
	{
		scanf("%d%d%d%d", &r1, &c1, &r2, &c2);
		
		res = 0;
		for (j = r1 ; j <= r2; j++)
		{
			res += (prerow[j][c2] - prerow[j][c1]);
		}
		for (j = c1; j <= c2; j++)
		{
			res += (precol[r2][j] - precol[r1][j]);
		}

		printf("%I64d\n", res);
	}
}

int main()
{
	//freopen("i.txt", "r", stdin);
	//freopen("o.txt", "w", stdout);

	input();
	solve();
	
	//system("pause");
	return 0;
}



### Codeforces二维前缀和的应用 在解决涉及矩阵区域查询的问题时,二维前缀和是一种非常有效的工具。通过预先计算部分和,可以在常数时间内快速获取任意子矩形内的元素总和。 #### 什么是二维前缀和? 对于一个大小为 \( m \times n \) 的矩阵 `A`,定义其对应的前缀和矩阵 `sum` 如下: \[ sum[i][j] = A[0...i-1][0...j-1] \] 即 `sum[i][j]` 表示从原点 `(0, 0)` 到位置 `(i-1, j-1)` 所构成的矩形区域内所有元素之和[^2]。 为了方便处理边界情况,通常会将索引偏移一位,在实际编程中使用 `sum[i+1][j+1]` 来表示上述范围内的累加值。 #### 计算方法 构建前缀和的过程可以通过双重循环完成,时间复杂度为 O(mn),其中 m 和 n 分别代表矩阵的高度和宽度。核心代码如下所示: ```cpp for(int i = 1; i <= h; ++i){ for(int j = 1; j <= w; ++j){ // 当前格子加上左边、上面以及左上的三个方向已经累积的结果 sum[i][j] = A[i-1][j-1] + sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1]; } } ``` 这里需要注意减去重复计算的部分 `- sum[i-1][j-1]`,因为这部分被前面两次相加操作多算了。 #### 查询指定矩形区域的和 假设要查询以坐标 `(x1,y1)` 作为左上角顶点,`(x2,y2)` 作为右下角顶点所围成的小矩形内部数值总和,则可以按照下面的方式进行计算: \[ query(x_1, y_1, x_2, y_2)=sum[x_2][y_2]-sum[x_1-1][y_2]-sum[x_2][y_1-1]+sum[x_1-1][y_1-1]\] 这同样遵循了容斥原理来排除重叠部分的影响。 #### 实际应用案例 考虑这样一个题目:“在一个整数矩阵中找到满足特定条件的最大/最小面积”。这类问题往往需要频繁地对不同尺寸的子矩形做求和运算,而借助于预处理好的前缀和表就可以大大简化这些操作并提高效率。 例如,在某些情况下可能还需要结合其他数据结构如线段树或者二分查找来进行更复杂的优化;而在另一些场景里则可以直接利用简单的四边形不等式性质加速搜索过程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值