救救孩子吧
查看原帖
救救孩子吧
677234
FstAutoMaton楼主2024/7/10 12:28

已经调疯了,一直WA 5个点qaq

#include <bits/stdc++.h>

using namespace std;
using LL = long long;
using _i = __int128;

const int kMaxN = 4e5 + 5;

struct Dot {
  LL x, y;

  bool operator==(const Dot &b) const {
    return x == b.x && y == b.y;
  }

  bool operator<(const Dot &a) const {
    return x == a.x ? y < a.y : x < a.x;
  }
} a[kMaxN];

int n, q, pl = 1, pr = 1;      // 最下方,最上方
vector<pair<Dot, Dot> > v[2];  // 0:右半,1:左半

_i operator*(Dot a, Dot b) {
  return a.x * b.y - a.y * b.x;
}

Dot operator-(Dot a, Dot b) {
  return Dot{a.x - b.x, a.y - b.y};
}

Dot operator+(Dot a, Dot b) {
  return Dot{a.x + b.x, a.y + b.y};
}

int FindRight(int x) {
  if (v[0][0].first.y > x || v[0][v[0].size() - 1].second.y < x) {
    return -1;
  }
  int l = 0, r = v[0].size() - 1;
  for (int mid; l < r; v[0][mid].second.y >= x ? r = mid : l = mid + 1) {
    mid = l + r >> 1;
  }
  return r;
}

int FindLeft(int x) {
  if (v[1][0].first.y < x || v[1][v[1].size() - 1].second.y > x) {
    return -1;
  }
  int l = 0, r = v[1].size() - 1;
  for (int mid; l < r; v[1][mid].first.y >= x ? l = mid : r = mid - 1) {
    mid = l + r + 1 >> 1;
  }
  return l;
}

int main() {
  ios ::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i].x >> a[i].y;
    a[i + n] = a[i];
    (a[i].y <= a[pl].y) && (pl = i);
    (a[i].y >= a[pr].y) && (pr = i);
  }
  pr += n * (pl > pr);
  for (int i = pl; i < pr; i++) {
    v[0].push_back({a[i], a[i + 1]});
  }
  pr -= n * (pr > n), pl += (pr > pl) * n;
  for (int i = pr; i < pl; i++) {
    v[1].push_back({a[i], a[i + 1]});
  }
  for (cin >> q; q; q--) {
    Dot x;
    cin >> x.x >> x.y;
    int s = FindRight(x.y);
    int t = FindLeft(x.y);
    if (s == -1 || t == -1) {
      cout << "OUT\n";
    } else {
      Dot l = v[0][s].first, r = v[0][s].second;
      Dot f = l - x, g = r - x;
      // cout << l.x << " " << l.y << " " << r.x << " " << r.y << "\n";
      _i val = f * g;
      if (val > 0) {
        l = v[1][t].first, r = v[1][t].second;
        // cout << l.x << " " << l.y << " " << r.x << " " << r.y << "\n";
        f = l - x, g = r - x;
        val = f * g;
        cout << ((val > 0) ? "IN" : (val == 0 && min(l.x, r.x) <= x.x && max(l.x, r.x) >= x.x && min(l.y, r.y) <= x.y && max(l.y, r.y) >= x.y ? "ON" : "OUT")) << "\n";
      } else if (val == 0 && min(l.x, r.x) <= x.x && max(l.x, r.x) >= x.x && min(l.y, r.y) <= x.y && max(l.y, r.y) >= x.y) {
        cout << "ON\n";
      } else {
        cout << "OUT\n";
      }
    }
  }
  return 0;
}
2024/7/10 12:28
加载中...