E. Maze 2D
time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output

The last product of the R2 company in the 2D games' field is a new revolutionary algorithm of searching for the shortest path in a 2 × nmaze.

Imagine a maze that looks like a 2 × n rectangle, divided into unit squares. Each unit square is either an empty cell or an obstacle. In one unit of time, a person can move from an empty cell of the maze to any side-adjacent empty cell. The shortest path problem is formulated as follows. Given two free maze cells, you need to determine the minimum time required to go from one cell to the other.

Unfortunately, the developed algorithm works well for only one request for finding the shortest path, in practice such requests occur quite often. You, as the chief R2 programmer, are commissioned to optimize the algorithm to find the shortest path. Write a program that will effectively respond to multiple requests to find the shortest path in a 2 × n maze.

Input

The first line contains two integers, n and m (1 ≤ n ≤ 2·105; 1 ≤ m ≤ 2·105) — the width of the maze and the number of queries, correspondingly. Next two lines contain the maze. Each line contains n characters, each character equals either '.' (empty cell), or 'X' (obstacle).

Each of the next m lines contains two integers vi and ui (1 ≤ vi, ui ≤ 2n) — the description of the i-th request. Numbers viui mean that you need to print the value of the shortest path from the cell of the maze number vi to the cell number ui. We assume that the cells of the first line of the maze are numbered from 1 to n, from left to right, and the cells of the second line are numbered from n + 1 to 2n from left to right. It is guaranteed that both given cells are empty.

Output

Print m lines. In the i-th line print the answer to the i-th request — either the size of the shortest path or -1, if we can't reach the second cell from the first one.

Sample test(s)
input
4 7
.X..
...X
5 1
1 3
7 7
1 4
6 1
4 7
5 7
output
1
4
0
5
2
2
2
input
10 3
X...X..X..
..X...X..X
11 7
7 18
18 10
output
9
-1
3

这……线段树神题啊

但是这是道馆之战的弱化版……

道馆之战是树上的情况,这题只是一条链的情况

用线段树维护每一个1*2的格子从第一格能不能到第一格、从第一格能不能到第二格、从第二格能不能到第一格、从第二格能不能到第二格

#include<cstdio>
#include<iostream>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct segtree{
int l,r;
int a_to_a,a_to_b,b_to_a,b_to_b;
}tree[1000010];
segtree query;
bool mrk[2][200010];
int n,m,x0,y0,x1,y1;
segtree merge(segtree a,segtree b)
{
segtree k;
k.a_to_a=k.a_to_b=k.b_to_a=k.b_to_b=-1;
k.l=min(a.l,b.l); k.r=max(a.r,b.r); if (a.a_to_a!=-1&&b.a_to_a!=-1)k.a_to_a=a.a_to_a+b.a_to_a+1;
if (a.a_to_b!=-1&&b.b_to_a!=-1)
{
if (k.a_to_a==-1)k.a_to_a=a.a_to_b+b.b_to_a+1;
else k.a_to_a=min(k.a_to_a,a.a_to_b+b.b_to_a+1);
} if (a.a_to_a!=-1&&b.a_to_b!=-1)k.a_to_b=a.a_to_a+b.a_to_b+1;
if (a.a_to_b!=-1&&b.b_to_b!=-1)
{
if (k.a_to_b==-1)k.a_to_b=a.a_to_b+b.b_to_b+1;
else k.a_to_b=min(k.a_to_b,a.a_to_b+b.b_to_b+1);
} if (a.b_to_a!=-1&&b.a_to_a!=-1)k.b_to_a=a.b_to_a+b.a_to_a+1;
if (a.b_to_b!=-1&&b.b_to_a!=-1)
{
if (k.b_to_a==-1)k.b_to_a=a.b_to_b+b.b_to_a+1;
else k.b_to_a=min(k.b_to_a,a.b_to_b+b.b_to_a+1);
} if (a.b_to_a!=-1&&b.a_to_b!=-1)k.b_to_b=a.b_to_a+b.a_to_b+1;
if (a.b_to_b!=-1&&b.b_to_b!=-1)
{
if (k.b_to_b==-1)k.b_to_b=a.b_to_b+b.b_to_b+1;
else k.b_to_b=min(k.b_to_b,a.b_to_b+b.b_to_b+1);
}
return k;
}
inline void buildtree(int now,int l,int r)
{
tree[now].l=l;tree[now].r=r;
if (l==r)
{
tree[now].a_to_a=tree[now].a_to_b=tree[now].b_to_a=tree[now].b_to_b=-1;
if (mrk[0][l])tree[now].a_to_a=0;
if (mrk[1][l])tree[now].b_to_b=0;
if (mrk[0][l]&&mrk[1][l])
{
tree[now].a_to_b=1;
tree[now].b_to_a=1;
}
return;
}
int mid=(l+r)>>1;
buildtree(now<<1,l,mid);
buildtree(now<<1|1,mid+1,r);
tree[now]=merge(tree[now<<1],tree[now<<1|1]);
}
inline void work(int now,int x,int y)
{
int l=tree[now].l,r=tree[now].r;
if (l==x&&r==y)
{
if (!query.l)query=tree[now];
else query=merge(query,tree[now]);
return;
}
int mid=(l+r)>>1;
if (y<=mid)work(now<<1,x,y);
else if (x>mid)work(now<<1|1,x,y);
else
{
work(now<<1,x,mid);
work(now<<1|1,mid+1,y);
}
}
inline int ask(int x0,int y0,int x1,int y1)
{
query.l=query.r=0;
work(1,y0,y1);
if (!x0&&!x1)return query.a_to_a;
if (!x0&&x1)return query.a_to_b;
if (x0&&!x1)return query.b_to_a;
if (x0&&x1)return query.b_to_b;
}
int main()
{
n=read();m=read();
for(int i=0;i<=1;i++)
for(int j=1;j<=n;j++)
{
char ch=getchar();while (ch!='X'&&ch!='.')ch=getchar();
if (ch=='.')mrk[i][j]=1;
}
buildtree(1,1,n);
for(int i=1;i<=m;i++)
{
y0=read();y1=read();x0=x1=0;
if ((y0-1)%n+1>(y1-1)%n+1)swap(y0,y1);
if (y0>n){x0=1;y0-=n;}
if (y1>n){x1=1;y1-=n;}
printf("%d\n",ask(x0,y0,x1,y1));
}
}

  

cf413E Maze 2D的更多相关文章

  1. test20181005 迷宫

    题意 分析 时间复杂度里的n,m写反了. 出题人很有举一反三的精神. 代码 我的代码常数巨大,加了各种优化后开O3最慢点都要0.9s. #include<cstdlib> #include ...

  2. CF数据结构练习

    1. CF 438D The Child and Sequence 大意: n元素序列, m个操作: 1,询问区间和. 2,区间对m取模. 3,单点修改 维护最大值, 取模时暴力对所有>m的数取 ...

  3. DFS经典题,reachable or not in a 2D maze

    [[0, 0, 0, 0, 0, 1], [1, 1, 0, 0, 0, 1], [0, 0, 0, 1, 0, 0], [0, 1, 1, 0, 0, 1], [0, 1, 0, 0, 1, 0], ...

  4. LightOJ 1337 F - The Crystal Maze (bfs)

    Description You are in a plane and you are about to be dropped with a parasuit in a crystal maze. As ...

  5. [LeetCode] The Maze III 迷宫之三

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  6. [LeetCode] The Maze II 迷宫之二

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  7. [LeetCode] The Maze 迷宫

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  8. Leetcode: The Maze III(Unsolved Lock Problem)

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  9. Leetcode: The Maze II

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

随机推荐

  1. [每日一题] 11gOCP 1z0-053 :2013-09-29 Flashback Data Archive ...................................6

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/12205299 正确答案:A 具体请参考:http://blog.csdn.net/guoy ...

  2. Linux安装mysql-python库时报错解决办法

    用pip安装mysql-python库的时候遇到如下报错 root@LoidAir:~# pip install mysql-python Collecting mysql-python Using ...

  3. (转)linux bash shell 入门教程

    Shell Script(bash)简介 众所皆知地,UNIX上以小工具著名,利用许多简单的小工具,来完成原本需要大量软体开发的工作,这一点特色,使得UNIX成为许多人心目中理想的系统平台. 在众多的 ...

  4. 【自由谈】城域网IPv6过渡技术——4v6场景技术总结(1)

    为什么会存在4v6应用场景?主要是从“云-管-端”的IPv6状态决定的,“云”侧IPv4类业务丰富,IPv6驱动力小,所以“云”在较长一段时间内还是以IPv4类业务为主.“管”侧的IPv6化程度高,设 ...

  5. IOS设计模式学习(18)模板方法

    1 前言 模板方法模式是面向对象软件设计中一种非常简单的设计模式.其基本思想是在抽象类的一个方法定义“标准”算法.在这个方法中调用的基本操作由子类重载予以实现.这个方法成为“模板”.因为方法定义的算法 ...

  6. VC用OLE方式读写Excel

    前几天要做一个项目,需要读取Excel中的数据.从网上查资料发现,主要是有两种方式.一是把Excel表当成数据库使用ODBC读写,这样操作起来就跟操作Access数据库似的.但这种方式效率比较低.另一 ...

  7. 让你的java开发变得如此 Smart

    http://my.oschina.net/huangyong/blog/196408

  8. FoxOne---一个快速高效的BS框架--(2)

    FoxOne---一个快速高效的BS框架--(1) FoxOne---一个快速高效的BS框架--(2) FoxOne---一个快速高效的BS框架--(3) FoxOne---一个快速高效的BS框架-- ...

  9. (第三章)Java内存模型(中)

    一.volatile的内存语义 1.1 volatile的特性 理解volatile特性的一个好办法是把对volatile变量的单个读/写,看成是使用同一个锁对这些单个读/写操作做了同步.下面通过具体 ...

  10. 配置NFS服务

    1. NFS配置,需要安装哪些包?nfs-utils  和 rpcbind2. 如果不开启rpcbind服务,就启动NFS,会怎么样?如果不开启rpcbind服务,会报错:rpc.nfsd: writ ...