John Doe has a field, which is a rectangular table of size n × m. We assume that the field rows are numbered from 1 to n from top to bottom, and the field columns are numbered from 1 to m from left to right. Then the cell of the field at the intersection of the x-th row and the y-th column has coordinates (xy).

We know that some cells of John's field are painted white, and some are painted black. Also, John has a tortoise, which can move along the white cells of the field. The tortoise can get from a white cell with coordinates (xy) into cell (x + 1; y) or (xy + 1), if the corresponding cell is painted white. In other words, the turtle can move only along the white cells of the field to the right or down. The turtle can not go out of the bounds of the field.

In addition, John has q queries, each of them is characterized by four numbers x1, y1, x2, y2 (x1 ≤ x2y1 ≤ y2). For each query John wants to know whether the tortoise can start from the point with coordinates (x1y1), and reach the point with coordinates (x2y2), moving only along the white squares of the field.

Input

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 500) — the field sizes.

Each of the next n lines contains m characters "#" and ".": the j-th character of the i-th line equals "#", if the cell (ij) is painted black and ".", if it is painted white.

The next line contains integer q (1 ≤ q ≤ 6·105) — the number of queries. Next q lines contain four space-separated integers x1y1x2 and y2 (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m) — the coordinates of the starting and the finishing cells. It is guaranteed that cells (x1y1) and (x2y2) are white.

Output

For each of q queries print on a single line "Yes", if there is a way from cell (x1y1) to cell (x2y2), that meets the requirements, and "No" otherwise. Print the answers to the queries in the order, in which the queries are given in the input.

Example

Input
3 3
...
.##
.#.
5
1 1 3 3
1 1 1 3
1 1 3 1
1 1 1 2
1 1 2 1
Output
No
Yes
Yes
Yes
Yes
Input
5 5
.....
.###.
.....
.###.
.....
5
1 1 5 5
1 1 1 5
1 1 3 4
2 1 2 5
1 1 2 5
Output
Yes
Yes
Yes
No
Yes

分治+位运算

确定一条中线,用bitset标记左边的每个点可以到这条中线上的哪些点,右边的每个点可以从这条中线上的哪些点过来。←如果两个bitset有交集,说明左边的那个点可以到右面的那个点。

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<bitset>
#include<vector>
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*-''+ch;ch=getchar();}
return x*f;
}
int n,m,Q,sv=;
struct query{
int x1,y1,x2,y2;
int id;
}q[mxn*];
char mp[mxn][mxn];
vector<int>qu[mxn][mxn];
bool vis[mxn*];
bool ans[mxn*];
bitset<mxn>L[mxn][mxn],R[mxn][mxn];
void solve(int l,int r){
if(l>r)return;
int i,j;
int mid=(l+r)>>;
for(i=mid;i>=l;i--){
for(j=m;j;j--){//向左上扩展
L[i][j]=;
if(mp[i][j]=='.'){
if(i==mid)L[i][j][j]=;
else L[i][j]|=L[i+][j];
if(j<m)L[i][j]|=L[i][j+];
}
}
}
for(i=mid;i<=r;i++){//向右下扩展
for(j=;j<=m;j++){
R[i][j]=;
if(mp[i][j]=='.'){
if(i==mid)R[i][j][j]=;
else R[i][j]|=R[i-][j];
if(j>)R[i][j]|=R[i][j-];
}
}
}
for(i=mid;i>=l;i--){
for(j=m;j;j--){
for(int k=;k<qu[i][j].size();k++){
int v=qu[i][j][k];
if(vis[q[v].id])continue;
if(q[v].x2>=mid){
sv++;
vis[q[v].id]=;
// printf("sov:%d %d %d %d\n",q[v].x1,q[v].y1,q[v].x2,q[v].y2);
ans[q[v].id]=(L[q[v].x1][q[v].y1]&R[q[v].x2][q[v].y2]).any();
}
}
}
}
if(sv==Q)return;
solve(l,mid-);solve(mid+,r);
return;
}
int main(){
int i,j;
n=read();m=read();
for(i=;i<=n;i++)
scanf("%s",mp[i]+);
Q=read();
for(i=;i<=Q;i++){
q[i].x1=read(); q[i].y1=read();
q[i].x2=read(); q[i].y2=read();
q[i].id=i;
qu[q[i].x1][q[i].y1].push_back(i);
}
solve(,n);
for(i=;i<=Q;i++){
printf("%s\n",ans[i]?"Yes":"No");
}
return ;
}

John Doe has a field, which is a rectangular table of size n × m. We assume that the field rows are numbered from 1 to n from top to bottom, and the field columns are numbered from 1 to m from left to right. Then the cell of the field at the intersection of the x-th row and the y-th column has coordinates (xy).

We know that some cells of John's field are painted white, and some are painted black. Also, John has a tortoise, which can move along the white cells of the field. The tortoise can get from a white cell with coordinates (xy) into cell (x + 1; y) or (xy + 1), if the corresponding cell is painted white. In other words, the turtle can move only along the white cells of the field to the right or down. The turtle can not go out of the bounds of the field.

In addition, John has q queries, each of them is characterized by four numbers x1, y1, x2, y2 (x1 ≤ x2y1 ≤ y2). For each query John wants to know whether the tortoise can start from the point with coordinates (x1y1), and reach the point with coordinates (x2y2), moving only along the white squares of the field.

Input

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 500) — the field sizes.

Each of the next n lines contains m characters "#" and ".": the j-th character of the i-th line equals "#", if the cell (ij) is painted black and ".", if it is painted white.

The next line contains integer q (1 ≤ q ≤ 6·105) — the number of queries. Next q lines contain four space-separated integers x1y1x2 and y2 (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m) — the coordinates of the starting and the finishing cells. It is guaranteed that cells (x1y1) and (x2y2) are white.

Output

For each of q queries print on a single line "Yes", if there is a way from cell (x1y1) to cell (x2y2), that meets the requirements, and "No" otherwise. Print the answers to the queries in the order, in which the queries are given in the input.

Example

Input
3 3
...
.##
.#.
5
1 1 3 3
1 1 1 3
1 1 3 1
1 1 1 2
1 1 2 1
Output
No
Yes
Yes
Yes
Yes
Input
5 5
.....
.###.
.....
.###.
.....
5
1 1 5 5
1 1 1 5
1 1 3 4
2 1 2 5
1 1 2 5
Output
Yes
Yes
Yes
No
Yes

CodeForces 232E.Quick Tortoise的更多相关文章

  1. Codeforces 232E - Quick Tortoise bitset+分治

    题意: 思路: //By SiriusRen #include <cstdio> #include <bitset> #include <vector> using ...

  2. Solution -「CF 232E」Quick Tortoise

    \(\mathcal{Description}\)   Link.   在一张 \(n\times m\) 的网格图中有空格 . 和障碍格 #,\(q\) 次询问,每次查询从 \((x_1,y_1)\ ...

  3. CF232E Quick Tortoise , Fzoj 3118

    这一题由于数据较多,我们考虑离线处理. 分治.对于两个点s,t,如果起点在mid这条横线上方,终点在下方,那么它必定会穿过mid这条线.所以只要s可以到mid上一点x,x可以到t,st就是安全的. 用 ...

  4. cf232E. Quick Tortoise(分治 bitset dp)

    题意 题目链接 Sol 感觉这个思路还是不错的 #include<bits/stdc++.h> using namespace std; const int MAXN = 501, SS ...

  5. [CF232E]Quick Tortoise

    题目大意: 给你一个$n\times m(n,m\leq 500)$的格子,有一些是障碍物.从一个格子出发只能向下或向右走,有$q$组询问,每次询问从一个点是否能够到达另一个点. 思路: 分治. 两点 ...

  6. codeforces magic five --快速幂模

    题目链接:http://codeforces.com/contest/327/problem/C 首先先算出一个周期里面的值,保存在ans里面,就是平常的快速幂模m做法. 然后要计算一个公式,比如有k ...

  7. Codeforces Round #324 (Div. 2)解题报告

    ---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...

  8. Educational Codeforces Round 38

    http://codeforces.com/contest/938 A:sb题 //#pragma comment(linker, "/stack:200000000") //#p ...

  9. Educational Codeforces Round 13

    http://codeforces.com/contest/678 A:水题 #include<bits/stdc++.h> #define fi first #define se sec ...

随机推荐

  1. python 产生随机数

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  2. C#数组添加元素

    一.向数组添加元素 在C#中,只能在动态数组ArrayList类中向数组添加元素.因为动态数组是一个可以改变数组长度和元素个数的数据类型. 示例: using System;using System. ...

  3. webgis技术在智慧城市综合治理网格化社会管理平台(综治平台)的应用

      网格化社会管理平台功能:1 实有人口管理人口数据管理按照人口分类进行管理,分为常住人口.流动人口.特殊人群.弱势群体,功能包括人口信息管理.归口负责.人房关联.统计汇总.地图监管服务等功能.人口信 ...

  4. java第八次作业:课堂上发布的前5张图片(包括匿名对象、单例模式恶汉式、自动生成对象、args[]数组使用、静态关键字)

  5. IOS中将颜色转换为image

    - (UIImage *)createImageWithColor:(UIColor *)color { CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f ...

  6. Java微信公众号开发----定时获取access_token并保存到redis中

    本人原本是想做微信公众号菜单的创建修改删除等操作的,但是发现需要access_token,通过阅读文档,发现文档要求有以下几点: 1.access_token 获取后有效期是2小时 2.access_ ...

  7. [转]LLE

    原始特征的数量可能很大,或者说样本是处于一个高维空间中,通过映射或变换的方法,降高维数据降低到低维空间中的数据,这个过程叫特征提取,也称降维. 特征提取得基本任务研究从众多特征中求出那些对分类最有效的 ...

  8. 学习笔记(二):使用 TensorFlow 的起始步骤(First Steps with TensorFlow)

    目录 1.工具包 TensorFlow 张量 (Tensor) 图 (graph) TensorBoard 2.tf.estimator API Estimator 预创建的 Estimator (p ...

  9. 201621123080 《Java程序设计》第13周学习总结

    201621123080 <Java程序设计>第13周学习总结 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能( ...

  10. Linux-Java安装

    安装 yum -y install java-1.8.0-openjdk* Tomcat安装:到官网http://tomcat.apache.org/,然后找到Tomcat9下载地址 http://m ...