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 ...
随机推荐
- df du
df命令详细用法 a:显示全部的档案系统和各分割区的磁盘使用情形 i:显示i -nodes的使用量 k:大小用k来表示 (默认值) t:显示某一个档案系统的所有分割区磁盘使用量 x:显示不是某一个档案 ...
- ubuntu 安装cloudera hadoop
参考:http://www.aboutyun.com/thread-8921-1-1.html auto wlan0iface wlan0 inet staticaddress 10.32.37.12 ...
- Socket与TcpClient的区别(转载)
Socket和TcpClient有什么区别 原文:http://wxwinter.spaces.live.com/blog/cns!C36588978AFC344A!322.entry 回答: &qu ...
- QT中实现中文的显示与国际化
1 增加头文件 #include "QTextCodec" 2 在文件中增加如下内容 QTextCodec::setCodecForTr(QTextCodec::codecF ...
- [shell基础]——算术运算
shell只支持整数运算.一般可用let.expr.declare.$[]实现. 更精准的运算建议使用Linux下的bc工具——一款高精度计算语言. 1. let是shell内建的整数运算命令 ## ...
- centos安装redis3为系统服务
源地址:http://my.oschina.net/haoqoo/blog/464247 <span></span>#无wget,请通过命令yum install wget安装 ...
- Daily Scrum6
今天我们小组开会内容分为以下部分: part 1: Anti-spam and anti-abuse module模块总结: part 2: 部分优化代码的展示于交流: part 3:针对用户积分模块 ...
- android ViewPaper高度自适应
tv_btn_web.measure(0, 0);//计算所需的真实宽高 LayoutParams params=vp_btn_menu.getLayoutParams(); params.heigh ...
- 【Permutations】cpp
题目: Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the fo ...
- bzoj 3039 悬线法求最大01子矩阵
首先预处理每个F点左右,下一共有多少个F点,然后 对于每个为0的点(R),从这个点开始,一直到这个点 下面第一个R点,这一区间中的min(左),min(右)更新答案. ps:我估计这道题数据有的格式不 ...