#include "bits/stdc++.h" using namespace std; #define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i)) #define rep(i,j) FOR(i,0,j) #define each(x,y) for(auto &(x):(y)) #define mp make_pair #define mt make_tuple #define all(x) (x).begin(),(x).end() #define debug(x) cout<<#x<<": "<<(x)< pii; typedef vector vi; typedef vector vll; long long gcd(long long a, long long b){ return b?gcd(b, a%b):a; } long long lcm(long long a, long long b){ return a*b/gcd(a,b); } bool les(pair a, pair b) { // a.f/a.s < b.f/b.s // a.f*b.s < b.f * a.s return a.first*b.second < b.first*a.second; } void adjust(ll &a1, ll &b1, ll &a2, ll &b2) { ll s1 = a1 + b1; ll s2 = a2 + b2; ll l = lcm(s1, s2); a1 *= l / s1; b1 *= l / s1; a2 *= l / s2; b2 *= l / s2; } bool check(ll a1, ll b1, ll b2, ll a3, ll b3) { return a3*(a1 - b2) > b3*(b1 + b2); } bool check2(ll a1, ll b1, ll a2, ll b2, ll a3, ll b3) { return a1*a3 >= b1*b2 && a2*a3 >= b2*b3; } bool check3(ll a1, ll b1, ll b2, ll a3, ll b3) { return a1-b2 >= 0 && a3*(a1 - b2) < b3*(b1 + b2); } int main(){ ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; ll a1, a2, a3, b1, b2, b3; // (1) もとの三角形 ll ans = 1; // (2) 追加辺ともとの三角形 ans += N; vll P(N), A(N), B(N); vector>> p2ab(3); rep(i, N) { cin >> P[i] >> A[i] >> B[i]; ll g = gcd(A[i], B[i]); A[i] /= g; B[i] /= g; p2ab[P[i]].push_back(mp(A[i], B[i])); } rep(i, 3)sort(all(p2ab[i])); // (3) もとの三角形+追加辺*1 rep(i, 3) { each(p, p2ab[i]) { each(q, p2ab[(i + 1) % 3]) { tie(a1, b1) = p; tie(a2, b2) = q; if(a1*a2>b1*b2) { ans++; } } } } // (4) 追加3辺(上向き) if(sz(p2ab[2])) each(p, p2ab[0]) { each(q, p2ab[1]) { tie(a1, b1) = p; tie(a2, b2) = q; if(a1*a2>b1*b2) { adjust(a1, b1, a2, b2); int lb = -1, ub = sz(p2ab[2]) - 1, m; tie(a3, b3) = p2ab[2][ub]; if(!check(a1, b1, b2, a3, b3))continue; while(ub - lb > 1) { m = (ub + lb) / 2; tie(a3, b3) = p2ab[2][m]; (check(a1, b1, b2, a3, b3) ? ub : lb) = m; } ans += sz(p2ab[2]) - ub; } } } // (5) 追加3辺(下向き) if(sz(p2ab[2])) each(p, p2ab[0]) { each(q, p2ab[1]) { tie(a1, b1) = p; tie(a2, b2) = q; if(a1*a2 >= b1*b2) { adjust(a1, b1, a2, b2); each(r, p2ab[2]) { tie(a3, b3) = r; if(check2(a1, b1, a2, b2, a3, b3) && check3(a1, b1, b2, a3, b3)) { ++ans; } } } } } cout << ans << endl; }