Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C. Ray Tracing 模拟+预处理、几何

C. Ray Tracing
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are k sensors located in the rectangular room of size n × m meters. The i-th sensor is located at point (xi, yi). 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.

Input

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

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

Output

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.

Examples
input
3 3 4
1 1
1 2
2 1
2 2
output
1
-1
-1
2
input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3
output
1
-1
-1
2
5
-1
input
7 4 5
1 3
2 2
5 1
5 3
4 3
output
13
2
9
5
-1
Note

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).


Source

Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)


My Solution

模拟+预处理、几何

首先每个点只有 2 条 abs(k) == 1的直线, 并且每条线最多经过一次,即每个店做多经过2次, 所以复杂度是O(n)的,模拟是可行的

故先在读入的时候把该点 对应的 2 条  abs(k) == 1的直线 然后分别求出 他们与 矩形边框的2个交点 a(x1, y1),b(x2, y2) cnt[make_pair(a, b)].push_back(x, y); 

即x, y 在 线段(a, b) or (b, a)上

然后初始的时候 x0 = 0, y0 = 0; if(n < m){x = n, y = n;} else{x = m, y = m;}

先对 (x0, y0) -> (x, y) 进行处理, 即遍历 cnt[make_pair(ii(x0, y0), ii(x, y))],把ans[vec[i]]] = 0 的点进行标记为 ans[vec[i]]] = sum + abs(vec[i].first - x0);

然后sum 更新为 sum += abs(x - x0);

像这样依次模拟放射求出一个向量(即始点终点),然后遍历一遍cnt[make_pair(ii(x0, y0), ii(x, y))],并更新 sum

直到 ((x == m || x == 0) && (y == 0 || y == n)) 的时候结束循环

因为每个店最多经过2次, 所以 复杂度  O(n)

 此外这里默认的理解为n行m列所以debug了很久 ⊙﹏⊙‖∣,才发现m行n列


#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> ii;
const LL maxn = 1e6 + 8;

vector<ii> val;
map<ii, LL> ans;
map<pair<ii, ii>, vector<ii> > cnt;
map<pair<ii, ii>, bool> flag;

int main()
{
    #ifdef LOCAL
    freopen("c.txt", "r", stdin);
    //freopen("c.out", "w", stdout);
    LL T = 4;
    while(T--){
    #endif // LOCAL
    ios::sync_with_stdio(false); cin.tie(0);

    LL n, m, k, x, y, x0, y0, xt, yt;
    ii a, b;
    cin >> m >> n >> k;
    for(LL i = 0; i < k; i++){
        cin >> x >> y;
        val.push_back(ii(x, y));
        //
        y0 = m - x + y;
        x0 = n + x - y;
        if(y0 <= n) a = ii(m, y0);
        else a =  ii(x0, n);

        y0 = - x + y;
        x0 = x - y;
        if(y0 >= 0) {b = ii(0, y0);}
        else b = ii(x0, 0);

        /*
        if(x == 1 && y == 3) cout <<"n, m" << n << " " << m << " " <<  a.first << " " << a.second << "->" << b.first << " " << b.second << endl;
        */

        cnt[make_pair(a, b)].push_back(ii(x, y));
        cnt[make_pair(b, a)].push_back(ii(x, y));

        //
        y0 = -m + x + y;
        x0 = x + y;
        if(y0 >= 0) a = ii(m, y0);
        else a = ii(x0, 0);

        y0 = x + y;
        x0 = -n + x + y;
        if(x0 >= 0) b = ii(x0, n);
        else b = ii(0, y0);

        /*
        if(x == 1 && y == 3) cout << a.first << " " << a.second << "->" << b.first << " " << b.second << endl;
        */

        cnt[make_pair(a, b)].push_back(ii(x, y));
        cnt[make_pair(b, a)].push_back(ii(x, y));
    }

    bool ok;
    LL sz, sum = 0;
    //first step
    x0 = 0, y0 = 0;
    if(n < m){x = n, y = n;}
    else{x = m, y = m;}
    vector<ii> &vec = cnt[make_pair(ii(x0, y0), ii(x, y))];
    sz =  vec.size();
    for(LL i = 0; i < sz; i++){
        if(ans[vec[i]]== 0){
            ans[vec[i]] = sum + abs(vec[i].first - x0);
            //cout << sum << " " << ans[vec[i]] << endl;
        }
    }
    sum += abs(x - x0);
    flag[make_pair(ii(x0, y0), ii(x, y))] = true;
    flag[make_pair(ii(x, y), ii(x0, y0))] = true;
    //
    //cout << "here" << endl;

    while(!((x == m || x == 0) && (y == 0 || y == n))){
        //cout << x0 << " " << y0 << " -> " << x << " " << y << endl;
        //cout << sum << endl;
        ok = false;

        yt = m - x + y;
        xt = n + x - y;
        if(yt <= n) a = ii(m, yt);
        else a =  ii(xt, n);

        yt = - x + y;
        xt = x - y;
        if(yt >= 0) {b = ii(0, yt);}
        else b = ii(xt, 0);

        if(a == ii(x, y)){
            if(b != ii(x0, y0)){
                ok = true;
                x0 = x;
                y0 = y;
                x = b.first;
                y = b.second;
            }
        }
        else if(b == ii(x, y)){  //else 不一定是 b == ii(x, y) 的所以要加一个判断
            if(a != ii(x0, y0)){
                ok = true;
                x0 = x;
                y0 = y;
                x = a.first;
                y = a.second;
            }
        }


        if(!ok){
            yt = -m + x + y;
            xt = x + y;
            if(yt >= 0) a = ii(m, yt);
            else a = ii(xt, 0);

            yt = x + y;
            xt = -n + x + y;
            if(xt >= 0) b = ii(xt, n);
            else b = ii(0, yt);

            if(a == ii(x, y)){
                if(b != ii(x0, y0)){
                    ok = true;
                    x0 = x;
                    y0 = y;
                    x = b.first;
                    y = b.second;
                }
            }
            else if(b == ii(x, y)){
                if(a != ii(x0, y0)){
                    ok = true;
                    x0 = x;
                    y0 = y;
                    x = a.first;
                    y = a.second;
                }
            }
        }
        if(ok){
            if(flag[make_pair(ii(x0, y0), ii(x, y))] || flag[make_pair(ii(x, y), ii(x0, y0))]) continue;
            vector<ii> &vec = cnt[make_pair(ii(x0, y0), ii(x, y))];
            sz =  vec.size();
            for(LL i = 0; i < sz; i++){
                if(ans[vec[i]]== 0){
                    ans[vec[i]] = sum + abs(vec[i].first - x0);
                    //cout << sum << " " << ans[vec[i]] << endl;
                }
            }
            flag[make_pair(ii(x0, y0), ii(x, y))] = true;
            flag[make_pair(ii(x, y), ii(x0, y0))] = true;
            //If there is not some legal poLL, we also should count it to the sum;
            //sum += abs(x - x0);
        }
        //
        sum += abs(x - x0);

    }
    //cout << x0 << " " << y0 << " -> " << x << " " << y << endl;

    for(LL i = 0; i < k; i++){
        if(ans[val[i]] != 0) cout << ans[val[i]] << "\n";
        else cout << -1 << "\n";
    }

    #ifdef LOCAL
    val.clear(); ans.clear(); cnt.clear(); flag.clear();
    cout << endl;
    }
    #endif // LOCAL
    return 0;
}

  Thank you!

                                                                                                                                               ------from ProLights 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值