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. USACO08FEB Hotel

    题目传送门 线段树维护区间 线段树结构体 struct zzz{ int l,r,mi; //l为以左端点的为起点的最长子串 //r为以右端点为终点的最长子串 //mi是区间内部的最长子串 }tree ...

  2. UpdatePanel中点击按钮Session过期跳转页面相关问题:Sys.WebForms.PageRequestManagerParserErrorException:无法分析从服务器收到的消息

    使用 Response.Write("<script language=javascript>window.location.href='Login.aspx';</scr ...

  3. mysql 安装简介

    Linux: 安装 [root @ localhost ~]# yum install mysql-server 设定为开机自动启动 [root @ localhost ~]# chkconfig m ...

  4. VS2013连接SQL Server 2008 R2测试

    第一步,打开SQL Server 08,这里要说明一下,一定要开启服务,很多时候我们重启电脑以后,SQL Server的保留进程会被类似电脑管家之类的保护程序关闭,于是乎连接了半天的数据库都连不上. ...

  5. message() 信息提示

    //样式部分 .message { position: fixed;top: -100px;width: 400px;left: 50%;margin-left: -200px;z-index: 10 ...

  6. 【思维题 欧拉图】loj#10106. 单词游戏

    巧妙的模型转化 题目描述 来自 ICPC CERC 1999/2000,有改动. 有 NNN 个盘子,每个盘子上写着一个仅由小写字母组成的英文单词.你需要给这些盘子安排一个合适的顺序,使得相邻两个盘子 ...

  7. 使用el-checkbox实现全选,点击失效没有反应

    最近在公司接收到了一个需求,给收藏夹的书籍添加批量.全选删除实现思路:点击全选改变item的checked,改变item的checked,重新便利一下所有item的checked来改变全选的selec ...

  8. destoon 后台入口文件weigouadmin.php解析

    destoon有几个文件不能修改,一修改后台就无法登陆,weigouadmin.php就是其中之一,据官网客服说这个文件是可以修改的,不知为什么即使不修改打开一下保存后后台就不能登陆了.因刚接触dt, ...

  9. HDU:2255-奔小康赚大钱(KM算法模板)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others) Mem ...

  10. 并查集:CDOJ1594-老司机的奇幻漂流 (食物链)

    老司机的奇幻漂流 UESTC - 1594 Problem Description 老司机在救出了女票之后,就和她在全世界旅游,有一天,他们来到了一个神奇的小岛上. 这个小岛上有三种动物,他们互相克制 ...