B. Vasya and Cornfield(很好的一个解析几何题目!)

15 篇文章 1 订阅

滴答滴答---题目链接

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Vasya owns a cornfield which can be defined with two integers nn and dd. The cornfield can be represented as rectangle with vertices having Cartesian coordinates (0,d),(d,0),(n,n−d)(0,d),(d,0),(n,n−d) and (n−d,n)(n−d,n).

An example of a cornfield with n=7n=7 and d=2d=2.

Vasya also knows that there are mm grasshoppers near the field (maybe even inside it). The ii-th grasshopper is at the point (xi,yi)(xi,yi). Vasya does not like when grasshoppers eat his corn, so for each grasshopper he wants to know whether its position is inside the cornfield (including the border) or outside.

Help Vasya! For each grasshopper determine if it is inside the field (including the border).

Input

The first line contains two integers nn and dd (1≤d<n≤1001≤d<n≤100).

The second line contains a single integer mm (1≤m≤1001≤m≤100) — the number of grasshoppers.

The ii-th of the next mm lines contains two integers xixi and yiyi (0≤xi,yi≤n0≤xi,yi≤n) — position of the ii-th grasshopper.

Output

Print mm lines. The ii-th line should contain "YES" if the position of the ii-th grasshopper lies inside or on the border of the cornfield. Otherwise the ii-th line should contain "NO".

You can print each letter in any case (upper or lower).

Examples

input

Copy

7 2
4
2 4
4 1
6 3
4 5

output

Copy

YES
NO
NO
YES

input

Copy

8 7
4
4 4
2 8
8 1
6 1

output

Copy

YES
NO
YES
YES

Note

The cornfield from the first example is pictured above. Grasshoppers with indices 11 (coordinates (2,4)(2,4)) and 44 (coordinates (4,5)(4,5)) are inside the cornfield.

The cornfield from the second example is pictured below. Grasshoppers with indices 11 (coordinates (4,4)(4,4)), 33 (coordinates (8,1)(8,1)) and 44(coordinates (6,1)(6,1)) are inside the cornfield.

很好的一个解析几何题目!

题目大意:给定一个n * nn∗n的范围以及一个数字dd,再给定mm个点,要你求这mm个点是否分别在 (0,d),(d,0),(n,n - d)(n - d,n)(0,d),(d,0),(n,n−d)(n−d,n) 围成的范围里。

解析:真的是一道好题!

我们考虑先求出这四条直线的解析式。首先是过 (0,d),(d,0)(0,d),(d,0) 的:k = \frac{d - 0}{0 - d} = -1k=0−dd−0​=−1,b = db=d。于是乎 y= - x+dy=−x+d 推出 x + y = dx+y=d。

过 (0,d),(n - d,n)(0,d),(n−d,n) 的:k = \frac{d - n}{0 - (n - d)} = 1k=0−(n−d)d−n​=1,b = db=d,于是乎 y = x + dy=x+d 推出 x - y = -dx−y=−d。

过(n - d,n), (n, n - d)(n−d,n),(n,n−d)的:k = \frac{n - (n - d)}{(n - d) - n} = -1k=(n−d)−nn−(n−d)​=−1,b = -2n + db=−2n+d,于是乎 y = - x - 2n + dy=−x−2n+d 推出 x - y = 2n - dx−y=2n−d。

过(d,0), (n, n - d)(d,0),(n,n−d)的:k = \frac{0 - (n - d)}{d - n} = 1k=d−n0−(n−d)​=1,b = -db=−d,于是乎 y = x - dy=x−d 推出 x - y = dx−y=d。

此时我们再考虑如何判断这些点是否在里面即可。

于是乎问题转化成一个线性规划问题。

即:给定 44 条直线,求给定的点是否是在这四个点围成的矩形中。

于是只要满足:

d \leq x + yd≤x+yx + y \leq 2n - dx+y≤2n−d-d \leq x - y−d≤x−yx - y \leq dx−y≤d

即可。

#include<stdio.h>
#include<iostream>
using namespace std;
int main()
{int n,d;
cin>>n>>d;
int m;
cin>>m;
for(int i=1;i<=m;i++)
{
    int x,y;
    cin>>x>>y;
    if((x+y>=d)&&(x+y<=2*n-d)&&(-d<=x-y)&&(x-y<=d))
        cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值