CF724C.Ray Tracing

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are k sensors located in the rectangular room of size n × m meters. The i-th sensor is located at point (x__i, y__i). All sensors are located at distinct points strictly inside the rectangle.

Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.

At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.

When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.

For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print  - 1 for such sensors.

有 kk 个传感器位于一个大小为 n×mn \times m 米的矩形房间内。第 ii 个传感器位于点 (xi,yi)(x_i, y_i)。所有传感器均位于矩形内部(即不包含边界)且互不重合的点上。

房间的两个对角顶点分别位于点 (0,0)(0, 0) 和 (n,m)(n, m),房间的墙壁与坐标轴平行。

在时刻 00,一束激光从点 (0,0)(0, 0) 发出,方向指向点 (1,1)(1, 1)。该激光以 2\sqrt{2} 米/秒的速度传播,因此恰好在起始后 11 秒到达点 (1,1)(1, 1)。

当激光碰到墙壁时,按“入射角等于反射角”的规则发生反射。若激光到达四个顶点中的任意一个,则立即停止。

对每个传感器,你需要确定激光首次经过该传感器所在位置的时刻。若激光永远不会经过该点,则对该传感器输出 −1-1。

输入格式

The first line of the input contains three integers n, m and k (2 ≤ n, m ≤ 100 000, 1 ≤ k ≤ 100 000) — lengths of the room's walls and the number of sensors.

Each of the following k lines contains two integers x__i and y__i (1 ≤ x__i ≤ n - 1, 1 ≤ y__i ≤ m - 1) — coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.

输入的第一行包含三个整数 nn、mm 和 kk(2 ≤ n, m ≤ 100 0002 \leq n, m \leq 100\,000,1 ≤ k ≤ 100 0001 \leq k \leq 100\,000)——分别表示房间墙壁的长度以及传感器的数量。

接下来的 kk 行中,每行包含两个整数 xix_i 和 yiy_i(1 ≤ xi ≤ n − 11 \leq x_i \leq n - 1,1 ≤ yi ≤ m − 11 \leq y_i \leq m - 1)——表示第 ii 个传感器的坐标。保证任意两个传感器不会位于同一点。

输出格式

Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or  - 1 if this will never happen.

输出 k 个整数。其中第 i 个整数应等于光线首次经过第 i 个传感器所在位置的时刻(单位:秒);若该情况永远不会发生,则输出  - 1。

输入输出样例

  • 输入#1

    3 3 4
    1 1
    1 2
    2 1
    2 2

    输出#1

    1
    -1
    -1
    2
  • 输入#2

    3 4 6
    1 1
    2 1
    1 2
    2 2
    1 3
    2 3

    输出#2

    1
    -1
    -1
    2
    5
    -1
  • 输入#3

    7 4 5
    1 3
    2 2
    5 1
    5 3
    4 3

    输出#3

    13
    2
    9
    5
    -1

说明/提示

In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds.

In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).

在第一个样例中,光线将依次经过点 (0, 0)(0,\,0)、(1, 1)(1,\,1)、(2, 2)(2,\,2)、(3, 3)(3,\,3)。因此,它将在 33 秒后于点 (3, 3)(3,\,3) 处停止。

在第二个样例中,光线将依次经过以下各点:(0, 0)(0,\,0)、(1, 1)(1,\,1)、(2, 2)(2,\,2)、(3, 3)(3,\,3)、(2, 4)(2,\,4)、(1, 3)(1,\,3)、(0, 2)(0,\,2)、(1, 1)(1,\,1)、(2, 0)(2,\,0)、(3, 1)(3,\,1)、(2, 2)(2,\,2)、(1, 3)(1,\,3)、(0, 4)(0,\,4)。光线将在 1212 秒后于点 (0, 4)(0,\,4) 处停止。它将在点 (3, 3)(3,\,3)、(2, 4)(2,\,4)、(0, 2)(0,\,2)、(2, 0)(2,\,0) 和 (3, 1)(3,\,1) 处发生反射。

输入解题思路,AI测评打分。不知道怎么写?

首页