結果

問題 No.461 三角形はいくつ?
ユーザー mamekin
提出日時 2017-06-12 22:23:02
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 4,217 bytes
コンパイル時間 1,174 ms
コンパイル使用メモリ 111,068 KB
実行使用メモリ 193,348 KB
最終ジャッジ日時 2024-09-24 16:53:00
合計ジャッジ時間 46,339 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 4 WA * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

template <class T1>
class Operators
{
public:
    template <class T2>
    const T1 operator+(const T2& right) const{
        T1 ans = static_cast<const T1&>( *this );
        ans += right;
        return ans;
    }
    template <class T2>
    const T1 operator-(const T2& right) const{
        T1 ans = static_cast<const T1&>( *this );
        ans -= right;
        return ans;
    }
    template <class T2>
    const T1 operator*(const T2& right) const{
        T1 ans = static_cast<const T1&>( *this );
        ans *= right;
        return ans;
    }
    template <class T2>
    const T1 operator/(const T2& right) const{
        T1 ans = static_cast<const T1&>( *this );
        ans /= right;
        return ans;
    }
    bool operator!=(const T1& right) const{
        const T1& left = static_cast<const T1&>( *this );
        return !(left == right);
    }
    bool operator>(const T1& right) const{
        const T1& left = static_cast<const T1&>( *this );
        return right < left;
    }
    bool operator<=(const T1& right) const{
        const T1& left = static_cast<const T1&>( *this );
        return !(right < left);
    }
    bool operator>=(const T1& right) const{
        const T1& left = static_cast<const T1&>( *this );
        return !(left < right);
    }
};

class Fraction : public Operators<Fraction>
{
private:
    long long n; // 分子(numerator)
    long long d; // 分母(denominator)

    // 約分
    void reduce(){
        if(d < 0){
            n *= -1;
            d *= -1;
        }
        long long a = abs(n);
        long long b = d;
        while(b != 0){
            long long tmp = a % b;
            a = b;
            b = tmp;
        }
        n /= a;
        d /= a;
    }
public:
    Fraction(){
        n = 0;
        d = 1;
    }
    Fraction(long long n0){
        n = n0;
        d = 1;
    }
    Fraction(long long n0, long long d0){
        n = n0;
        d = d0;
        reduce();
    }
    pair<long long, long long> getValue() const{
        return make_pair(n, d);
    }
    Fraction& operator+=(const Fraction& f){
        n = n * f.d + d * f.n;
        d *= f.d;
        reduce();
        return *this;
    }
    Fraction& operator-=(const Fraction& f){
        n = n * f.d - d * f.n;
        d *= f.d;
        reduce();
        return *this;
    }
    Fraction& operator*=(const Fraction& f){
        n *= f.n;
        d *= f.d;
        reduce();
        return *this;
    }
    Fraction& operator/=(const Fraction& f){
        n *= f.d;
        d *= f.n;
        reduce();
        return *this;
    }
    bool operator==(const Fraction& f) const{
        return n == f.n && d == f.d;
    }
    bool operator<(const Fraction& f) const{
        return n * f.d < f.n * d;
    }
};

int main()
{
    int n;
    cin >> n;
    vector<vector<Fraction> > seg(3);
    for(int i=0; i<n; ++i){
        int p, a, b;
        cin >> p >> a >> b;
        seg[p].push_back(Fraction(a, a + b));
    }

    vector<Fraction> v1, v2;
    v1.push_back(0);
    for(const Fraction& a : seg[1]){
        v1.push_back(Fraction(1) - a);
        for(const Fraction& b : seg[2]){
            v1.push_back(Fraction(1) - b);
            Fraction f = Fraction(2) - a - b;
            if(f <= 1){
                v1.push_back(f);
                v2.push_back(f);
            }
        }
    }
    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());
    
    seg[0].push_back(1);
    sort(seg[0].begin(), seg[0].end());
    int ans = 0;
    unsigned a = 0;
    unsigned b = 0;
    for(const Fraction& f : seg[0]){
        while(a < v1.size() && v1[a] < f)
            ++ a;
        while(b < v2.size() && v2[b] <= f)
            ++ b;
        ans += a + (v2.size() - b);
    }
    cout << ans << endl;

    return 0;
}
0