Good Bye 2015 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?
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 r1i, c1i, r2i, c2i (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.
Print q integers, i-th should be equal to the number of ways to put a single domino inside the i-th rectangle.
5 8
....#..#
.#......
##.#....
##..#.##
........
4
1 1 2 3
4 1 4 1
1 2 4 5
2 5 5 8
4
0
10
15
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
53
89
120
23
0
2
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=+;
const ll INF = 1ll<<;
const int inf = ;
const int MOD= ; char mp[N][N];
int p[N][N],V[N][N];
int n,m;
int sss[][] ={,,,};
struct ss{
int x,y;
};
int check(int x,int y) {
if(x<=||y<=||x>n||y>m) return ;
return ;
}
int v[N][N];
void bfs(int x,int y) {
for(int i=;i<=n;i++) {
for(int j=;j<=m;j++) {
if(mp[i][j]!='#')
for(int k=;k<;k++) {
int xx = i+sss[k][];
int yy = j+sss[k][];
if(check(xx,yy)) continue;
if(mp[xx][yy]=='#') continue;
p[i][j]++;
} }
}
}
int main() {
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) {
getchar();
for(int j=;j<=m;j++) {
scanf("%c",&mp[i][j]);
}
}
bfs(,); for(int i=;i<=n;i++) {
for(int j=;j<=m;j++) {
V[i][j] = V[i][j]+V[i-][j]+V[i][j-]-V[i-][j-]+p[i][j];
}
}
int q,x,y,x2,y2;
scanf("%d",&q);
for(int i=;i<=q;i++) {
scanf("%d%d%d%d",&x,&y,&x2,&y2);
int ans = V[x2][y2]-V[x2][y-]-V[x-][y2]+V[x-][y-];
for(int j=x;j<x2;j++) {
if(mp[j][y2]=='.'&&mp[j][y2+]=='.') ans--;
}
ans-=p[x2][y2];
for(int j=y;j<y2;j++) {
if(mp[x2][j]=='.'&&mp[x2+][j]=='.') ans--;
}
printf("%d\n",ans);
}
}
代码
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 stand ...
- Codeforces Good Bye 2015 C. New Year and Domino 前缀和
C. New Year and Domino 题目连接: http://www.codeforces.com/contest/611/problem/C Description They say &q ...
- Good Bye 2015 C - New Year and Domino
题意:计算给定矩形面积(r1,c1),(r2,c2)内长度为2的有多少个?向右或向下计算. 思路:预处理字符.分别向右和向下处理.注意边界情况,可能算多了.用容斥原理计算长度为二的单位. #inclu ...
- TTTTTTTTTTTTT CF Good Bye 2015 C- New Year and Domino(CF611C) 二维前缀
题目 题意:给你一个n*m由.和#组成的矩阵,.代表可以放,#代表不可以,问在左上角(px,py)到(右下角qx,qy)这样的一个矩阵中,放下一个长度为2宽度为1的牌有多少种放法: #include ...
- New Year and Domino:二维前缀和
题目描述: They say "years are like dominoes, tumbling one after the other". But would a year f ...
- Codeforces 611C New Year and Domino(二维前缀和)
题目大概说给一个n*m个格子,格子'.'表示可以放东西,多次询问矩形区域(x1,y1)-(x2,y2)有几种放一张1*2的骨牌的方案数. 分别考虑横着竖着放,预处理出二维的前缀和,即sum[x][y] ...
- CODESOFT 2015中的二维码该怎样生成
由于二维条码具有储存量大.保密性高.追踪性高.抗损性强.备援性大.成本便宜等特性,其应用 渐趋广泛,因此二维码的制作对于CODESOFT条码设计软件的用户来讲可谓司空见惯.我们最常见的二维码要数QR码 ...
- Good Bye 2015 D. New Year and Ancient Prophecy
D. New Year and Ancient Prophecy time limit per test 2.5 seconds memory limit per test 512 megabytes ...
- Good Bye 2015 B. New Year and Old Property 计数问题
B. New Year and Old Property The year 2015 is almost over. Limak is a little polar bear. He has re ...
随机推荐
- UITabelView 高级(自定义Cell)
自定义一个Cell 当我们要显示复杂数据的时候,例如要做一个扣扣聊天界面,或是新闻列表,系统的行已经不能满足我们的要求,这个时候我们可以通过自定义这个行,让他显示更多复杂结构的样式. 自定义cell就 ...
- IOS应用程序升级
IOS应用程序升级流程介绍:IOS手机端应用程序需要升级时,打开服务器端html文件(本文为ucab.html文件)->点击在线安装->打开plist文件(本文中为ucab.plist文件 ...
- 004--VS C++ 绘制封闭图形
//全局变量HPEN hPen;HBRUSH hBru[4];int sBru[4] = { HS_VERTICAL, HS_HORIZONTAL, HS_CROSS, HS_DIAGCROSS }; ...
- 14.quartus联合modelsim仿真
在quartus调用modelsim仿真过程中,出现了一个错误,如下所示: Check the NativeLink log file I:/Quartus11.0/Myproject/testi_n ...
- VS(C++)编程遇到的错误集合
编译错误 1.error C1010: 原因:没有在文件开头添加include "stdafx.h". 2.error C2440: "=": 无法从" ...
- 心情符号love
写点什么呢,先谢谢心情吧,算是第一个脚印了,想先把之前的一些笔记和心得迁移进来吧,以后每个月都要充实自己的知识.向大婶们看齐.走你们走过的脚印,看你们前行的身影.沿着你们留下的路,继续为后者拓宽道路. ...
- openstack与VMware workStation的区别
免责声明: 本文中使用的部分图片来自于网络,如有侵权,请联系博主进行删除 最近一直在研究云计算,恰好有个同事问了我一个问题:你们研究的openstack到底是什么东西?跟VMware Work ...
- Matlab中颜色线形和形状
1.颜色字符串有'c', 'm', 'y', 'r', 'g', 'b', 'w',和'k'.分别表示青,红紫,黄,红,绿,白和黑. 2.线型字符串有:'-' 为实线, '--' 为虚线, ':' 为 ...
- nsight 初级使用指南
1.安装,没有什么特殊设置 2.打开vs,编译生成你需要分析的.exe,在vs上方菜单,有nsight menu, choose Start Graphics Debugging. 3.在弹出对话框中 ...
- string为什么可以写入共享内存
我今天在想这个vector,map为什么不能写入共享内存,原来是因为new的时候只是new了这个对象在共享内存上,而真正的堆上的内存并没有在共享内存里面的,如果要想vector 可以共享就要重写分配器 ...