首页 > 代码库 > CodeForces 232E.Quick Tortoise

CodeForces 232E.Quick Tortoise

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 + 1y) 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 ≤ n1 ≤ 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有交集,说明左边的那个点可以到右面的那个点。

 

 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<queue> 6 #include<bitset> 7 #include<vector> 8 using namespace std; 9 const int mxn=610;10 int read(){11     int x=0,f=1;char ch=getchar();12     while(ch<0 || ch>9){if(ch==-)f=-1;ch=getchar();}13     while(ch>=0 && ch<=9){x=x*10-0+ch;ch=getchar();}14     return x*f;15 }16 int n,m,Q,sv=0;17 struct query{18     int x1,y1,x2,y2;19     int id;20 }q[mxn*1000];21 char mp[mxn][mxn];22 vector<int>qu[mxn][mxn];23 bool vis[mxn*1000];24 bool ans[mxn*1000];25 bitset<mxn>L[mxn][mxn],R[mxn][mxn];26 void solve(int l,int r){27     if(l>r)return;28     int i,j;29     int mid=(l+r)>>1;30     for(i=mid;i>=l;i--){31         for(j=m;j;j--){//向左上扩展 32             L[i][j]=0;33             if(mp[i][j]==.){34                 if(i==mid)L[i][j][j]=1;35                 else L[i][j]|=L[i+1][j];36                 if(j<m)L[i][j]|=L[i][j+1];37             }38         }39     }40     for(i=mid;i<=r;i++){//向右下扩展 41         for(j=1;j<=m;j++){42             R[i][j]=0;43             if(mp[i][j]==.){44                 if(i==mid)R[i][j][j]=1;45                 else R[i][j]|=R[i-1][j];46                 if(j>1)R[i][j]|=R[i][j-1];47             }48         }49     }50     for(i=mid;i>=l;i--){51         for(j=m;j;j--){52             for(int k=0;k<qu[i][j].size();k++){53                 int v=qu[i][j][k];54                 if(vis[q[v].id])continue;55                 if(q[v].x2>=mid){56                     sv++;57                     vis[q[v].id]=1;58 //                    printf("sov:%d %d %d %d\n",q[v].x1,q[v].y1,q[v].x2,q[v].y2);59                     ans[q[v].id]=(L[q[v].x1][q[v].y1]&R[q[v].x2][q[v].y2]).any();60                 }61             }62         }63     }64     if(sv==Q)return;65     solve(l,mid-1);solve(mid+1,r);66     return;67 }68 int main(){69     int i,j;70     n=read();m=read();71     for(i=1;i<=n;i++)72         scanf("%s",mp[i]+1);73     Q=read();74     for(i=1;i<=Q;i++){75         q[i].x1=read();    q[i].y1=read();76         q[i].x2=read();    q[i].y2=read();77         q[i].id=i;78         qu[q[i].x1][q[i].y1].push_back(i);79     }80     solve(1,n);81     for(i=1;i<=Q;i++){82         printf("%s\n",ans[i]?"Yes":"No");83     }84     return 0;85 }

 

 

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 + 1y) 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 ≤ n1 ≤ 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