Anton and Chess
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the program that plays chess. However, he finds the game on 8 to 8 board to too simple, he uses an infinite one instead.

The first task he faced is to check whether the king is in check. Anton doesn't know how to implement this so he asks you to help.

Consider that an infinite chess board contains one white king and the number of black pieces. There are only rooks, bishops and queens, as the other pieces are not supported yet. The white king is said to be in check if at least one black piece can reach the cell with the king in one move.

Help Anton and write the program that for the given position determines whether the white king is in check.

Remainder, on how do chess pieces move:

  • Bishop moves any number of cells diagonally, but it can't "leap" over the occupied cells.
  • Rook moves any number of cells horizontally or vertically, but it also can't "leap" over the occupied cells.
  • Queen is able to move any number of cells horizontally, vertically or diagonally, but it also can't "leap".
Input

The first line of the input contains a single integer n (1 ≤ n ≤ 500 000) — the number of black pieces.

The second line contains two integers x0 and y0 ( - 109 ≤ x0, y0 ≤ 109) — coordinates of the white king.

Then follow n lines, each of them contains a character and two integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — type of the i-th piece and its position. Character 'B' stands for the bishop, 'R' for the rook and 'Q' for the queen. It's guaranteed that no two pieces occupy the same position.

Output

The only line of the output should contains "YES" (without quotes) if the white king is in check and "NO" (without quotes) otherwise.

Examples
input
2
4 2
R 1 1
B 1 5
output
YES
input
2
4 2
R 3 3
B 1 5
output
NO
Note

Picture for the first sample:

White king is in check, because the black bishop can reach the cell with the white king in one move. The answer is "YES".

Picture for the second sample:

Here bishop can't reach the cell with the white king, because his path is blocked by the rook, and the bishop cant "leap" over it. Rook can't reach the white king, because it can't move diagonally. Hence, the king is not in check and the answer is "NO".

分析:找到最近的点,二分即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_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<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define freopen freopen("in.txt","r",stdin)
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t;
set<pair<int,char> >a,b,c,d;
bool flag;
int main()
{
int i,j;
scanf("%d%d%d",&t,&n,&m);
rep(i,,t)
{
char str[];
scanf("%s%d%d",str,&j,&k);
if(j==n)a.insert(mp(k,str[]));
else if(k==m)b.insert(mp(j,str[]));
else if(n-j==m-k)c.insert(mp(j,str[]));
else if(n-j==k-m)d.insert(mp(j,str[]));
}
//
auto p=a.lower_bound(mp(m,'B'));
if(p!=a.begin())
{
p--;
if(p->se=='Q'||p->se=='R')flag=true;
p++;
}
p=a.upper_bound(mp(m,'B'));
if(p!=a.end())
{
if(p->se=='Q'||p->se=='R')flag=true;
}
//
p=b.lower_bound(mp(n,'B'));
if(p!=b.begin())
{
p--;
if(p->se=='Q'||p->se=='R')flag=true;
p++;
}
p=b.upper_bound(mp(n,'B'));
if(p!=b.end())
{
if(p->se=='Q'||p->se=='R')flag=true;
}
//
p=c.lower_bound(mp(n,'B'));
if(p!=c.begin())
{
p--;
if(p->se=='Q'||p->se=='B')flag=true;
p++;
}
p=c.upper_bound(mp(n,'B'));
if(p!=c.end())
{
if(p->se=='Q'||p->se=='B')flag=true;
}
//
p=d.lower_bound(mp(n,'B'));
if(p!=d.begin())
{
p--;
if(p->se=='Q'||p->se=='B')flag=true;
p++;
}
p=d.upper_bound(mp(n,'B'));
if(p!=d.end())
{
if(p->se=='Q'||p->se=='B')flag=true;
}
//
puts(flag?"YES":"NO");
//system("Pause");
return ;
}

Anton and Chess的更多相关文章

  1. Codeforces Round #379 (Div. 2) D. Anton and Chess 水题

    D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...

  2. Codeforces Round #379 (Div. 2) D. Anton and Chess 模拟

    题目链接: http://codeforces.com/contest/734/problem/D D. Anton and Chess time limit per test4 secondsmem ...

  3. Codeforces Round #379 (Div. 2) D. Anton and Chess —— 基础题

    题目链接:http://codeforces.com/contest/734/problem/D D. Anton and Chess time limit per test 4 seconds me ...

  4. 【29.89%】【codeforces 734D】Anton and Chess

    time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. Codeforces 734D. Anton and Chess(模拟)

    Anton likes to play chess. Also, he likes to do programming. That is why he decided to write the pro ...

  6. D. Anton and Chess 模拟题 + 读题

    http://codeforces.com/contest/734/problem/D 一开始的时候看不懂题目,以为象是中国象棋那样走,然后看不懂样例. 原来是走对角线的,长知识了. 所以我们就知道, ...

  7. Anton and Chess(模拟+思维)

    http://codeforces.com/group/1EzrFFyOc0/contest/734/problem/D 题意:就是给你一个很大的棋盘,给你一个白棋的位置还有n个黑棋的位置,问你黑棋能 ...

  8. Codeforces Round #379 (Div. 2) A B C D 水 二分 模拟

    A. Anton and Danik time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. Codeforces Round #379 (Div. 2) Analyses By Team:Red & Black

    A.Anton and Danik Problems: 给你长度为N的,只含'A','D'的序列,统计并输出何者出现的较多,相同为"Friendship" Analysis: lu ...

随机推荐

  1. jQuery插件实现左右无缝轮播

    前段时间学习jQuery的时候,在网上找了个SuperSlide插件,做轮播图demo,感觉对于新人而言,还是挺容易上手的,代码量也少. 直接贴代码吧. 1.HTML <!DOCTYPE htm ...

  2. Modernizr 测试浏览器是否兼容相应属性

    Modernizr  测试浏览器是否兼容相应属性

  3. LeetCode OJ 4. Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

  4. Gentoo安装详解(二)-- 编译内核

    编译内核: 安装内核源码: 选择内核:如gentoo-sources emerge gentoo-sources ls -l /usr/src/linux 手动编译内核: cd /usr/src/li ...

  5. javacript参数传递表单验证

    <!doctype html> <html> <head> <meta charset="utf-8"> <style typ ...

  6. ajax 假上传文件

    1. <form name="certForm" id="certForm" method="post" action="x ...

  7. cookie的简单应用

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  8. javascript中函数的call,apply及bind方法

    call 方法调用一个对象的一个方法,以另一个对象替换当前对象.call([thisObj[,arg1[, arg2[,  [,.argN]]]]])参数thisObj可选项.将被用作当前对象的对象. ...

  9. UVA 10480 Sabotage

    最小割+输出方案 #include<cstdio> #include<cstring> #include<string> #include<cmath> ...

  10. java计数器CountDownLatch

    CountDownLatch里面有个属性为state,当为零时触发代码往下执行,代码如下: package threadLock; import java.util.Random; import ja ...