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

using namespace std;

int n;

typedef struct {
        int w;
        int t;
        int d;
} car;

bool cmp_car(const car &a, const car &b)
{
        if (a.d != b.d) {
                return a.d < b.d;
        }
        return a.t < b.t;
}

const int max_n = 500001;

vector<car> a;
vector<car> b;

int calc_result()
{
        auto ai = a.begin();
        auto bi = b.begin();
        int result = 0;

        while(ai != a.end() && bi != b.end()) {
                int val, cnt1, cnt2;
                if (ai->d < bi->d) {
                        ai++;
                        continue;
                }
                if (bi->d < ai->d) {
                        bi++;
                        continue;
                }
                // ai.d = bi.d
                val = ai->d;
                cnt1=0;
                while( ai != a.end() && ai->d == val) {
                        ai++;
                        cnt1++;
                }
                cnt2=0;
                while( bi != b.end() && bi->d == val) {
                        bi++;
                        cnt2++;
                }
                result += min(cnt1, cnt2);
        }
        return result;
}

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

        for(int i = 0; i < n; ++i) {
                int r;
                car c;

                scanf("%d %d %d", &r, &(c.w), &(c.t));

                c.d = c.t - c.w;

                if (r == 1) {
                        a.push_back(c);
                } else {
                        b.push_back(c);
                };
        }

        sort(a.begin(), a.end(), cmp_car); 
        sort(b.begin(), b.end(), cmp_car); 

        printf("%d\n", calc_result());

        return 0;
}