結果

問題 No.461 三角形はいくつ?
ユーザー imulanimulan
提出日時 2016-12-12 07:24:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,962 ms / 5,000 ms
コード長 2,249 bytes
コンパイル時間 2,101 ms
コンパイル使用メモリ 172,668 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-19 20:05:09
合計ジャッジ時間 33,691 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 918 ms
4,376 KB
testcase_06 AC 530 ms
4,376 KB
testcase_07 AC 613 ms
4,376 KB
testcase_08 AC 415 ms
4,380 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 131 ms
4,380 KB
testcase_11 AC 705 ms
4,376 KB
testcase_12 AC 316 ms
4,380 KB
testcase_13 AC 1,035 ms
4,376 KB
testcase_14 AC 1,004 ms
4,380 KB
testcase_15 AC 1,040 ms
4,380 KB
testcase_16 AC 12 ms
4,376 KB
testcase_17 AC 12 ms
4,376 KB
testcase_18 AC 1,962 ms
4,376 KB
testcase_19 AC 1,956 ms
4,380 KB
testcase_20 AC 1,061 ms
4,380 KB
testcase_21 AC 1,068 ms
4,376 KB
testcase_22 AC 1,055 ms
4,376 KB
testcase_23 AC 121 ms
4,376 KB
testcase_24 AC 113 ms
4,380 KB
testcase_25 AC 1,415 ms
4,376 KB
testcase_26 AC 1,415 ms
4,380 KB
testcase_27 AC 1,413 ms
4,380 KB
testcase_28 AC 1,418 ms
4,380 KB
testcase_29 AC 1,593 ms
4,376 KB
testcase_30 AC 1,515 ms
4,376 KB
testcase_31 AC 1,715 ms
4,376 KB
testcase_32 AC 1,794 ms
4,380 KB
testcase_33 AC 3 ms
4,380 KB
testcase_34 AC 4 ms
4,376 KB
testcase_35 AC 3 ms
4,376 KB
testcase_36 AC 251 ms
4,380 KB
testcase_37 AC 236 ms
4,376 KB
testcase_38 AC 248 ms
4,376 KB
testcase_39 AC 215 ms
4,376 KB
testcase_40 AC 250 ms
4,376 KB
testcase_41 AC 249 ms
4,384 KB
testcase_42 AC 790 ms
4,380 KB
testcase_43 AC 805 ms
4,376 KB
testcase_44 AC 803 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

struct line
{
    ll a,b;

    bool operator<(const line& t) const {
        return a*(t.a+t.b) < t.a*(a+b);
    }
};

inline int intersect_inside(const line& m, const line& n)
{
    if(m.a*(n.a+n.b) + n.a*(m.a+m.b) > (m.a+m.b)*(n.a+n.b)) return 1;
    return 0;
}

inline int intersect_onedge(const line& m, const line& n)
{
    if(m.a*(n.a+n.b) + n.a*(m.a+m.b) == (m.a+m.b)*(n.a+n.b)) return 1;
    return 0;
}

inline line crosspoint(const line& pp, const line& qq)
{
    ll D=pp.a*(qq.a+qq.b) + qq.a*(pp.a+pp.b);
    ll C=(pp.a+pp.b)*(qq.a+qq.b);
    ll A=2*C-D, B=D-C;

    ll G=__gcd(A,B);
    A/=G;
    B/=G;
    return line{A,B};
}

int main()
{
    int n;
    scanf(" %d", &n);

    vector<line> l[3];
    rep(i,n)
    {
        int p;
        ll a,b;
        scanf(" %d %lld %lld", &p, &a, &b);

        ll G=__gcd(a,b);
        a/=G;
        b/=G;

        l[p].pb(line{a,b});
    }

    rep(i,3) sort(all(l[i]));

    ll two=0,three=0;

    rep(i,3)
    {
        int P=i, Q=(i+1)%3, R=(i+2)%3;
        rep(j,l[P].size())rep(k,l[Q].size())
        {
            line pp=l[P][j], qq=l[Q][k];

            two+=intersect_inside(pp,qq);

            if(intersect_inside(pp,qq) || intersect_onedge(pp,qq))
            {
                line base = max(line{pp.b,pp.a}, line{qq.b,qq.a});
                // printf("base (%d, %d)\n", base.a, base.b);

                int idx = lower_bound(all(l[R]),base) - l[R].begin();
                three += (int)l[R].size()-idx;

                // printf("idx = %d : (%d, %d)\n", idx,l[R][idx].a, l[R][idx].b);

                line cr = crosspoint(pp,qq);
                if(cr.a>0 && cr.b>0 && cr.a+cr.b<=400000)
                {
                    int c_idx = lower_bound(all(l[R]),cr) - l[R].begin();
                    if(c_idx<l[R].size() && l[R][c_idx].a==cr.a && l[R][c_idx].b==cr.b) --three;
                }
            }
        }
    }

    printf("%lld\n", 1+n+two+three/3);
    return 0;
}
0