結果

問題 No.461 三角形はいくつ?
ユーザー Pachicobue
提出日時 2018-09-15 16:10:01
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,939 bytes
コンパイル時間 2,711 ms
コンパイル使用メモリ 208,016 KB
最終ジャッジ日時 2025-01-06 13:26:36
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 39 TLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define show(x) std::cerr << #x << " = " << x << std::endl
using ll = long long;
template <typename T>
constexpr T gcd(const T a, const T b) { return (b != 0) ? gcd(b, a % b) : a; }
template <typename T>
constexpr T lcm(const T a, const T b) { return (a / gcd(a, b)) * b; }
class Rational
{
public:
    using T = ll;
    Rational() = default;
    Rational(const T N) : N{N} {}
    Rational(const T N_, const T D_) : N{N_}, D{D_} { N /= gcd(N_, D_), D /= gcd(N_, D_); }
    Rational operator+(const Rational& r) const { return Rational{lcm(D, r.D) / D * N + lcm(D, r.D) / r.D * r.N, lcm(D, r.D)}; }
    Rational operator-(const Rational& r) const { return Rational{lcm(D, r.D) / D * N - lcm(D, r.D) / r.D * r.N, lcm(D, r.D)}; }
    bool operator==(const Rational& r) const { return N * r.D == D * r.N; }
    bool operator<(const Rational& r) const { return N * r.D < D * r.N; }
    bool operator>(const Rational& r) const { return N * r.D > D * r.N; }

private:
    T N = 0, D = 1;
};
int main()
{
    int N;
    std::cin >> N;
    std::vector<Rational> A{1}, B{1}, C{1};
    for (int i = 0; i < N; i++) {
        int p, a, b;
        std::cin >> p >> a >> b;
        (p == 0 ? A : p == 1 ? B : C).push_back(Rational{a, a + b});
    }
    if (A.size() < B.size()) { std::swap(A, B); }
    if (A.size() < C.size()) { std::swap(A, C); }
    std::sort(A.begin(), A.end()), std::sort(B.begin(), B.end(), std::greater<Rational>{}), std::sort(C.begin(), C.end(), std::greater<Rational>{});
    int ans = 0;
    for (const auto& b : B) {
        for (const auto& c : C) {
            if (b + c < Rational(1)) { break; }
            ans += A.end() - std::lower_bound(A.begin(), A.end(), Rational(1) - std::min(b, c));
            ans -= std::upper_bound(A.begin(), A.end(), Rational(2) - b - c) - std::lower_bound(A.begin(), A.end(), Rational(2) - b - c);
        }
    }
    std::cout << ans << std::endl;
    return 0;
}
0