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. C++中有三种创建对象的方法

    #include <iostream> using namespace std; class A { private: int n; public: A(int m):n(m) { } ~ ...

  2. c++ sort用法 学习笔记

    c++ sort排序函数,需要加库#include<algorithm>,语法描述:sort(begin,end,cmp),cmp参数可以没有,如果没有默认非降序排序. 首先是升序排序: ...

  3. 单例模式的几种实现-Java版

    目录 关键点 饿汉式 懒汉式 双检锁 静态内部类单例模式 枚举方式 关键点 私有化构造器 通过静态方法或枚举返回单例类对象 确保单例类对象只有一个,尤其在多线程环境下. 确保每个类被序列化不会重新创建 ...

  4. VUE2.0声明周期钩子:不同阶段不同钩子的开启

  5. MySQL中 IFNULL、NULLIF和ISNULL函数的用法

    mysql 中 ifnull().nullif().isnull()函数的用法讲解: 一.IFNULL(expr1,expr2)用法: 假如expr1不为NULL,则 IFNULL() 的返回值为ex ...

  6. micrium ucprobe使用笔记

    前段时间在学习ucos-iii的时候,用到了micrium ucprobe,发现在调试的时候,很方便,可以直观的看到任务的运行使用情况,全局变量的值变化等,当然详细的可以参考官方文档,也可以参考网上的 ...

  7. ubuntu12.04安装teamviewer

    ubuntu 12.04 64位 下载地址:http://downloadap2.teamviewer.com/download/teamviewer_linux_x64.deb 下载之后,选中,右击 ...

  8. 排序 sort函数

    sort函数见下表: 函数名 功能描述 sort 对给定区间所有元素进行排序 stable_sort 对给定区间所有元素进行稳定排序 partial_sort 对给定区间所有元素部分排序 partia ...

  9. HBase0.94.2-cdh4.2.0需求评估测试报告1.0之五

    根据以上图分析得出以下结论: 1. 在上面的hbase配置下,顺序和随机写记录(每条记录1K),每写入86-87M(大小可配)左右数据生成一个磁盘文件(store file). 2. 在上面的hbas ...

  10. socket中send和recv函数

    Socket一次Recv接受的字节有限制么? 从套接字接收数据. 返回值是表示接收数据的字符串. 一次接收的最大数据量由bufsize指定.它默认为零. 注意为了最好地匹配硬件和网络现实,bufsiz ...