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
#include <iostream>
#include <vector>
#include <algorithm>


template<typename T>
std::ostream& operator<<(std::ostream& out, std::vector<T>& v) {
    out << "vector of size " << v.size() << ":";
    for(int i=0; i < v.size(); i++)
        out << " " << v[i];
    return out;
}

int main() {
    std::ios::sync_with_stdio(false);


    int n;
    std::cin >> n;
    std::vector<int> vertical;
    std::vector<int> horizontal;
    for(int i = 0; i < n; i++) {
        int r, w, t;
        std::cin >> r >> w >> t;
        if(r == 1) {
            vertical.push_back(w-t);
        } else {
            horizontal.push_back(w-t);
        }
    }
    std::sort(vertical.begin(), vertical.end());
    std::sort(horizontal.begin(), horizontal.end());

    //std::cout << vertical << std::endl;
    //std::cout << horizontal << std::endl;

    int removed = 0;

    int v_pos = 0, h_pos = 0;

    while(true) {
        if(v_pos >= vertical.size() || h_pos >= horizontal.size())
            break;
        if(vertical[v_pos] < horizontal[h_pos]) {
            v_pos++;
            continue;
        } else if(vertical[v_pos] > horizontal[h_pos]) {
            h_pos++;
            continue;
        } else {
            int v_count = 0;
            int h_count = 0;
            int collision = vertical[v_pos];

            while(v_pos < vertical.size() && vertical[v_pos] == collision) {
                v_pos++;
                v_count++;
            }
            while(h_pos < horizontal.size() && horizontal[h_pos] == collision) {
                h_pos++;
                h_count++;
            }
            removed += std::min(v_count, h_count);
        }
    }

    std::cout << removed << std::endl;

    return 0;
}