301753: CF331D2. Escaping on Beaveractor

Memory Limit:512 MB Time Limit:6 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Escaping on Beaveractor

题意翻译

小海狸计划逃出学校。 对于一张 $(b + 1) \times (b + 1)$ 的网格图中,左下角为 $(0, 0)$ ,右上角为 $(b, b)$。向上为纵坐标正方向,向右为横坐标正方向。 网格中有 n 个**互不相交**的**平行于网格线**的箭头,每当小海狸碰到箭头将**立即**转向箭头所指方向。一个箭头由起始坐标 $(x_0, y_0)$ 指向终止坐标 $(x_1, y_1)$ 。 为了找出最优逃学路线小海狸将给出 $q$ 次询问, 对于第 $i$ 个询问将给出起始坐标 $(x_i, y_i)$, 起始方向 $w_i \in \{ U,\; D,\; L,\; R\}$ ,分别表示上下左右,以及最多走过的长度 $t_i$ 。 请你给出小海狸是否能逃离学校,如果能,请输出她离开学校前的最后位置,如果不能,请输出她在经过长度为 $t_i$ 的路径后的位置(以上请均以坐标形式输出)。

题目描述

Don't put up with what you're sick of! The Smart Beaver decided to escape from the campus of Beaver Science Academy (BSA). BSA is a $ b×b $ square on a plane. Each point $ x,y $ $ (0<=x,y<=b) $ belongs to BSA. To make the path quick and funny, the Beaver constructed a Beaveractor, an effective and comfortable types of transport. The campus obeys traffic rules: there are $ n $ arrows, parallel to the coordinate axes. The arrows do not intersect and do not touch each other. When the Beaveractor reaches some arrow, it turns in the arrow's direction and moves on until it either reaches the next arrow or gets outside the campus. The Beaveractor covers exactly one unit of space per one unit of time. You can assume that there are no obstacles to the Beaveractor. The BSA scientists want to transport the brand new Beaveractor to the "Academic Tractor" research institute and send the Smart Beaver to do his postgraduate studies and sharpen pencils. They have $ q $ plans, representing the Beaveractor's initial position $ (x_{i},y_{i}) $ , the initial motion vector $ w_{i} $ and the time $ t_{i} $ that have passed after the escape started. Your task is for each of the $ q $ plans to determine the Smart Beaver's position after the given time.

输入输出格式

输入格式


The first line contains two integers: the number of traffic rules $ n $ and the size of the campus $ b $ , $ 0<=n $ , $ 1<=b $ . Next $ n $ lines contain the rules. Each line of the rules contains four space-separated integers $ x_{0} $ , $ y_{0} $ , $ x_{1} $ , $ y_{1} $ — the beginning and the end of the arrow. It is guaranteed that all arrows are parallel to the coordinate axes and have no common points. All arrows are located inside the campus, that is, $ 0<=x_{0},y_{0},x_{1},y_{1}<=b $ holds. Next line contains integer $ q $ — the number of plans the scientists have, $ 1<=q<=10^{5} $ . The $ i $ -th plan is represented by two integers, $ x_{i} $ , $ y_{i} $ are the Beaveractor's coordinates at the initial time, $ 0<=x_{i},y_{i}<=b $ , character $ w_{i} $ , that takes value U, D, L, R and sets the initial direction up, down, to the left or to the right correspondingly (the Y axis is directed upwards), and $ t_{i} $ — the time passed after the escape started, $ 0<=t_{i}<=10^{15} $ . - to get 30 points you need to solve the problem with constraints $ n,b<=30 $ (subproblem D1); - to get 60 points you need to solve the problem with constraints $ n,b<=1000 $ (subproblems D1+D2); - to get 100 points you need to solve the problem with constraints $ n,b<=10^{5} $ (subproblems D1+D2+D3).

输出格式


Print $ q $ lines. Each line should contain two integers — the Beaveractor's coordinates at the final moment of time for each plan. If the Smart Beaver manages to leave the campus in time $ t_{i} $ , print the coordinates of the last point in the campus he visited.

输入输出样例

输入样例 #1

3 3
0 0 0 1
0 2 2 2
3 3 2 3
12
0 0 L 0
0 0 L 1
0 0 L 2
0 0 L 3
0 0 L 4
0 0 L 5
0 0 L 6
2 0 U 2
2 0 U 3
3 0 U 5
1 3 D 2
1 3 R 2

输出样例 #1

0 0
0 1
0 2
1 2
2 2
3 2
3 2
2 2
3 2
1 3
2 2
1 3

Input

题意翻译

小海狸计划逃出学校。 对于一张 $(b + 1) \times (b + 1)$ 的网格图中,左下角为 $(0, 0)$ ,右上角为 $(b, b)$。向上为纵坐标正方向,向右为横坐标正方向。 网格中有 n 个**互不相交**的**平行于网格线**的箭头,每当小海狸碰到箭头将**立即**转向箭头所指方向。一个箭头由起始坐标 $(x_0, y_0)$ 指向终止坐标 $(x_1, y_1)$ 。 为了找出最优逃学路线小海狸将给出 $q$ 次询问, 对于第 $i$ 个询问将给出起始坐标 $(x_i, y_i)$, 起始方向 $w_i \in \{ U,\; D,\; L,\; R\}$ ,分别表示上下左右,以及最多走过的长度 $t_i$ 。 请你给出小海狸是否能逃离学校,如果能,请输出她离开学校前的最后位置,如果不能,请输出她在经过长度为 $t_i$ 的路径后的位置(以上请均以坐标形式输出)。

加入题单

算法标签: