QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#107573 | #5570. Epidemic Escape | maspy | TL | 68ms | 5656kb | C++23 | 25.2kb | 2023-05-22 01:17:45 | 2023-05-22 01:17:47 |
Judging History
answer
#line 1 "library/my_template.hpp"
#if defined(LOCAL)
#include <my_template_compiled.hpp>
#else
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;
template <class T>
constexpr T infty = 0;
template <>
constexpr int infty<int> = 1'000'000'000;
template <>
constexpr ll infty<ll> = ll(infty<int>) * infty<int> * 2;
template <>
constexpr u32 infty<u32> = infty<int>;
template <>
constexpr u64 infty<u64> = infty<ll>;
template <>
constexpr i128 infty<i128> = i128(infty<ll>) * infty<ll>;
template <>
constexpr double infty<double> = infty<ll>;
template <>
constexpr long double infty<long double> = infty<ll>;
using pi = pair<ll, ll>;
using vi = vector<ll>;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
#define vv(type, name, h, ...) \
vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) \
vector<vector<vector<type>>> name( \
h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...) \
vector<vector<vector<vector<type>>>> name( \
a, vector<vector<vector<type>>>( \
b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))
// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define overload4(a, b, c, d, e, ...) e
#define overload3(a, b, c, d, ...) d
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) overload3(__VA_ARGS__, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)
#define FOR_subset(t, s) \
for (ll t = (s); t >= 0; t = (t == 0 ? -1 : (t - 1) & (s)))
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define stoi stoll
int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
template <typename T, typename U>
T ceil(T x, U y) {
return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U>
T floor(T x, U y) {
return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T, typename U>
pair<T, T> divmod(T x, U y) {
T q = floor(x, y);
return {q, x - q * y};
}
template <typename T, typename U>
T SUM(const vector<U> &A) {
T sum = 0;
for (auto &&a: A) sum += a;
return sum;
}
#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) \
sort(all(x)), x.erase(unique(all(x)), x.end()), x.shrink_to_fit()
template <typename T>
T POP(deque<T> &que) {
T a = que.front();
que.pop_front();
return a;
}
template <typename T>
T POP(pq<T> &que) {
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(pqg<T> &que) {
assert(!que.empty());
T a = que.top();
que.pop();
return a;
}
template <typename T>
T POP(vc<T> &que) {
assert(!que.empty());
T a = que.back();
que.pop_back();
return a;
}
template <typename F>
ll binary_search(F check, ll ok, ll ng, bool check_ok = true) {
if (check_ok) assert(check(ok));
while (abs(ok - ng) > 1) {
auto x = (ng + ok) / 2;
tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
}
return ok;
}
template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
FOR(iter) {
double x = (ok + ng) / 2;
tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
}
return (ok + ng) / 2;
}
template <class T, class S>
inline bool chmax(T &a, const S &b) {
return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
return (a > b ? a = b, 1 : 0);
}
// ? は -1
vc<int> s_to_vi(const string &S, char first_char) {
vc<int> A(S.size());
FOR(i, S.size()) { A[i] = (S[i] != '?' ? S[i] - first_char : -1); }
return A;
}
template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
int N = A.size();
vector<T> B(N + 1);
FOR(i, N) { B[i + 1] = B[i] + A[i]; }
if (off == 0) B.erase(B.begin());
return B;
}
// stable sort
template <typename T>
vector<int> argsort(const vector<T> &A) {
vector<int> ids(len(A));
iota(all(ids), 0);
sort(all(ids),
[&](int i, int j) { return (A[i] == A[j] ? i < j : A[i] < A[j]); });
return ids;
}
// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
vc<T> B(len(I));
FOR(i, len(I)) B[i] = A[I[i]];
return B;
}
#endif
#line 1 "library/other/io.hpp"
// based on yosupo's fastio
#include <unistd.h>
namespace fastio {
#define FASTIO
// クラスが read(), print() を持っているかを判定するメタ関数
struct has_write_impl {
template <class T>
static auto check(T &&x) -> decltype(x.write(), std::true_type{});
template <class T>
static auto check(...) -> std::false_type;
};
template <class T>
class has_write : public decltype(has_write_impl::check<T>(std::declval<T>())) {
};
struct has_read_impl {
template <class T>
static auto check(T &&x) -> decltype(x.read(), std::true_type{});
template <class T>
static auto check(...) -> std::false_type;
};
template <class T>
class has_read : public decltype(has_read_impl::check<T>(std::declval<T>())) {};
struct Scanner {
FILE *fp;
char line[(1 << 15) + 1];
size_t st = 0, ed = 0;
void reread() {
memmove(line, line + st, ed - st);
ed -= st;
st = 0;
ed += fread(line + ed, 1, (1 << 15) - ed, fp);
line[ed] = '\0';
}
bool succ() {
while (true) {
if (st == ed) {
reread();
if (st == ed) return false;
}
while (st != ed && isspace(line[st])) st++;
if (st != ed) break;
}
if (ed - st <= 50) {
bool sep = false;
for (size_t i = st; i < ed; i++) {
if (isspace(line[i])) {
sep = true;
break;
}
}
if (!sep) reread();
}
return true;
}
template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
bool read_single(T &ref) {
if (!succ()) return false;
while (true) {
size_t sz = 0;
while (st + sz < ed && !isspace(line[st + sz])) sz++;
ref.append(line + st, sz);
st += sz;
if (!sz || st != ed) break;
reread();
}
return true;
}
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
bool read_single(T &ref) {
if (!succ()) return false;
bool neg = false;
if (line[st] == '-') {
neg = true;
st++;
}
ref = T(0);
while (isdigit(line[st])) { ref = 10 * ref + (line[st++] & 0xf); }
if (neg) ref = -ref;
return true;
}
template <typename T,
typename enable_if<has_read<T>::value>::type * = nullptr>
inline bool read_single(T &x) {
x.read();
return true;
}
bool read_single(double &ref) {
string s;
if (!read_single(s)) return false;
ref = std::stod(s);
return true;
}
bool read_single(char &ref) {
string s;
if (!read_single(s) || s.size() != 1) return false;
ref = s[0];
return true;
}
template <class T>
bool read_single(vector<T> &ref) {
for (auto &d: ref) {
if (!read_single(d)) return false;
}
return true;
}
template <class T, class U>
bool read_single(pair<T, U> &p) {
return (read_single(p.first) && read_single(p.second));
}
template <size_t N = 0, typename T>
void read_single_tuple(T &t) {
if constexpr (N < std::tuple_size<T>::value) {
auto &x = std::get<N>(t);
read_single(x);
read_single_tuple<N + 1>(t);
}
}
template <class... T>
bool read_single(tuple<T...> &tpl) {
read_single_tuple(tpl);
return true;
}
void read() {}
template <class H, class... T>
void read(H &h, T &... t) {
bool f = read_single(h);
assert(f);
read(t...);
}
Scanner(FILE *fp) : fp(fp) {}
};
struct Printer {
Printer(FILE *_fp) : fp(_fp) {}
~Printer() { flush(); }
static constexpr size_t SIZE = 1 << 15;
FILE *fp;
char line[SIZE], small[50];
size_t pos = 0;
void flush() {
fwrite(line, 1, pos, fp);
pos = 0;
}
void write(const char val) {
if (pos == SIZE) flush();
line[pos++] = val;
}
template <class T, enable_if_t<is_integral<T>::value, int> = 0>
void write(T val) {
if (pos > (1 << 15) - 50) flush();
if (val == 0) {
write('0');
return;
}
if (val < 0) {
write('-');
val = -val; // todo min
}
size_t len = 0;
while (val) {
small[len++] = char(0x30 | (val % 10));
val /= 10;
}
for (size_t i = 0; i < len; i++) { line[pos + i] = small[len - 1 - i]; }
pos += len;
}
void write(const string s) {
for (char c: s) write(c);
}
void write(const char *s) {
size_t len = strlen(s);
for (size_t i = 0; i < len; i++) write(s[i]);
}
void write(const double x) {
ostringstream oss;
oss << fixed << setprecision(15) << x;
string s = oss.str();
write(s);
}
void write(const long double x) {
ostringstream oss;
oss << fixed << setprecision(15) << x;
string s = oss.str();
write(s);
}
template <typename T,
typename enable_if<has_write<T>::value>::type * = nullptr>
inline void write(T x) {
x.write();
}
template <class T>
void write(const vector<T> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) write(' ');
write(val[i]);
}
}
template <class T, class U>
void write(const pair<T, U> val) {
write(val.first);
write(' ');
write(val.second);
}
template <size_t N = 0, typename T>
void write_tuple(const T t) {
if constexpr (N < std::tuple_size<T>::value) {
if constexpr (N > 0) { write(' '); }
const auto x = std::get<N>(t);
write(x);
write_tuple<N + 1>(t);
}
}
template <class... T>
bool write(tuple<T...> tpl) {
write_tuple(tpl);
return true;
}
template <class T, size_t S>
void write(const array<T, S> val) {
auto n = val.size();
for (size_t i = 0; i < n; i++) {
if (i) write(' ');
write(val[i]);
}
}
void write(i128 val) {
string s;
bool negative = 0;
if (val < 0) {
negative = 1;
val = -val;
}
while (val) {
s += '0' + int(val % 10);
val /= 10;
}
if (negative) s += "-";
reverse(all(s));
if (len(s) == 0) s = "0";
write(s);
}
};
Scanner scanner = Scanner(stdin);
Printer printer = Printer(stdout);
void flush() { printer.flush(); }
void print() { printer.write('\n'); }
template <class Head, class... Tail>
void print(Head &&head, Tail &&... tail) {
printer.write(head);
if (sizeof...(Tail)) printer.write(' ');
print(forward<Tail>(tail)...);
}
void read() {}
template <class Head, class... Tail>
void read(Head &head, Tail &... tail) {
scanner.read(head);
read(tail...);
}
} // namespace fastio
using fastio::print;
using fastio::flush;
using fastio::read;
#define INT(...) \
int __VA_ARGS__; \
read(__VA_ARGS__)
#define LL(...) \
ll __VA_ARGS__; \
read(__VA_ARGS__)
#define STR(...) \
string __VA_ARGS__; \
read(__VA_ARGS__)
#define CHAR(...) \
char __VA_ARGS__; \
read(__VA_ARGS__)
#define DBL(...) \
double __VA_ARGS__; \
read(__VA_ARGS__)
#define VEC(type, name, size) \
vector<type> name(size); \
read(name)
#define VV(type, name, h, w) \
vector<vector<type>> name(h, vector<type>(w)); \
read(name)
void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
#line 3 "main.cpp"
#line 1 "library/nt/rational.hpp"
template <typename T = long long, bool REDUCE = true>
struct Rational {
T num, den;
Rational() : num(0), den(1) {}
Rational(T x) : num(x), den(1) {}
Rational(T a, T b, bool coprime = false) : num(a), den(b) {
if (!coprime && REDUCE) reduce();
}
static T gcd(T a, T b) {
a = max(a, -a), b = max(b, -b);
while (b) {
a %= b;
swap(a, b);
}
return a;
}
void reduce() {
if (!REDUCE) return;
T g = gcd(num, den);
num /= g, den /= g;
}
Rational &operator+=(const Rational &p) {
T g = (REDUCE ? gcd(den, p.den) : 1);
num = num * (p.den / g) + p.num * (den / g);
den *= p.den / g;
reduce();
return *this;
}
Rational &operator-=(const Rational &p) {
T g = (REDUCE ? gcd(den, p.den) : 1);
num = num * (p.den / g) - p.num * (den / g);
den *= p.den / g;
reduce();
return *this;
}
Rational &operator*=(const Rational &p) {
T g1 = (REDUCE ? gcd(num, p.den) : 1);
T g2 = (REDUCE ? gcd(den, p.num) : 1);
num = (num / g1) * (p.num / g2);
den = (den / g2) * (p.den / g1);
return *this;
}
Rational &operator/=(const Rational &p) {
T g1 = (REDUCE ? gcd(num, p.num) : 1);
T g2 = (REDUCE ? gcd(den, p.den) : 1);
num = (num / g1) * (p.den / g2);
den = (den / g2) * (p.num / g1);
if (den < 0) num = -num, den = -den;
return *this;
}
Rational operator-() const { return Rational(-num, den); }
Rational operator+(const Rational &p) const { return Rational(*this) += p; }
Rational operator-(const Rational &p) const { return Rational(*this) -= p; }
Rational operator*(const Rational &p) const { return Rational(*this) *= p; }
Rational operator/(const Rational &p) const { return Rational(*this) /= p; }
bool operator==(const Rational &p) const {
return num * p.den == p.num * den;
}
bool operator!=(const Rational &p) const {
return num * p.den != p.num * den;
}
bool operator<(const Rational &p) const { return num * p.den < p.num * den; }
bool operator>(const Rational &p) const { return num * p.den > p.num * den; }
bool operator<=(const Rational &p) const {
return num * p.den <= p.num * den;
}
bool operator>=(const Rational &p) const {
return num * p.den >= p.num * den;
}
string to_string() { return std::to_string(num) + "/" + std::to_string(den); }
double to_double() { return double(num) / double(den); }
};
#line 2 "library/geo/base.hpp"
template <typename T>
struct Point {
T x, y;
Point() = default;
template <typename A, typename B>
Point(A x, B y) : x(x), y(y) {}
template <typename A, typename B>
Point(pair<A, B> p) : x(p.fi), y(p.se) {}
Point operator+(Point p) const { return {x + p.x, y + p.y}; }
Point operator-(Point p) const { return {x - p.x, y - p.y}; }
bool operator==(Point p) const { return x == p.x && y == p.y; }
Point operator-() const { return {-x, -y}; }
bool operator<(Point p) const {
if (x != p.x) return x < p.x;
return y < p.y;
}
T dot(Point other) { return x * other.x + y * other.y; }
T det(Point other) { return x * other.y - y * other.x; }
void read() { fastio::read(x), fastio::read(y); }
void write() { fastio::printer.write(pair<T, T>({x, y})); }
};
// A -> B -> C と進むときに、左に曲がるならば +1、右に曲がるならば -1
template <typename T>
int ccw(Point<T> A, Point<T> B, Point<T> C) {
T x = (B - A).det(C - A);
if (x > 0) return 1;
if (x < 0) return -1;
return 0;
}
template <typename REAL, typename T>
REAL dist(Point<T> A, Point<T> B) {
A = A - B;
T p = A.dot(A);
return sqrt(REAL(p));
}
template <typename T>
struct Line {
T a, b, c;
Line(T a, T b, T c) : a(a), b(b), c(c) {}
Line(Point<T> A, Point<T> B) {
a = A.y - B.y, b = B.x - A.x, c = A.x * B.y - A.y * B.x;
}
Line(T x1, T y1, T x2, T y2) : Line(Point<T>(x1, y1), Point<T>(x2, y2)) {}
template <typename U>
U eval(Point<U> P) {
return a * P.x + b * P.y + c;
}
template <typename U>
T eval(U x, U y) {
return a * x + b * y + c;
}
bool is_parallel(Line other) { return a * other.b - b * other.a == 0; }
bool is_orthogonal(Line other) { return a * other.a + b * other.b == 0; }
};
template <typename T>
struct Segment {
Point<T> A, B;
Segment(Point<T> A, Point<T> B) : A(A), B(B) {}
Segment(T x1, T y1, T x2, T y2)
: Segment(Point<T>(x1, y1), Point<T>(x2, y2)) {}
template <enable_if_t<is_integral<T>::value, int> = 0>
bool contain(Point<T> C) {
T det = (C - A).det(B - A);
if (det != 0) return 0;
return (C - A).dot(B - A) >= 0 && (C - B).dot(A - B) >= 0;
}
Line<T> to_Line() { return Line(A, B); }
};
template <typename REAL>
struct Circle {
Point<REAL> O;
REAL r;
Circle(Point<REAL> O, REAL r) : O(O), r(r) {}
Circle(REAL x, REAL y, REAL r) : O(x, y), r(r) {}
template <typename T>
bool contain(Point<T> p) {
REAL dx = p.x - O.x, dy = p.y - O.y;
return dx * dx + dy * dy <= r * r;
}
};
template <typename T>
struct Polygon {
vc<Point<T>> points;
T a;
template <typename A, typename B>
Polygon(vc<pair<A, B>> pairs) {
for (auto&& [a, b]: pairs) points.eb(Point<T>(a, b));
build();
}
Polygon(vc<Point<T>> points) : points(points) { build(); }
int size() { return len(points); }
template <typename REAL>
REAL area() {
return a * 0.5;
}
template <enable_if_t<is_integral<T>::value, int> = 0>
T area_2() {
return a;
}
bool is_convex() {
FOR(j, len(points)) {
int i = (j == 0 ? len(points) - 1 : j - 1);
int k = (j == len(points) - 1 ? 0 : j + 1);
if ((points[j] - points[i]).det(points[k] - points[j]) < 0) return false;
}
return true;
}
private:
void build() {
a = 0;
FOR(i, len(points)) {
int j = (i + 1 == len(points) ? 0 : i + 1);
a += points[i].det(points[j]);
}
if (a < 0) {
a = -a;
reverse(all(points));
}
}
};
#line 2 "library/geo/convex_hull.hpp"
template <typename T>
vector<int> ConvexHull(vector<pair<T, T>>& XY, string mode = "full",
bool inclusive = false, bool sorted = false) {
assert(mode == "full" || mode == "lower" || mode == "upper");
ll N = XY.size();
if (N == 1) return {0};
if (N == 2) return {0, 1};
vc<int> I = argsort(XY);
auto check = [&](ll i, ll j, ll k) -> bool {
auto xi = XY[i].fi, yi = XY[i].se;
auto xj = XY[j].fi, yj = XY[j].se;
auto xk = XY[k].fi, yk = XY[k].se;
auto dx1 = xj - xi, dy1 = yj - yi;
auto dx2 = xk - xj, dy2 = yk - yj;
T det = dx1 * dy2 - dy1 * dx2;
return (inclusive ? det >= 0 : det > 0);
};
auto calc = [&]() {
vector<int> P;
for (auto&& k: I) {
while (P.size() > 1) {
auto i = P[P.size() - 2];
auto j = P[P.size() - 1];
if (check(i, j, k)) break;
P.pop_back();
}
P.eb(k);
}
return P;
};
vc<int> P;
if (mode == "full" || mode == "lower") {
vc<int> Q = calc();
P.insert(P.end(), all(Q));
}
if (mode == "full" || mode == "upper") {
if (!P.empty()) P.pop_back();
reverse(all(I));
vc<int> Q = calc();
P.insert(P.end(), all(Q));
}
if (mode == "upper") reverse(all(P));
if (len(P) >= 2 && P[0] == P.back()) P.pop_back();
return P;
}
template <typename T>
vector<int> ConvexHull(vector<Point<T>>& XY, string mode = "full",
bool inclusive = false, bool sorted = false) {
assert(mode == "full" || mode == "lower" || mode == "upper");
ll N = XY.size();
if (N == 1) return {0};
if (N == 2) return {0, 1};
vc<int> I = argsort(XY);
auto check = [&](ll i, ll j, ll k) -> bool {
auto xi = XY[i].x, yi = XY[i].y;
auto xj = XY[j].x, yj = XY[j].y;
auto xk = XY[k].x, yk = XY[k].y;
auto dx1 = xj - xi, dy1 = yj - yi;
auto dx2 = xk - xj, dy2 = yk - yj;
T det = dx1 * dy2 - dy1 * dx2;
return (inclusive ? det >= 0 : det > 0);
};
auto calc = [&]() {
vector<int> P;
for (auto&& k: I) {
while (P.size() > 1) {
auto i = P[P.size() - 2];
auto j = P[P.size() - 1];
if (check(i, j, k)) break;
P.pop_back();
}
P.eb(k);
}
return P;
};
vc<int> P;
if (mode == "full" || mode == "lower") {
vc<int> Q = calc();
P.insert(P.end(), all(Q));
}
if (mode == "full" || mode == "upper") {
if (!P.empty()) P.pop_back();
reverse(all(I));
vc<int> Q = calc();
P.insert(P.end(), all(Q));
}
if (mode == "upper") reverse(all(P));
if (len(P) >= 2 && P[0] == P.back()) P.pop_back();
return P;
}
#line 1 "library/geo/convex_polygon.hpp"
template <typename T>
struct ConvexPolygon {
using P = Point<T>;
int n;
vc<P> point;
ConvexPolygon(vc<P> point) : n(len(point)), point(point) { assert(n >= 1); }
// 比較関数 comp(i,j)
template <typename F>
int periodic_min_comp(F comp) {
int L = 0, M = n, R = n + n;
while (1) {
if (R - L == 2) break;
int L1 = (L + M) / 2, R1 = (M + R + 1) / 2;
if (comp(L1, M)) { R = M, M = L1; }
elif (comp(R1, M)) { L = M, M = R1; }
else {
L = L1, R = R1;
}
}
return M % n;
}
int nxt_idx(int i) { return (i + 1 == n ? 0 : i + 1); }
int prev_idx(int i) { return (i == 0 ? n - 1 : i - 1); }
// 中:1, 境界:0, 外:-1
// int side(P p) {}
pair<int, T> min_dot(P p) {
int idx = periodic_min_comp([&](int i, int j) -> bool {
return point[i % n].dot(p) < point[j % n].dot(p);
});
return {idx, point[idx].dot(p)};
}
pair<int, T> max_dot(P p) {
int idx = periodic_min_comp([&](int i, int j) -> bool {
return point[i % n].dot(p) > point[j % n].dot(p);
});
return {idx, point[idx].dot(p)};
}
// pair<int, int> visible_range(P p) {}
};
#line 7 "main.cpp"
using Re = double;
using QQ = Rational<i128, false>;
using Poly = ConvexPolygon<QQ>;
void solve() {
LL(N);
VEC(pi, AB, N);
ll O = 0;
{
vc<pi> tmp;
for (auto&& [a, b]: AB) {
if (a == 0 && b == 0) {
++O;
} else {
tmp.eb(a, b);
}
}
swap(AB, tmp);
N = len(AB);
}
using P = Point<QQ>;
vc<P> point(N);
FOR(i, N) {
auto [a, b] = AB[i];
point[i].x = QQ{a, a * a + b * b};
point[i].y = QQ{b, a * a + b * b};
}
using Poly = ConvexPolygon<QQ>;
vc<Poly> hull;
FOR(5) {
N = len(point);
if (point.empty()) break;
vc<int> I = ConvexHull<QQ>(point, "full");
vc<bool> use(N);
for (auto&& i: I) use[i] = 1;
vc<P> polygon = rearrange(point, I);
vc<P> rest;
FOR(i, N) {
if (!use[i]) rest.eb(point[i]);
}
hull.eb(ConvexPolygon<QQ>(polygon));
swap(point, rest);
}
auto solve = [&](ll x, ll y, ll k) -> void {
if (x == 0 && y == 0) return print(-1);
if (k < O) { return print(0.0); }
k -= O;
vc<Re> ts;
Point<QQ> p = {QQ(x), QQ(y)};
for (auto&& poly: hull) {
int idx = poly.max_dot(p).fi;
vc<int> I;
int i = idx;
FOR(5) {
I.eb(i);
i = poly.prev_idx(i);
}
i = idx;
FOR(5) {
I.eb(i);
i = poly.nxt_idx(i);
}
UNIQUE(I);
for (auto&& idx: I) {
QQ dot = p.dot(poly.point[idx]);
if (dot <= 0) continue;
ts.eb(dot.to_double());
}
}
sort(all(ts));
reverse(all(ts));
if (len(ts) <= k) { return print(-1); }
Re ANS = 1.0 / ts[k];
Re d = sqrtl(x * x + y * y);
ANS *= d / 2;
print(ANS);
};
LL(Q);
FOR(Q) {
LL(x, y, k);
--k;
solve(x, y, k);
}
}
signed main() {
solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 2ms
memory: 3664kb
input:
5 5 -3 5 4 -6 2 -5 0 4 1 2 -3 -10 1 6 -9 1
output:
8.700255424092125 3.226019562257254
result:
ok 2 numbers
Test #2:
score: 0
Accepted
time: 1ms
memory: 3896kb
input:
8 4 -1 4 -8 0 9 4 -7 -5 -2 5 -5 7 5 -9 2 10 4 -8 1 7 -7 5 -10 8 2 -9 9 2 4 -7 5 -1 -10 2 6 -3 2 2 -9 3 -10 -10 1 5 9 1
output:
3.167762968124702 26.162950903902257 5.461488320163313 6.363961030678928 -1 5.289408221642574 3.726779962499650 4.609772228646444 2.929442379201411 4.761728940206488
result:
ok 10 numbers
Test #3:
score: 0
Accepted
time: 2ms
memory: 3676kb
input:
5 -4 -7 5 0 2 4 -7 -7 4 4 20 0 -5 2 -4 -7 2 -7 7 3 4 -4 3 -7 4 3 4 -4 1 2 4 1 6 -7 2 4 -4 2 4 4 3 5 4 1 -1 9 2 8 9 3 4 -4 2 6 3 3 -10 -3 2 -7 7 1 9 -4 1 -4 -7 3 -2 0 2
output:
7.000000000000000 5.130527658008168 -1 -1 -1 3.535533905932738 2.236067977499790 11.985407794480752 15.320646925708528 3.535533905932738 2.462740091320326 4.527692569068709 3.762998305872593 15.320646925708528 2.981423969999720 5.621703504797988 7.071067811865476 2.735793833832251 -1 8.125000000000000
result:
ok 20 numbers
Test #4:
score: 0
Accepted
time: 3ms
memory: 3608kb
input:
100 63 -48 20 -62 -81 -31 -17 -93 2 -74 72 25 -71 37 -71 17 56 67 -47 65 -89 14 62 30 -71 -33 14 -53 -57 -52 30 80 -14 -69 -45 -19 -54 -71 58 -20 -57 12 5 -56 -76 -2 26 61 24 60 10 -97 -63 38 17 81 -43 -38 44 35 -86 37 62 72 77 11 41 29 14 81 77 55 -54 -33 -43 -51 76 14 55 47 43 24 69 -13 16 75 11 9...
output:
26.758678868757293 29.571405997861689 24.622144504490262 27.771745654730640 26.678366712896512 24.423702460472160 28.893348196396307 29.776169557758458 31.940362970515167 27.214901602377861 31.728095045748496 27.071160551681185 25.299110030617502 26.871065152124928 28.995839453427891 28.356314246197...
result:
ok 100 numbers
Test #5:
score: 0
Accepted
time: 68ms
memory: 5656kb
input:
10000 -3 3 -6 2 -4 1 -2 -5 5 -6 -7 -2 0 7 1 -4 8 0 -4 4 -6 -2 5 0 2 9 -4 -8 0 -8 7 4 -7 2 3 3 4 1 -1 7 -4 -2 6 0 3 -5 -7 2 0 -9 7 0 7 3 -6 0 1 7 6 2 2 -9 1 8 3 -3 2 -9 4 2 4 -5 6 0 -3 6 7 3 0 8 0 -4 7 0 -5 8 5 -5 -5 -1 0 9 -4 -3 -9 -1 7 -2 -7 -2 4 0 -6 6 -3 4 6 7 2 5 -8 -5 0 5 4 0 0 -4 0 -6 -5 3 -5 ...
output:
2.154917004616741 2.167265935742733 2.067643085494710 2.111841978749802 2.111841978749802 2.111841978749801 2.124987278610405 2.121320343559642 2.027587510099406 2.092882282881672 2.141537214391802 2.061552812808830 2.154917004616741 2.000000000000000 2.121320343559642 2.167265935742733 2.0676430854...
result:
ok 10000 numbers
Test #6:
score: -100
Time Limit Exceeded
input:
10000 -90174 318421 -37261 138897 -260388 -302590 -906833 35071 317743 -283220 390311 -85301 880987 325969 -315218 -116767 103089 -8223 -134988 -973121 -444593 229407 -552060 549321 265624 -337609 -264546 322379 28687 110143 467764 303005 -335748 32188 213125 274156 240105 751 -81255 -129323 148563 ...