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 (x; y).
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 (x; y) into cell (x + 1; y) or (x; y + 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 ≤ x2, y1 ≤ y2). For each query John wants to know whether the tortoise can start from the point with coordinates (x1; y1), and reach the point with coordinates (x2; y2), 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 (i; j) 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 x1, y1, x2 and y2 (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m) — the coordinates of the starting and the finishing cells. It is guaranteed that cells (x1; y1) and (x2; y2) are white.
Output
For each of q queries print on a single line "Yes", if there is a way from cell (x1; y1) to cell (x2; y2), 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
3 3
...
.##
.#.
5
1 1 3 3
1 1 1 3
1 1 3 1
1 1 1 2
1 1 2 1
No
Yes
Yes
Yes
Yes
5 5
.....
.###.
.....
.###.
.....
5
1 1 5 5
1 1 1 5
1 1 3 4
2 1 2 5
1 1 2 5
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 (x; y).
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 (x; y) into cell (x + 1; y) or (x; y + 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 ≤ x2, y1 ≤ y2). For each query John wants to know whether the tortoise can start from the point with coordinates (x1; y1), and reach the point with coordinates (x2; y2), 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 (i; j) 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 x1, y1, x2 and y2 (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ m) — the coordinates of the starting and the finishing cells. It is guaranteed that cells (x1; y1) and (x2; y2) are white.
Output
For each of q queries print on a single line "Yes", if there is a way from cell (x1; y1) to cell (x2; y2), 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
3 3
...
.##
.#.
5
1 1 3 3
1 1 1 3
1 1 3 1
1 1 1 2
1 1 2 1
No
Yes
Yes
Yes
Yes
5 5
.....
.###.
.....
.###.
.....
5
1 1 5 5
1 1 1 5
1 1 3 4
2 1 2 5
1 1 2 5
Yes
Yes
Yes
No
Yes
CodeForces 232E.Quick Tortoise的更多相关文章
- Codeforces 232E - Quick Tortoise bitset+分治
题意: 思路: //By SiriusRen #include <cstdio> #include <bitset> #include <vector> using ...
- Solution -「CF 232E」Quick Tortoise
\(\mathcal{Description}\) Link. 在一张 \(n\times m\) 的网格图中有空格 . 和障碍格 #,\(q\) 次询问,每次查询从 \((x_1,y_1)\ ...
- CF232E Quick Tortoise , Fzoj 3118
这一题由于数据较多,我们考虑离线处理. 分治.对于两个点s,t,如果起点在mid这条横线上方,终点在下方,那么它必定会穿过mid这条线.所以只要s可以到mid上一点x,x可以到t,st就是安全的. 用 ...
- cf232E. Quick Tortoise(分治 bitset dp)
题意 题目链接 Sol 感觉这个思路还是不错的 #include<bits/stdc++.h> using namespace std; const int MAXN = 501, SS ...
- [CF232E]Quick Tortoise
题目大意: 给你一个$n\times m(n,m\leq 500)$的格子,有一些是障碍物.从一个格子出发只能向下或向右走,有$q$组询问,每次询问从一个点是否能够到达另一个点. 思路: 分治. 两点 ...
- codeforces magic five --快速幂模
题目链接:http://codeforces.com/contest/327/problem/C 首先先算出一个周期里面的值,保存在ans里面,就是平常的快速幂模m做法. 然后要计算一个公式,比如有k ...
- Codeforces Round #324 (Div. 2)解题报告
---恢复内容开始--- Codeforces Round #324 (Div. 2) Problem A 题目大意:给二个数n.t,求一个n位数能够被t整除,存在多组解时输出任意一组,不存在时输出“ ...
- Educational Codeforces Round 38
http://codeforces.com/contest/938 A:sb题 //#pragma comment(linker, "/stack:200000000") //#p ...
- Educational Codeforces Round 13
http://codeforces.com/contest/678 A:水题 #include<bits/stdc++.h> #define fi first #define se sec ...
随机推荐
- Dapper学习总结
看了<Dapper从入门到精通>后的总结 (1)Dapper 是直接扩展 IDBConnection,而且是单独一个文件,可以直接嵌入到项目中使用. (2)通过手写sql语句,调用exec ...
- 自行实现一个简易RPC框架
10分钟写一个RPC框架 1.RpcFramework package com.alibaba.study.rpc.framework; import java.io.ObjectInputStrea ...
- Java中的==和equals的区别详解
1.基础知识 (1)String x = "hello"; (2)String x = new String ("hello"); 第1种方式的工作机制是,首先 ...
- [提供可行性脚本] RHEL 7/CentOS 7/Fedora28 重命名网卡名称
实验说明: 在许多自动化任务中,脚本往往是通过读取配置文件来获取信息的,红帽系的系统自升级之后(CentOS7/RHEL7),网卡命名采用“一致性网络设备接口”的命名方法,导致不同设备的不同网卡名称各 ...
- 【laravel】Laravel 5 TokenMismatchException on PHP 5.6.9
When I realized this was only happening in IE and Chrome, but not Firefox, it led me to the fix. The ...
- 用Python实现小说中的汉字频率统计
环境: Python 3的代码,亲测可用. 思路: 是先把每个字符提出来放在列表里:再过滤掉其中的标点符号:最后用字典对某个字出现的频率进行累加. 扩展: 用处很多,稍微改改,既可以用来统计小说或文 ...
- 嵌入式入门学习笔记3:[转]编译linux
摘自:https://blog.csdn.net/baidu_24256693/article/details/80115354 编译Linux是什么意思? Linux内核是Linux操作系统的核心, ...
- ProC第一弹
编译pro*c 的makefile例子 原来只需在makefile中追加include $(ORACLE_HOME)/precomp/lib/env_precomp.mk,其他一切按照makefile ...
- hdu 4565
Problem Description A sequence Sn is defined as:Where a, b, n, m are positive integers.┌x┐is the cei ...
- Prolog&Epilog
这篇博客会简单介绍一下Prolog&Epilog 然后再简单介绍下我对于程序在计算机中到底如何运行的一些理解(因为自己之前也从来没有接触过这些方面的知识,所以如果有讲的不对的地方希望大家能够帮 ...