1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <bits/stdc++.h>
#include <unistd.h>
#include <sstream>
using namespace std;
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) cerr << "TRACE(" #x ")" << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << endl;
typedef long long LL; typedef unsigned long long ULL;


#define BIGMOD 1000012177LL

struct Car {
    int wi, ti;
    int Type;

    int collisionCount;

    Car(int _Type, int wi, int ti, int collisionCount) : Type(_Type), wi(wi), ti(ti), collisionCount(collisionCount) {}

    string toString() {
        ostringstream s;
        s << "Car(" << Type << "," << wi << ",t=" << ti << ",cs=" << collisionCount << ")";
        return s.str();
    }
};

int main() {
    int n;
    
    map<int, int> carsOnVerticalLane;
    map<int, int> carsOnHorizontalLane;
    vector<Car> cars;
    scanf("%d", &n);
    for (int i=0; i < n; i++)
    {
        int t, wi, ti;
        scanf("%d%d%d", &t, &wi, &ti);
        cars.emplace_back(Car(t, wi, ti, 0));
        int diff = wi - ti;
        if (t == 1)
        {
            if (carsOnVerticalLane.find(diff) == carsOnVerticalLane.end())
            {
                carsOnVerticalLane[diff] = 0;
            }
            carsOnVerticalLane[diff]++;  
        }
        if (t == 2)
        {
            if (carsOnHorizontalLane.find(diff) == carsOnHorizontalLane.end())
            {
                carsOnHorizontalLane[diff] = 0;
            }
            carsOnHorizontalLane[diff]++;  
        }
    }
    /*printf("printing cars\n");
    LL allCollisions = 0;
    for (int i=0; i < cars.size(); i++)
    {
        Car &c = cars[i];
        int diff = c.wi - c.ti;
        if (c.Type == 1)
        {
            if (carsOnHorizontalLane.find(diff) != carsOnHorizontalLane.end())
            {
                c.collisionCount = carsOnHorizontalLane[diff];
            }
        }
        if (c.Type == 2)
        {
            if (carsOnVerticalLane.find(diff) != carsOnVerticalLane.end())
            {
                c.collisionCount = carsOnVerticalLane[diff];
            }
        }
        allCollisions += c.collisionCount;
        printf("%s\n", c.toString().c_str());
    }*/

    int res = 0;
    for (auto it : carsOnVerticalLane)
    {
        int lane = it.first;
        int verticalCarsCount = it.second;
        int horizontalCarsCount = carsOnHorizontalLane[lane];

        //printf("lane=%d verticalCars=%d horizontalCars=%d\n", lane, verticalCarsCount, horizontalCarsCount);

        res += min(verticalCarsCount, horizontalCarsCount);
    }
    printf("%d\n", res);
    return 0;
}