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. sqlserver的实例名忘记了

    电脑图标右击/管理/服务和应用程序/服务 也可以直接services.msc打开 打开服务,找到sqlserver的服务,这个服务括号中的名称就是实例名了,但是要加上localhost,也就是loca ...

  2. [vijos]P1979 NOIP2015 信息传递

    描述 有 n 个同学(编号为 1 到 n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为 i 的同学的信息传递对象是编号为 TiTi 的同学. 游戏开始时,每人都只知道 ...

  3. spring boot自动配置实现

    自从用了spring boot,都忘记spring mvc中的xml配置是个什么东西了,再也回不去.为啥spring boot这么好用呢, 约定大于配置的设计初衷, 让我们只知道维护好applicat ...

  4. 201621123080《java程序设计》第14周实验总结

    201621123080<java程序设计>第14周实验总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 2. 使用数据库技术改造你的系统 2. ...

  5. 再生龙备份还原linux系统

    相关下载: Clonezilla再生龙:http://sourceforge.net/projects/clonezilla/files/clonezilla_live_stable/ tuxboot ...

  6. 分享几个能用的 editplus 注册码

    转载自: https://www.cnblogs.com/shihaiming/p/6422441.html 原文:http://host.zzidc.com/wljc/1286.html EditP ...

  7. ubuntu中卸载没有安装完全的软件包

    sudo apt-get autoclean sudo apt-get clean sudo apt-get autoremove

  8. w3resource_MySQL练习:Subquery

    w3resource_MySQL练习题:Subquery 1. Write a query to find the name (first_name, last_name) and the salar ...

  9. python数据类型之元组(tuple)

    元组是python的基础类型之一,是有序的. 元组是不可变的,一旦创建便不能再修改,所以叫只读列表. name = ('alex', 'jack') name[0] = 'mark' # TypeEr ...

  10. jflash合并两个文件

    有时候需要将两个代码块烧写进入单片机的flash,可以使用合并的方法将两个文件合并为一个文件进行烧写,也可以分两次烧写,但要注意不要擦写不相关的存储空间. 打开J-FLASH,新建一个工程,然后fil ...