Anton and Chess

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+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;
set<pair<int,char> >a,b,c,d;
bool flag;
int main()
{
    int i,j;
    scanf("%d%d%d",&t,&n,&m);
    rep(i,1,t)
    {
        char str[2];
        scanf("%s%d%d",str,&j,&k);
        if(j==n)a.insert(mp(k,str[0]));
        else if(k==m)b.insert(mp(j,str[0]));
        else if(n-j==m-k)c.insert(mp(j,str[0]));
        else if(n-j==k-m)d.insert(mp(j,str[0]));
    }
    //
    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 0;
}
posted @ 2016-11-17 16:57  mxzf0213  阅读(205)  评论(0编辑  收藏  举报