Submission #41141099


Source Code Expand

#include <atcoder/all>
using namespace atcoder;
#include <bits/stdc++.h>
#include <cassert>
using namespace std;
using ll = long long;
const double pi = 3.14159265359;
const ll INF = 1LL << 60;

template<class T> inline bool chmin(T& a, T b){ if (a > b){a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b){ if (a < b){a = b; return true;} return false;}

//using mint = modint998244353;
//using mint = modint1000000007;
//using mint = modint;  // mint::set_mod(p); later
//using mint = static_modint<1000000009>;

int N;
vector<ll> x, y;

// (a, b) がベクトル Pi->Pj のどちらにあるかを調べるために外積を求める
ll check(int i, int j, ll u, ll v)
{
  ll a = x[j] - x[i];
  ll b = y[j] - y[i];
  ll c = u - x[i];
  ll d = v - y[i];
  return a*d - b*c;
}

void solve(ll u, ll v)
{
  if (check(0, 1, u, v) < 0 || check(0, N-1, u, v) > 0){
    cout << "OUT\n";
    return;
  }

  // P0->P1 上
  if (check(0, 1, u, v) == 0){
    ll a = x[1] - x[0];
    ll b = y[1] - y[0];
    ll c = u - x[0];
    ll d = v - y[0];
    if (a*a + b*b >= c*c + d*d){
      cout << "ON\n";
    } else {
      cout << "OUT\n";
    }
    return;
  }

  // P0->PN-1上
  if (check(0, N-1, u, v) == 0){
    ll a = x[N-1] - x[0];
    ll b = y[N-1] - y[0];
    ll c = u - x[0];
    ll d = v - y[0];
    if (a*a + b*b >= c*c + d*d){
      cout << "ON\n";
    } else {
      cout << "OUT\n";
    }
    return;
  }
  
  // どの三角形の間に入り得るのかをニブタン
  int ok = 1;
  int ng = N-1;
  while (abs(ok-ng) > 1){
    int mid = (ok + ng) / 2;
    if (check(0, mid, u, v) >= 0){
      ok = mid;
    } else {
      ng = mid;
    }
  }

  // 実際に三角形の中に入っているか
  ll op = check(ok, ok+1, u, v); // outer product
  if (op == 0){
    cout << "ON\n";
  } else if (op > 0){
    cout << "IN\n";
  } else {
    cout << "OUT\n";
  }
}

int main()
{
  cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);

  // 入力
  cin >> N;
  x.resize(N);
  y.resize(N);
  for (int i = 0; i < N; i++) cin >> x[i] >> y[i];

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++){
    ll a, b;
    cin >> a >> b;
    solve(a, b);
  }
  
  return 0;
}

Submission Info

Submission Time
Task G - Polygon and Points
User unnohideyuki
Language C++ (GCC 9.2.1)
Score 600
Code Size 2303 Byte
Status AC
Exec Time 122 ms
Memory 6340 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 2
AC × 26
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All hack_01.txt, hack_02.txt, hand_01.txt, hand_02.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, sample_01.txt, sample_02.txt
Case Name Status Exec Time Memory
hack_01.txt AC 9 ms 3612 KiB
hack_02.txt AC 2 ms 3536 KiB
hand_01.txt AC 2 ms 3668 KiB
hand_02.txt AC 2 ms 3484 KiB
random_01.txt AC 2 ms 3512 KiB
random_02.txt AC 2 ms 3564 KiB
random_03.txt AC 116 ms 6320 KiB
random_04.txt AC 96 ms 4952 KiB
random_05.txt AC 115 ms 6200 KiB
random_06.txt AC 109 ms 6272 KiB
random_07.txt AC 104 ms 5464 KiB
random_08.txt AC 104 ms 5484 KiB
random_09.txt AC 116 ms 6340 KiB
random_10.txt AC 110 ms 5796 KiB
random_11.txt AC 97 ms 5268 KiB
random_12.txt AC 102 ms 5532 KiB
random_13.txt AC 104 ms 5232 KiB
random_14.txt AC 116 ms 5936 KiB
random_15.txt AC 115 ms 6276 KiB
random_16.txt AC 121 ms 6288 KiB
random_17.txt AC 122 ms 6324 KiB
random_18.txt AC 5 ms 3660 KiB
random_19.txt AC 50 ms 3472 KiB
random_20.txt AC 7 ms 3588 KiB
sample_01.txt AC 2 ms 3464 KiB
sample_02.txt AC 2 ms 3608 KiB