「CF413E」Maze 2D

2014年4月24日4,4664

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

output

input

output

题解

题意是给一张2*n的地图,有一些X不能走,问一个点到另一个的最短路

我原来以为是dp。。。

后来发现各种细节跪到最后

orz卓神线段树秒杀,维护l-r区间内两行格子,四个角之间的距离

 

 

avatar
1 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
zld3794955hzwer Recent comment authors
  Subscribe  
提醒
zld3794955

Orz。。居然写得这么短。。本弱写的线段树长得跟狗一样T T