結果

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

ソースコード

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{0}, B{0}, C{0};
    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{b, 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::sort(C.begin(), C.end());
    int ans = 0;
    for (const auto& b : B) {
        for (const auto& c : C) {
            if (b + c > Rational(1)) { break; }
            ans += std::upper_bound(A.begin(), A.end(), Rational(1) - std::max(b, c)) - A.begin();
            ans -= std::upper_bound(A.begin(), A.end(), Rational(1) - b - c) - std::lower_bound(A.begin(), A.end(), Rational(1) - b - c);
        }
    }
    std::cout << ans << std::endl;
    return 0;
}
0