shogo-library

競技プログラミング用の C++ ライブラリ

View the Project on GitHub shogo314/shogo-library

:heavy_check_mark: test/yosupo/geo/furthest-pair.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/furthest_pair"

#include <iostream>
#include <vector>

#include "../../../src/geometry/point-pair.hpp"
#include "../../../src/geometry/vector.hpp"

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int t;
    std::cin >> t;
    while (t--) {
        int n;
        std::cin >> n;
        std::vector<Vector<long long>> p(n);
        for (auto& x : p) std::cin >> x;
        auto res = farthest_point_pair_squared(p);
        std::cout << res.i << ' ' << res.j << '\n';
    }
}
#line 1 "test/yosupo/geo/furthest-pair.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/furthest_pair"

#include <iostream>
#include <vector>

#line 2 "src/geometry/point-pair.hpp"

/**
 * @file point-pair.hpp
 * @brief 最近点対・最遠点対
 */

#include <algorithm>
#include <cassert>
#include <cmath>
#include <limits>
#include <numeric>
#include <stack>
#include <utility>
#line 16 "src/geometry/point-pair.hpp"

#line 2 "src/geometry/polygon.hpp"

/**
 * @file polygon.hpp
 * @brief 多角形 (面積、重心、内外判定、凸性、凸包、凸多角形の切断)
 */

#line 11 "src/geometry/polygon.hpp"
#include <type_traits>
#line 14 "src/geometry/polygon.hpp"

#line 2 "src/geometry/vector.hpp"

/**
 * @file vector.hpp
 * @brief 幾何ベクトル
 */

#include <array>
#line 11 "src/geometry/vector.hpp"
#include <istream>
#include <ostream>
#line 15 "src/geometry/vector.hpp"

#line 2 "src/utility/traits.hpp"

/**
 * @file traits.hpp
 * @brief 型特性
 */

#include <cstddef>
#include <functional>
#include <iterator>
#include <set>
#line 14 "src/utility/traits.hpp"

#if __cplusplus >= 202002L
#include <concepts>
#include <ranges>
#endif

namespace internal_traits {
using std::begin, std::end;

#if __cplusplus >= 202002L
template <class T>
    struct is_range_impl : std::bool_constant < requires(T& t) {
    begin(t);
    end(t);
}>{};
#else
template <class T, class = void>
struct is_range_impl : std::false_type {};
template <class T>
struct is_range_impl<T, std::void_t<decltype(begin(std::declval<T&>()), end(std::declval<T&>()))>> : std::true_type {};
#endif

// サイズ付き範囲から要素を直接構築するコンストラクタで共有する。
#if __cplusplus >= 202002L
template <class R, class S>
concept sized_constructible_range = std::ranges::input_range<R> && std::ranges::sized_range<R> &&
                                    std::is_constructible_v<S, std::ranges::range_reference_t<R>>;

template <class R>
auto range_size(R& r) {
    return std::ranges::size(r);
}
template <class R>
auto range_begin(R& r) {
    return std::ranges::begin(r);
}
template <class R>
using range_value_t = std::ranges::range_value_t<R>;
#else
using std::size;
template <class R>
auto range_size(R& r) -> decltype(size(r)) {
    return size(r);
}
template <class R>
auto range_begin(R& r) -> decltype(begin(r)) {
    return begin(r);
}
template <class R>
using range_value_t = typename std::iterator_traits<decltype(range_begin(std::declval<R&>()))>::value_type;

template <class R, class S, class = void>
struct is_sized_constructible_range : std::false_type {};
template <class R, class S>
struct is_sized_constructible_range<R, S, std::void_t<decltype(range_size(std::declval<R&>())), decltype(++std::declval<decltype(begin(std::declval<R&>()))&>()), decltype(begin(std::declval<R&>()) != end(std::declval<R&>())), decltype(*begin(std::declval<R&>()))>>
    : std::is_constructible<S, decltype(*begin(std::declval<R&>()))> {};
#endif
}  // namespace internal_traits

template <class T>
struct is_range : internal_traits::is_range_impl<T>::type {};
template <class T>
inline constexpr bool is_range_v = is_range<T>::value;

#if __cplusplus >= 202002L
#define HAS_METHOD(func_name)                                                                   \
    namespace internal_traits {                                                                 \
    template <class T>                                                                          \
    concept has_##func_name##_impl = requires { std::declval<T>().func_name(); };               \
    }                                                                                           \
    template <class T>                                                                          \
    struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T>> {}; \
    template <class T>                                                                          \
    inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;

#define HAS_METHOD_ARG(func_name)                                                                  \
    namespace internal_traits {                                                                    \
    template <class T, class U>                                                                    \
    concept has_##func_name##_impl = requires { std::declval<T>().func_name(std::declval<U>()); }; \
    }                                                                                              \
    template <class T, class U>                                                                    \
    struct has_##func_name : std::bool_constant<internal_traits::has_##func_name##_impl<T, U>> {}; \
    template <class T, class U>                                                                    \
    inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#else
#define HAS_METHOD(func_name)                                                              \
    namespace internal_traits {                                                            \
    template <class T, class = void>                                                       \
    struct has_##func_name##_impl : std::false_type {};                                    \
    template <class T>                                                                     \
    struct has_##func_name##_impl<T, std::void_t<decltype(std::declval<T>().func_name())>> \
        : std::true_type {};                                                               \
    }                                                                                      \
    template <class T>                                                                     \
    struct has_##func_name : internal_traits::has_##func_name##_impl<T>::type {};          \
    template <class T>                                                                     \
    inline constexpr bool has_##func_name##_v = has_##func_name<T>::value;

#define HAS_METHOD_ARG(func_name)                                                                              \
    namespace internal_traits {                                                                                \
    template <class T, typename U, class = void>                                                               \
    struct has_##func_name##_impl : std::false_type {};                                                        \
    template <class T, typename U>                                                                             \
    struct has_##func_name##_impl<T, U, std::void_t<decltype(std::declval<T>().func_name(std::declval<U>()))>> \
        : std::true_type {};                                                                                   \
    }                                                                                                          \
    template <class T, typename U>                                                                             \
    struct has_##func_name : internal_traits::has_##func_name##_impl<T, U>::type {};                           \
    template <class T, typename U>                                                                             \
    inline constexpr bool has_##func_name##_v = has_##func_name<T, U>::value;
#endif

HAS_METHOD(repr)
HAS_METHOD(type_str)
HAS_METHOD(initializer_str)
HAS_METHOD(max)
HAS_METHOD(min)
HAS_METHOD(reversed)
HAS_METHOD(sorted)
HAS_METHOD(sum)
HAS_METHOD(product)
HAS_METHOD(product_xor)
HAS_METHOD_ARG(count)
HAS_METHOD_ARG(find)
HAS_METHOD_ARG(lower_bound)
HAS_METHOD_ARG(upper_bound)

#define ENABLE_IF_T_IMPL(expr) std::enable_if_t<expr, std::nullptr_t> = nullptr
#define ENABLE_IF_T(...) ENABLE_IF_T_IMPL((__VA_ARGS__))

namespace internal_traits {
// 射影 (proj) の既定値
#if __cplusplus >= 202002L
using identity = std::identity;
#else
struct identity {
    using is_transparent = void;
    template <class T>
    constexpr T&& operator()(T&& x) const noexcept {
        return std::forward<T>(x);
    }
};
#endif
}  // namespace internal_traits

template <class C>
using mem_value_type = typename C::value_type;
template <class C>
using mem_difference_type = typename C::difference_type;

namespace internal_traits {
template <class T, class = void>
struct is_sorted_container_impl : std::false_type {};
template <class T>
struct is_sorted_container_impl<std::set<T>> : std::true_type {};
template <class T>
struct is_sorted_container_impl<std::multiset<T>> : std::true_type {};
}  // namespace internal_traits
template <class T>
struct is_sorted_container : internal_traits::is_sorted_container_impl<T>::type {};
template <class T>
inline constexpr bool is_sorted_container_v = is_sorted_container<T>::value;
#line 17 "src/geometry/vector.hpp"

/**
 * @brief 幾何学としてのベクトル
 *
 * @tparam T 型 省略したらdouble
 * @tparam Dim 次元 省略したら2
 */
template <typename T = double, int Dim = 2, ENABLE_IF_T(std::is_scalar_v<T> && (Dim > 0))>
struct Vector {
    std::array<T, Dim> v;  //!< ベクトルの成分表示

    /**
     * @brief デフォルトコンストラクタ
     * ゼロベクトル
     */
    constexpr Vector() noexcept : v() {}
    /**
     * @brief 可変長引数によるコンストラクタ
     *
     * 各成分を T に変換して入れる (Vector(1, 2.5) のように型が混ざっても縮小変換の警告を出さない)
     * @tparam Args
     */
    template <typename... Args>
    constexpr Vector(Args... args) noexcept : v{static_cast<T>(args)...} {}

    /**
     * @brief i番目の成分を返す
     *
     * @param i
     * @return T&
     */
    constexpr T& operator[](long long i) noexcept {
        return v[i];
    }
    /**
     * @brief i番目の成分を返す
     *
     * @param i
     * @return T&
     */
    constexpr const T& operator[](long long i) const noexcept {
        return v[i];
    }

    /**
     * @brief 加算代入演算子
     *
     * @param rhs
     * @return Vector&
     */
    constexpr Vector& operator+=(const Vector& rhs) noexcept {
        for (int i = 0; i < Dim; i++) v[i] += rhs[i];
        return *this;
    }
    /**
     * @brief 減算代入演算子
     *
     * @param rhs
     * @return Vector&
     */
    constexpr Vector& operator-=(const Vector& rhs) noexcept {
        for (int i = 0; i < Dim; i++) v[i] -= rhs.v[i];
        return *this;
    }
    /**
     * @brief スカラー倍代入演算子
     *
     * @param coef
     * @return Vector&
     */
    constexpr Vector& operator*=(const T& coef) noexcept {
        for (int i = 0; i < Dim; i++) v[i] *= coef;
        return *this;
    }
    /**
     * @brief スカラーの逆数倍代入演算子
     *
     * @param coef
     * @return Vector&
     */
    constexpr Vector& operator/=(const T& coef) {
        for (int i = 0; i < Dim; i++) {
            if constexpr (std::is_integral_v<T>) {
                assert(v[i] % coef == 0 && "Vector::operator/= : coef must be a divisor of all elements");
            }
            v[i] /= coef;
        }
        return *this;
    }

    /**
     * @brief 単項プラス演算子
     *
     * @return Vector
     */
    [[nodiscard]]
    constexpr Vector operator+() const noexcept {
        return *this;
    }
    /**
     * @brief 単項マイナス演算子
     *
     * @return Vector
     */
    [[nodiscard]]
    constexpr Vector operator-() const noexcept {
        return *this * (-1);
    }

    /**
     * @brief 加算演算子
     *
     * @param lhs
     * @param rhs
     * @return Vector
     */
    [[nodiscard]]
    friend constexpr Vector operator+(const Vector& lhs, const Vector& rhs) noexcept {
        return std::move(Vector(lhs) += rhs);
    }

    /**
     * @brief 減算演算子
     *
     * @param lhs
     * @param rhs
     * @return Vector
     */
    [[nodiscard]]
    friend constexpr Vector operator-(const Vector& lhs, const Vector& rhs) noexcept {
        return std::move(Vector(lhs) -= rhs);
    }

    /**
     * @brief スカラー倍演算子
     *
     * @param lhs
     * @param coef
     * @return Vector
     */
    [[nodiscard]]
    friend constexpr Vector operator*(const Vector& lhs, const T& coef) noexcept {
        return std::move(Vector(lhs) *= coef);
    }

    /**
     * @brief スカラー倍演算子
     *
     * @param coef
     * @param rhs
     * @return Vector
     */
    [[nodiscard]]
    friend constexpr Vector operator*(const T& coef, const Vector& rhs) noexcept {
        return std::move(Vector(rhs) *= coef);
    }

    /**
     * @brief スカラーの逆数倍演算子
     *
     * @param lhs
     * @param coef
     * @return Vector
     */
    [[nodiscard]]
    friend constexpr Vector operator/(const Vector& lhs, const T& coef) {
        return std::move(Vector(lhs) /= coef);
    }

    /**
     * @brief 等価演算子
     *
     * @param lhs
     * @param rhs
     * @return bool
     */
    [[nodiscard]]
    friend constexpr bool operator==(const Vector& lhs, const Vector& rhs) noexcept {
        return lhs.v == rhs.v;
    }

    /**
     * @brief 非等価演算子
     *
     * @param lhs
     * @param rhs
     * @return bool
     */
    [[nodiscard]]
    friend constexpr bool operator!=(const Vector& lhs, const Vector& rhs) noexcept {
        return lhs.v != rhs.v;
    }

    /**
     * @brief 小なり演算子
     *
     * @param lhs
     * @param rhs
     * @return bool
     */
    [[nodiscard]]
    friend constexpr bool operator<(const Vector& lhs, const Vector& rhs) noexcept {
        return lhs.v < rhs.v;
    }
    /**
     * @brief 大なり演算子
     *
     * @param lhs
     * @param rhs
     * @return bool
     */
    [[nodiscard]]
    friend constexpr bool operator>(const Vector& lhs, const Vector& rhs) noexcept {
        return lhs.v > rhs.v;
    }
    /**
     * @brief 小なりイコール演算子
     *
     * @param lhs
     * @param rhs
     * @return bool
     */
    [[nodiscard]]
    friend constexpr bool operator<=(const Vector& lhs, const Vector& rhs) noexcept {
        return lhs.v <= rhs.v;
    }
    /**
     * @brief 大なりイコール演算子
     *
     * @param lhs
     * @param rhs
     * @return bool
     */
    [[nodiscard]]
    friend constexpr bool operator>=(const Vector& lhs, const Vector& rhs) noexcept {
        return lhs.v >= rhs.v;
    }

    /**
     * @brief 入力ストリームからの読み込み
     *
     * @tparam CharT 入力ストリームの文字型
     * @tparam Traits 入力ストリームの文字型特性
     * @param is
     * @param rhs
     * @return std::basic_istream<CharT, Traits>&
     */
    template <class CharT, class Traits>
    friend std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, Vector& rhs) {
        for (int i = 0; i < Dim; i++) is >> rhs.v[i];
        return is;
    }
    /**
     * @brief 出力ストリームへの書き込み
     *
     * @tparam CharT 出力ストリームの文字型
     * @tparam Traits 出力ストリームの文字型特性
     * @param is
     * @param rhs
     * @return std::basic_ostream<CharT, Traits>&
     */
    template <class CharT, class Traits>
    friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const Vector& rhs) {
        for (int i = 0; i < Dim; i++) {
            if (i != 0) os << CharT(' ');
            os << rhs.v[i];
        }
        return os;
    }

    /**
     * @brief 内積
     *
     * @param lhs
     * @param rhs
     * @return T
     */
    friend constexpr T dot(const Vector& lhs, const Vector& rhs) noexcept {
        T ret = 0;
        for (int i = 0; i < Dim; i++) ret += lhs[i] * rhs[i];
        return ret;
    }

    /**
     * @brief 絶対値の2乗
     *
     * @return T
     */
    constexpr T squared_norm() const noexcept {
        return dot(*this, *this);
    }
    /**
     * @brief 絶対値
     *
     * @return double
     */
    constexpr double abs() const noexcept {
        return std::sqrt(squared_norm());
    }
};

/**
 * @brief 成分から作るときの推論ガイド。型は成分の共通の型、次元は成分の個数にする
 *
 * Vector(2LL, 3LL) は Vector<long long, 2>、Vector(1, 2, 3) は Vector<int, 3>、Vector(0.0, 0.0) は Vector<double, 2>
 * (これがないと、テンプレート引数の省略時の値から Vector<double, 2> になってしまう)
 */
template <typename T, typename... U, ENABLE_IF_T(std::is_scalar_v<T> && (std::is_scalar_v<U> && ...))>
Vector(T, U...) -> Vector<std::common_type_t<T, U...>, 1 + sizeof...(U)>;

/**
 * @brief 二次元ベクトルどうしの外積(平行四辺形の符号付き面積)
 *
 * @param lhs
 * @param rhs
 * @tparam T 座標の型
 * @return T
 */
template <typename T>
constexpr T cross(const Vector<T, 2>& lhs, const Vector<T, 2>& rhs) noexcept {
    return lhs[0] * rhs[1] - lhs[1] * rhs[0];
}
/**
 * @brief 三次元ベクトルどうしの外積
 *
 * @param lhs
 * @param rhs
 * @tparam T 座標の型
 * @return Vector<T, 3>
 */
template <typename T>
constexpr Vector<T, 3> cross(const Vector<T, 3>& lhs, const Vector<T, 3>& rhs) noexcept {
    Vector<T, 3> ret;
    ret[0] = lhs[1] * rhs[2] - lhs[2] * rhs[1];
    ret[1] = lhs[2] * rhs[0] - lhs[0] * rhs[2];
    ret[2] = lhs[0] * rhs[1] - lhs[1] * rhs[0];
    return ret;
}

/**
 * @brief 3点の位置関係 (counter-clockwise)
 *
 * 整数型でも使える。浮動小数点型では誤差を考えずに比較する
 *
 * @tparam T 座標の型
 * @return int 1: a→b→c が反時計回り, -1: 時計回り, 2: c, a, b の順に一直線上, -2: a, b, c の順に一直線上, 0: c が線分 ab 上
 */
template <typename T>
constexpr int ccw(const Vector<T, 2>& a, const Vector<T, 2>& b, const Vector<T, 2>& c) {
    Vector<T, 2> u = b - a, v = c - a;
    T cr = cross(u, v);
    if (cr > 0) return 1;
    if (cr < 0) return -1;
    if (dot(u, v) < 0) return 2;
    if (u.squared_norm() < v.squared_norm()) return -2;
    return 0;
}
#line 16 "src/geometry/polygon.hpp"

/**
 * @brief 多角形の符号付き面積の2倍
 *
 * @tparam T 座標の型
 * @param polygon 頂点の座標
 * @return T 面積の2倍(反時計回りなら正、時計回りなら負)
 */
template <typename T>
T polygon_area_doubled(const std::vector<Vector<T, 2>>& polygon) {
    T ret = 0;
    for (int i = 0; i < static_cast<int>(polygon.size()); i++) {
        int j = i + 1;
        if (j == static_cast<int>(polygon.size())) j = 0;
        ret += cross(polygon[i], polygon[j]);
    }
    return ret;
}

/**
 * @brief 多角形の面積
 *
 * @tparam T 座標の型
 * @param polygon 頂点の座標(順番、反時計回りか時計回りかは問わない)
 * @return double 面積
 */
template <typename T>
double polygon_area(const std::vector<Vector<T, 2>>& polygon) {
    return std::abs(polygon_area_doubled(polygon)) / 2.;
}

/**
 * @brief 多角形の面積と一次モーメント (polygon_edge_moment の戻り値)
 * 辺ごとの値を足すと、多角形全体の値になる。+ と - があるので、CircularSum などの累積和に載せられる
 *
 * @tparam T 座標の型。整数型なら値は __int128_t で持つ (座標が 1e9 なら mx は 1e27 程度になる)
 */
template <typename T>
struct PolygonMoment {
    using W = std::conditional_t<std::is_integral_v<T>, __int128_t, T>;
    W area2 = 0;       //!< 符号付き面積 S の 2 倍 (反時計回りなら正)
    W mx = 0, my = 0;  //!< 一次モーメントの 6 倍 (6S gx, 6S gy。(gx, gy) は重心)

    PolygonMoment& operator+=(const PolygonMoment& rhs) {
        area2 += rhs.area2, mx += rhs.mx, my += rhs.my;
        return *this;
    }
    PolygonMoment& operator-=(const PolygonMoment& rhs) {
        area2 -= rhs.area2, mx -= rhs.mx, my -= rhs.my;
        return *this;
    }
    friend PolygonMoment operator+(PolygonMoment lhs, const PolygonMoment& rhs) { return lhs += rhs; }
    friend PolygonMoment operator-(PolygonMoment lhs, const PolygonMoment& rhs) { return lhs -= rhs; }
    friend bool operator==(const PolygonMoment& lhs, const PolygonMoment& rhs) {
        return lhs.area2 == rhs.area2 && lhs.mx == rhs.mx && lhs.my == rhs.my;
    }
    friend bool operator!=(const PolygonMoment& lhs, const PolygonMoment& rhs) { return !(lhs == rhs); }

    /**
     * @brief 符号付き面積 (反時計回りなら正)
     */
    double area() const { return static_cast<double>(area2) / 2; }
    /**
     * @brief 重心 (面積が 0 なら assert で落ちる)
     */
    Vector<double, 2> centroid() const {
        assert(area2 != 0 && "PolygonMoment::centroid: area must be nonzero");
        const double a = static_cast<double>(area2) * 3;
        return Vector<double, 2>(static_cast<double>(mx) / a, static_cast<double>(my) / a);
    }
};

/**
 * @brief 多角形の辺 p→q の寄与 (原点と p, q でできる三角形の面積と一次モーメント)
 * 全辺の寄与の和が多角形の値になる。部分多角形のクエリは、寄与の累積和 (CircularSum など) で求められる
 *
 * @tparam T 座標の型
 */
template <typename T>
PolygonMoment<T> polygon_edge_moment(const Vector<T, 2>& p, const Vector<T, 2>& q) {
    using W = typename PolygonMoment<T>::W;
    const W c = static_cast<W>(p[0]) * q[1] - static_cast<W>(p[1]) * q[0];
    return {c, (static_cast<W>(p[0]) + q[0]) * c, (static_cast<W>(p[1]) + q[1]) * c};
}

/**
 * @brief 多角形 (の領域) の重心
 *
 * @tparam T 座標の型
 * @param polygon 頂点の座標(順番、反時計回りか時計回りかは問わない。面積が 0 なら assert で落ちる)
 * @return Vector<double, 2> 重心
 */
template <typename T>
Vector<double, 2> polygon_centroid(const std::vector<Vector<T, 2>>& polygon) {
    const int n = static_cast<int>(polygon.size());
    PolygonMoment<T> m;
    for (int i = 0; i < n; i++) m += polygon_edge_moment(polygon[i], polygon[i + 1 == n ? 0 : i + 1]);
    return m.centroid();
}

/**
 * @brief 多角形の凸性判定
 *
 * @tparam T 座標の型
 * @param polygon 頂点の座標(反時計回り)
 * @return true 凸
 * @return false 凹
 */
template <typename T>
bool polygon_is_convex(const std::vector<Vector<T, 2>>& polygon) {
    int n = polygon.size();
    for (int i = 0; i < n; i++) {
        int j = i + 1;
        if (j == n) j = 0;
        int k = j + 1;
        if (k == n) k = 0;
        if (cross(polygon[j] - polygon[i], polygon[k] - polygon[j]) < 0) return false;
    }
    return true;
}

/**
 * @brief 点の多角形内包含判定
 *
 * @tparam T 座標の型
 * @param polygon 頂点の座標(反時計回り)
 * @param p 点の座標
 * @return int 0: 外部, 1: 辺上, 2: 内部
 */
template <typename T>
int polygon_contains(const std::vector<Vector<T, 2>>& polygon, const Vector<T, 2>& p) {
    bool in = false;
    for (int i = 0; i < static_cast<int>(polygon.size()); i++) {
        int j = i + 1;
        if (j == static_cast<int>(polygon.size())) j = 0;
        Vector<T> v1 = polygon[i] - p, v2 = polygon[j] - p;
        if (v1[1] > v2[1]) std::swap(v1, v2);
        T c = cross(v1, v2);
        if (v1[1] <= 0 && v2[1] > 0 && c < 0) in = !in;
        if (c == 0 && dot(v1, v2) <= 0) return 1;
    }
    return in ? 2 : 0;
}

/**
 * @brief 多角形の半凸包
 * 与えられた頂点リストの順番を保って反時計回りに凸包を構成する
 * xの昇順にソートされていれば下凸包、xの降順にソートされていれば上凸包が得られる
 *
 * @tparam T 座標の型
 * @param points 頂点リスト
 * @param include_straight 内角が180度の頂点を含むかどうか
 * @return std::vector<Vector<T, 2>> 半凸包の頂点の座標(反時計回り)
 */
template <typename T>
std::vector<Vector<T, 2>> select_convex(const std::vector<Vector<T, 2>>& points, bool include_straight = false) {
    std::vector<Vector<T, 2>> ret;
    for (auto& p : points) {
        while (static_cast<int>(ret.size()) >= 2) {
            T c = cross(ret.back() - ret[static_cast<int>(ret.size()) - 2], p - ret.back());
            if (c > 0 || (c == 0 && include_straight)) break;
            ret.pop_back();
        }
        ret.push_back(p);
    }
    return ret;
}

/**
 * @brief 多角形の凸包
 *
 * @tparam T 座標の型
 * @param points 頂点リスト
 * @param include_straight 内角が180度の頂点を含むかどうか
 * @return std::vector<Vector<T, 2>> 凸包の頂点の座標(反時計回り)
 */
template <typename T>
std::vector<Vector<T, 2>> convex_hull(const std::vector<Vector<T, 2>>& _points, bool include_straight = false) {
    std::vector<Vector<T, 2>> points = _points;
    std::sort(points.begin(), points.end());
    points.erase(std::unique(points.begin(), points.end()), points.end());
    std::vector<Vector<T, 2>> ret = select_convex(points, include_straight);
    std::reverse(points.begin(), points.end());
    std::vector<Vector<T, 2>> tmp = select_convex(points, include_straight);
    // 全点が一直線上にある場合、下側と上側が同じ点列になるので片方だけ使う
    if (ret.size() == points.size() && tmp.size() == points.size()) return ret;
    for (int i = 1; i < static_cast<int>(tmp.size()) - 1; i++) ret.push_back(tmp[i]);
    return ret;
}

/**
 * @brief 凸多角形を直線 p1p2 で切り、直線の左側 (p1 から p2 を見て左) の部分を返す。浮動小数点型限定
 *
 * @tparam T 座標の型
 * @param polygon 凸多角形の頂点の座標 (反時計回り)
 * @return std::vector<Vector<T, 2>> 左側の凸多角形の頂点 (反時計回り)。直線上の頂点も含む。左側に何もなければ空
 */
template <typename T>
std::vector<Vector<T, 2>> convex_cut(const std::vector<Vector<T, 2>>& polygon, const Vector<T, 2>& p1, const Vector<T, 2>& p2) {
    static_assert(std::is_floating_point_v<T>, "convex_cut: T must be a floating point type");
    assert(p1 != p2 && "convex_cut: degenerate line");
    std::vector<Vector<T, 2>> res;
    const int n = static_cast<int>(polygon.size());
    for (int i = 0; i < n; i++) {
        const Vector<T, 2>&a = polygon[i], &b = polygon[i + 1 == n ? 0 : i + 1];
        T ca = cross(p2 - p1, a - p1), cb = cross(p2 - p1, b - p1);
        if (ca >= 0) res.push_back(a);
        // 辺 ab が直線をまたぐなら、交点を入れる
        if ((ca > 0 && cb < 0) || (ca < 0 && cb > 0)) res.push_back(a + (b - a) * (ca / (ca - cb)));
    }
    return res;
}
#line 19 "src/geometry/point-pair.hpp"

/**
 * @brief closest_point_pair_squared の戻り値
 */
template <typename T>
struct ClosestPairSquared {
    T squared_dist;  //!< 最近点対の距離の2乗
    long long i, j;  //!< 最近点対の添字
};
/**
 * @brief closest_point_pair の戻り値
 */
struct ClosestPair {
    double dist;     //!< 最近点対の距離
    long long i, j;  //!< 最近点対の添字
};

/**
 * @brief 最近点対
 *
 * @tparam T 座標の型
 * @param points 頂点リスト (2個以上)
 * @return ClosestPairSquared<T> 最近点対の距離の2乗と添字
 */
template <typename T>
ClosestPairSquared<T> closest_point_pair_squared(const std::vector<Vector<T, 2>>& points) {
    assert(points.size() >= 2 && "closest_point_pair_squared: fewer than 2 points");
    std::vector<int> sorted_idx(points.size());
    std::iota(sorted_idx.begin(), sorted_idx.end(), 0);
    std::sort(sorted_idx.begin(), sorted_idx.end(), [&](int i, int j) {
        return points[i][0] < points[j][0];
    });
    std::vector<int> left(1, 0);                                        // 左端
    std::vector<int> right(1, points.size());                           // 右端
    std::vector<int> mid(1, points.size() / 2);                         // 中央
    std::vector<int> par(1, -1);                                        // 親
    std::vector<T> min_dist_squared(1, std::numeric_limits<T>::max());  // 最小距離の2乗
    std::vector<std::pair<int, int>> min_dist_pair(1, {-1, -1});        // 最小距離の点対
    std::stack<int> stk;
    stk.push(~0);
    stk.push(0);
    while (!stk.empty()) {
        int u = stk.top();
        stk.pop();
        if (u >= 0) {
            // 行きがけ
            if (left[u] + 1 < mid[u]) {
                int v = left.size();
                left.push_back(left[u]);
                right.push_back(mid[u]);
                mid.push_back((left[u] + mid[u]) / 2);
                par.push_back(u);
                min_dist_squared.push_back(std::numeric_limits<T>::max());
                min_dist_pair.push_back({-1, -1});
                stk.push(~v);
                stk.push(v);
            }
            if (mid[u] + 1 < right[u]) {
                int v = left.size();
                left.push_back(mid[u]);
                right.push_back(right[u]);
                mid.push_back((mid[u] + right[u]) / 2);
                par.push_back(u);
                min_dist_squared.push_back(std::numeric_limits<T>::max());
                min_dist_pair.push_back({-1, -1});
                stk.push(~v);
                stk.push(v);
            }
        } else {
            // 帰りがけ
            u = ~u;
            std::vector<int> cand;
            for (int i = left[u]; i < right[u]; i++) {
                T dx = points[sorted_idx[i]][0] - points[sorted_idx[mid[u]]][0];
                if (dx * dx < min_dist_squared[u]) cand.push_back(i);
            }
            std::sort(cand.begin(), cand.end(), [&](int i, int j) {
                return points[sorted_idx[i]][1] < points[sorted_idx[j]][1];
            });
            for (int i = 0; i < static_cast<int>(cand.size()); i++) {
                for (int j = i + 1; j < static_cast<int>(cand.size()); j++) {
                    T dx = points[sorted_idx[cand[j]]][0] - points[sorted_idx[cand[i]]][0];
                    T dy = points[sorted_idx[cand[j]]][1] - points[sorted_idx[cand[i]]][1];
                    if (dy * dy >= min_dist_squared[u]) break;
                    T d = dx * dx + dy * dy;
                    if (d < min_dist_squared[u]) {
                        min_dist_squared[u] = d;
                        min_dist_pair[u] = {sorted_idx[cand[i]], sorted_idx[cand[j]]};
                    }
                }
            }
            if (par[u] != -1 && min_dist_squared[u] < min_dist_squared[par[u]]) {
                min_dist_squared[par[u]] = min_dist_squared[u];
                min_dist_pair[par[u]] = min_dist_pair[u];
            }
        }
    }
    return {min_dist_squared[0], min_dist_pair[0].first, min_dist_pair[0].second};
}

/**
 * @brief 最近点対
 *
 * @tparam T 座標の型
 * @param points 頂点リスト (2個以上)
 * @return ClosestPair 最近点対の距離と添字
 */
template <typename T>
ClosestPair closest_point_pair(const std::vector<Vector<T, 2>>& points) {
    auto [d2, i, j] = closest_point_pair_squared(points);
    return {std::sqrt(static_cast<double>(d2)), i, j};
}

/**
 * @brief farthest_point_pair_squared の戻り値
 */
template <typename T>
struct FarthestPairSquared {
    T squared_dist;  //!< 最遠点対の距離の2乗
    long long i, j;  //!< 最遠点対の添字
};
/**
 * @brief farthest_point_pair の戻り値
 */
struct FarthestPair {
    double dist;     //!< 最遠点対の距離
    long long i, j;  //!< 最遠点対の添字
};

/**
 * @brief 最遠点対 (凸包を作り、回転キャリパー法で対蹠点の組を調べる) O(n log n)
 *
 * 整数型でも使える。座標の差の2乗の和の2倍が T に収まる必要がある
 * @tparam T 座標の型
 * @param points 頂点リスト (2個以上)
 * @return FarthestPairSquared<T> 最遠点対の距離の2乗と添字 (i != j)
 */
template <typename T>
FarthestPairSquared<T> farthest_point_pair_squared(const std::vector<Vector<T, 2>>& points) {
    const int n = static_cast<int>(points.size());
    assert(n >= 2);
    // 凸包の頂点を、もとの添字に戻せるようにしておく
    std::vector<std::pair<Vector<T, 2>, int>> sorted(n);
    for (int i = 0; i < n; i++) sorted[i] = {points[i], i};
    std::sort(sorted.begin(), sorted.end());
    auto index_of = [&](const Vector<T, 2>& p) {
        return std::lower_bound(sorted.begin(), sorted.end(), std::make_pair(p, -1))->second;
    };
    std::vector<Vector<T, 2>> hull = convex_hull(points);
    const int k = static_cast<int>(hull.size());
    if (k == 1) return {T(0), sorted[0].second, sorted[1].second};  // すべて同じ点
    FarthestPairSquared<T> res{T(0), -1, -1};
    auto update = [&](int a, int b) {
        T d = (hull[a] - hull[b]).squared_norm();
        if (res.i == -1 || d > res.squared_dist) res = {d, a, b};
    };
    if (k == 2) {
        update(0, 1);
    } else {
        // 辺 i→i+1 に対して、最も遠い頂点 j を反時計回りに進めていく
        int j = 1;
        for (int i = 0; i < k; i++) {
            int ni = i + 1 == k ? 0 : i + 1;
            while (true) {
                int nj = j + 1 == k ? 0 : j + 1;
                if (cross(hull[ni] - hull[i], hull[nj] - hull[j]) > 0)
                    j = nj;
                else
                    break;
            }
            update(i, j);
            update(ni, j);
        }
    }
    return {res.squared_dist, index_of(hull[res.i]), index_of(hull[res.j])};
}

/**
 * @brief 最遠点対
 *
 * @tparam T 座標の型
 * @param points 頂点リスト (2個以上)
 * @return FarthestPair 最遠点対の距離と添字
 */
template <typename T>
FarthestPair farthest_point_pair(const std::vector<Vector<T, 2>>& points) {
    auto [d2, i, j] = farthest_point_pair_squared(points);
    return {std::sqrt(static_cast<double>(d2)), i, j};
}
#line 8 "test/yosupo/geo/furthest-pair.test.cpp"

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int t;
    std::cin >> t;
    while (t--) {
        int n;
        std::cin >> n;
        std::vector<Vector<long long>> p(n);
        for (auto& x : p) std::cin >> x;
        auto res = farthest_point_pair_squared(p);
        std::cout << res.i << ' ' << res.j << '\n';
    }
}
Back to top page