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 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 题意:n*m的矩阵,从(0,0)沿45度角射出一束光,速度为根号2,碰到边界反射,到角上结束。k个点,分别输出光到每个点的时间. 一段一段的模拟。
每个点y-x和y+x为定值,用两个vector存点,v1[b],v2[b],这样在模拟每一段线段的时候就不用遍历整个点的集合。只需要遍历当前k和b下的vector中的点。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<vector>
using namespace std; int n,m,t; struct Node
{
int num;
int x,y; }; Node getPoint(Node st,int k)
{
Node p;
if(k*(-st.x)+st.y!=st.y&&k*(-st.x)+st.y>=&&k*(-st.x)+st.y<=m)
{
p.x=;
p.y=k*(-st.x)+st.y;
return p;
}
if(k*(n-st.x)+st.y!=st.y&&k*(n-st.x)+st.y>=&&k*(n-st.x)+st.y<=m)
{
p.x=n;
p.y=k*(n-st.x)+st.y;
return p;
}
if((-st.y+k*st.x)/k!=st.x&&(-st.y+k*st.x)/k>=&&(-st.y+k*st.x)/k<=n)
{
p.x=(-st.y+k*st.x)/k;
p.y=;
return p;
}
if((m-st.y+k*st.x)/k!=st.x&&(m-st.y+k*st.x)/k>=&&(m-st.y+k*st.x)/k<=n)
{
p.x=(m-st.y+k*st.x)/k;
p.y=m;
return p;
}
} bool isEnd(int x,int y)
{
if(x==n&&y==)
return ;
if(x==n&&y==m)
return ;
if(x==&&y==m)
return ;
return ;
} int getCost(int sx,int sy,int k,int tmpx,int tmpy)
{
int b=sy-k*sx;
if(tmpy!=k*tmpx+b)
return -;
return abs(tmpx-sx);
} long long ans[];
vector<Node> v1[];
vector<Node> v2[];
int main()
{
scanf("%d%d%d",&n,&m,&t);
/*Node a;
a.x=3;
a.y=3;
Node b=getPoint(a,-1);
cout<<b.x<<" "<<b.y<<endl;*/
int all=;
for(int i=; i<t; i++)
{
Node node;
node.num=i;
scanf("%d%d",&node.x,&node.y);
int b1=node.y-node.x+;
int b2=node.y+node.x;
v1[b1].push_back(node);
v2[b2].push_back(node);
all++;
}
//for(int i=0;i<v2[6].size();i++)
// cout<<v2[6][i].num<<" "<<v2[6][i].x<<" "<<v2[6][i].y<<endl;
Node st;
st.x=;
st.y=;
int cnt=;
long long time=;
while(!isEnd(st.x,st.y))
{
int k;
if(cnt%)
k=-;
else
k=;
cnt++;
Node en=getPoint(st,k);
int b=st.y-k*st.x;
if(k==)
{
b+=;
for(int i=; i<v1[b].size(); i++)
{
if(ans[v1[b][i].num]==)
{
int cost=getCost(st.x,st.y,k,v1[b][i].x,v1[b][i].y);
ans[v1[b][i].num]=time+cost;
}
}
}
if(k==-)
{
for(int i=; i<v2[b].size(); i++)
{
if(ans[v2[b][i].num]==)
{
int cost=getCost(st.x,st.y,k,v2[b][i].x,v2[b][i].y);
ans[v2[b][i].num]=time+cost;
}
}
}
time+=abs(en.x-st.x);
st=en;
}
//cout<<"cnt:"<<cnt<<endl;
for(int i=; i<all; i++)
if(ans[i]==)
printf("-1\n");
else
printf("%I64d\n",ans[i]);
return ;
}

codeforces_724C_Ray Tracing的更多相关文章

  1. 如何利用ETW(Event Tracing for Windows)记录日志

    ETW是Event Tracing for Windows的简称,它是Windows提供的原生的事件跟踪日志系统.由于采用内核(Kernel)层面的缓冲和日志记录机制,所以ETW提供了一种非常高效的事 ...

  2. #研发解决方案介绍#Tracing(鹰眼)

    郑昀 最后更新于2014/11/12 关键词:GoogleDapper.分布式跟踪.鹰眼.Tracing.HBase.HDFS. 本文档适用人员:研发   分布式系统为什么需要 Tracing?   ...

  3. OpenCascade Ray Tracing Rendering

    OpenCascade Ray Tracing Rendering eryar@163.com 摘要Abstract:OpenCascade6.7.0中引入了光线跟踪算法的实现.使用光线跟踪算法可实现 ...

  4. (ETW) Event Tracing for Windows 入门 (含pdf下载)

    内容提纲 • ETW 介绍 • ETW 使用 • ETW 监控本机Demo • ETW 监控远程机器的思路 • 底层类库:EventSource 介绍 • 底层类库:TraceEvent 介绍 ETW ...

  5. 使用 Web Tracing Framework 分析富 JS 应用的性能

    来自谷歌的 Web Tracing Framework 包含一组工具和脚本,用于 JavaScript 相关代码的性能分析.它是重 JavaScript 应用程序的理想选择,而 JavaScript ...

  6. NS3 日志(Logging)、命令行参数、Tracing系统概述(转载)

    NS-3日志子系统的提供了各种查看仿真结果的渠道: 一.使用Logging Module 1 [预备知识]日志级别及其对应的宏 NS-3 提供了若干个日志级别来满足不同的 Debug 需求,每一级的日 ...

  7. 开始研究Ray tracing

    几个月前面试时Boss问过我一个问题--"除了scanline渲染方法,你还知道什么其他渲染方式?",我没答出来,至今记忆犹新. 前段时间摆弄Intel VTune时看了它的示例代 ...

  8. [转]分布式系统为什么需要 Tracing?

    分布式系统为什么需要 Tracing?   先介绍一个概念:分布式跟踪,或分布式追踪.   电商平台由数以百计的分布式服务构成,每一个请求路由过来后,会经过多个业务系统并留下足迹,并产生对各种Cach ...

  9. End-to-End Tracing of Ajax/Java Applications Using DTrace

    End-to-End Tracing of Ajax/Java Applications Using DTrace         By Amit Hurvitz, July 2007     Aja ...

随机推荐

  1. Spring MVC 4实现RESTFul WebServices的CRUD实例和使用RestTemplate进行请求(全注解形式配置Web和Filter)

    在这篇文章中,我们将使用Spring4 MVC编写一个CRUD RESTful Web服务,写一个REST客户端RestTemplate来使用这些服务.我们也将利用外部客户端测试的服务. 下面将展示核 ...

  2. 关于SVN版本冲突问题

    版本冲突原因: 假设A.B两个用户都在版本号为100的时候,更新了kingtuns.txt这个文件,A用户在修改完成之后提交kingtuns.txt到服务器,这个时候提交成功,这个时候kingtuns ...

  3. Java中基于HTTP协议网络编程

    java中为我们的网络支持提供了java.net包,能够使我们以编程的方式来訪问Web服务功能,这篇博客,就跟大家分享一下.Java中的网络编程的知识.主要是学习下该java.net包下的API. U ...

  4. 今天開始慢下脚步,開始ios技术知识的查漏补缺。

    从2014.6.30 開始工作算起. 如今已经是第416天了.不止不觉.时间过的真快. 通过对之前工作的总结.发现,你的知识面.会决定你面对问题时的态度.过程和结果. 简单来讲.知识面拓展了,你才干有 ...

  5. poj 3020 Antenna Placement(二分无向图 匈牙利)

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6438   Accepted: 3176 ...

  6. MyBatis 中#{}与${}绑定参数的区别

    MyBatis 中#{}与${}绑定参数的区别: #{}将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号.如:order by #{id},如果传入的值是111,那么解析成sql时的值为o ...

  7. 谈谈c++纯虚函数的意义!

    纯虚函数的存在有什么意义呢?相信大学假设有c++这么课程.在讲到纯虚函数时,必然会讲到纯虚函数是面向接口编程的基础. 如今和大家分享下纯虚函数设计的原由.目的.产生的效果. 现代软件project很庞 ...

  8. timus 1018. Binary Apple Tree

    1018. Binary Apple Tree Time limit: 1.0 secondMemory limit: 64 MB Let's imagine how apple tree looks ...

  9. JavaScript Patterns 2.2 Minimizing Globals

    Access a global variable in a browser environment: myglobal = "hello"; // antipattern cons ...

  10. 【POJ 2689】 Prime Distance

    [题目链接] http://poj.org/problem?id=2689 [算法] 我们知道,一个在区间[l,r]中的合数的最小质因子必然不超过sqrt(r) 那么,先暴力筛出1-50000中的质数 ...