Ray Tracing

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 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 xi and yi (1 ≤ xi ≤ n - 1, 1 ≤ 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) after3 seconds.

分析:暴力模拟轨迹,然后枚举答案时看射线可能从哪边过来即可;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<ll,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
const int maxn=1e5+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
inline ll read()
{
    ll x=0;int 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;
}
int n,m,k,t,from[4][maxn];
ll vis[4][maxn],ans;
void dfs(int fr,int dir,int pos,ll t)
{
    if(vis[dir][pos]!=-1)return;
    vis[dir][pos]=t;
    from[dir][pos]=fr;
    if(dir==0)
    {
        if(fr==0&&n-pos<m)dfs(0,1,n-pos,t+n-pos);
        else if(fr==0&&pos+m<n)dfs(0,2,pos+m,t+m);
        else if(fr==1&&pos<m)dfs(0,3,pos,t+pos);
        else if(fr==1&&pos-m>0)dfs(1,2,pos-m,t+m);
    }
    else if(dir==1)
    {
        if(fr==0&&n-m+pos>0)dfs(1,2,n-m+pos,t+m-pos);
        else if(fr==0&&pos+n<m)dfs(0,3,pos+n,t+n);
        else if(fr==1&&n-pos>0)dfs(1,0,n-pos,t+pos);
        else if(fr==1&&pos-n>0)dfs(1,3,pos-n,t+n);
    }
    else if(dir==2)
    {
        if(fr==1&&m-pos>0)dfs(1,3,m-pos,t+pos);
        else if(fr==1&&pos-m>0)dfs(1,0,pos-m,t+m);
        else if(fr==0&&m-n+pos>0)dfs(1,1,m-n+pos,t+n-pos);
        else if(fr==0&&pos+m<n)dfs(0,0,pos+m,t+m);
    }
    else if(dir==3)
    {
        if(fr==1&&pos<n)dfs(0,0,pos,t+pos);
        else if(fr==1&&pos-n>0)dfs(1,1,pos-n,t+n);
        else if(fr==0&&m-pos<n)dfs(0,2,m-pos,t+m-pos);
        else if(fr==0&&pos+n<m)dfs(0,1,pos+n,t+n);
    }
}
int main()
{
    int i,j;
    scanf("%d%d%d",&n,&m,&k);
    memset(vis,-1,sizeof(vis));
    dfs(0,0,0,0LL);
    while(k--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        ans=1e18;
        if(a-b>=0&&vis[0][a-b]!=-1&&from[0][a-b]==0)ans=min(ans,vis[0][a-b]+b);
        if(m+a-b<n&&vis[2][m+a-b]!=-1&&from[2][m+a-b]==1)ans=min(ans,vis[2][m+a-b]+m-b);
        if(b-a>0&&vis[3][b-a]!=-1&&from[3][b-a]==0)ans=min(ans,vis[3][b-a]+a);
        if(n+b-a<m&&vis[1][n+b-a]!=-1&&from[1][n+b-a]==1)ans=min(ans,vis[1][n+b-a]+n-a);

        if(a+b<n&&vis[0][a+b]!=-1&&from[0][a+b]==1)ans=min(ans,vis[0][a+b]+b);
        if(a+b-m>0&&vis[2][a+b-m]!=-1&&from[2][a+b-m]==0)ans=min(ans,vis[2][a+b-m]+m-b);
        if(a+b<m&&vis[3][a+b]!=-1&&from[3][a+b]==1)ans=min(ans,vis[3][a+b]+a);
        if(a+b-n>0&&vis[1][a+b-n]!=-1&&from[1][a+b-n]==0)ans=min(ans,vis[1][a+b-n]+n-a);
        printf("%lld
",ans==1e18?-1:ans);
    }
    //system("Pause");
    return 0;
}
原文地址:https://www.cnblogs.com/dyzll/p/5941631.html