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 + 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....##.#.51 1 3 31 1 1 31 1 3 11 1 1 21 1 2 1

Output

NoYesYesYesYes

Input

5 5......###.......###......51 1 5 51 1 1 51 1 3 42 1 2 51 1 2 5

Output

YesYesYesNoYes

分治+位运算

确定一条中线,用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 + 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....##.#.51 1 3 31 1 1 31 1 3 11 1 1 21 1 2 1

Output

NoYesYesYesYes

Input

5 5......###.......###......51 1 5 51 1 1 51 1 3 42 1 2 51 1 2 5

Output

YesYesYesNoYes
时间: 2024-10-13 17:24:19

CodeForces 232E.Quick Tortoise的相关文章

CF232E Quick Tortoise , Fzoj 3118

这一题由于数据较多,我们考虑离线处理. 分治.对于两个点s,t,如果起点在mid这条横线上方,终点在下方,那么它必定会穿过mid这条线.所以只要s可以到mid上一点x,x可以到t,st就是安全的. 用bitset维护.设\(f1[i][j]\)为上方ij到mid这条线的是否可以的01值,\(f2[i][j]\)为下方ij到mid的01值.将f1[sx][sy]&f2[tx][ty],如果结果存在1,那么就能走到. Codeforces版本 #include <cstdio> #incl

cf232E. Quick Tortoise(分治 bitset dp)

题意 题目链接 Sol 感觉这个思路还是不错的 #include<bits/stdc++.h> using namespace std; const int MAXN = 501, SS = 5e6 + 10; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '

Educational Codeforces Round 56 (Rated for Div. 2) ABCD

题目链接:https://codeforces.com/contest/1093 A. Dice Rolling 题意: 有一个号数为2-7的骰子,现在有一个人他想扔到几就能扔到几,现在问需要扔多少次,能使扔出的总和等于xi. 题解: 由于是special judge,模拟一下搞搞就行了= = 代码如下: #include <bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; int n; while(t--

Educational Codeforces Round 79 D Santa&#39;s Bot

  被教育场 题意:先等概率选一个人,再从他想要礼物里等概率选一个,再独立于前两次选择,选一个人,他想要的礼物也被选中,则该组合有效,求组合有效的分数概率(模意义下) 玩一下两个样例应该就能出来知道咋算,虽然我第一个样例是跑了两重循环得出 7/8,拼凑起来才勉强理解的题意. 但知道咋算不一定会code啊. 我就是啊. 模拟了分数的加法乘法运算,通分约分,肯定要WA啊,因为到后面分子分母越来越大存不下. 但实际上,两个分数在某个模数意义下相加可以直接转化,即   x/y+u/v  == x*inv

【codeforces 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th

Codeforces Round #286 (Div. 1) A. Mr. Kitayuta, the Treasure Hunter DP

链接: http://codeforces.com/problemset/problem/506/A 题意: 给出30000个岛,有n个宝石分布在上面,第一步到d位置,每次走的距离与上一步的差距不大于1,问走完一路最多捡到多少块宝石. 题解: 容易想到DP,dp[i][j]表示到达 i 处,现在步长为 j 时最多收集到的财富,转移也不难,cnt[i]表示 i 处的财富. dp[i+step-1] = max(dp[i+step-1],dp[i][j]+cnt[i+step+1]) dp[i+st

Codeforces 772A Voltage Keepsake - 二分答案

You have n devices that you want to use simultaneously. The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power store