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

using namespace std;

int n, r, w, t, it1=0, it2=0, r1, r2, temp, wyn = 0, sum1 = 0, sum2 = 0;
int p1[500001], p2[500001];

int main(){
    ios_base::sync_with_stdio(false);

    cin>>n;
    for(int i=0; i<n; ++i){
        cin>>r>>w>>t;
        if(r == 1){
            p1[it1] = w-t;
            it1++;
        }else{
            p2[it2] = w-t;
            it2++;
        }
    }
    std::sort(&p1[0], &p1[it1]);
    std::sort(&p2[0], &p2[it2]);
    r1 = it1;
    r2 = it2;
    it1 = 0;
    it2 = 0;
    while(true){
        if(it1 >= r1 || it2 >= r2){
            break;
        }
        if(p1[it1] < p2[it2]){
            it1++;
            continue;
        }
        if(p1[it1] > p2[it2]){
            it2++;
            continue;
        }
        if(p1[it1] == p2[it2]){
            temp = p1[it1];
            while(true){
                if(p1[it1] == temp && it1 < r1){
                    sum1++;
                    it1++;
                }else{
                    break;
                }
            }
            while(true){
                if(p2[it2] == temp && it2 < r2){
                    sum2++;
                    it2++;
                }else{
                    break;
                }
            }
            if(sum1 > sum2){
                wyn += sum2;
            }else{
                wyn += sum1;
            }
            sum1 = 0;
            sum2 = 0;
        }
    }
    cout<<wyn;

    return 0;
}