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
#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second
#define _ ios_base::sync_with_stdio(0); cin.tie(0);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;

const int nax = 500 * 1000 + 10;
int n;
vi t[2];
vector<pi> A[2];

int main() {_
	cin >> n;
	for(int a,b,c, i = 1; i <= n; ++i) {
		cin >> a >> b >> c;
		a--;
		b -= c;
		t[a].PB(b);
	}
	for(int k : {0, 1}) {
		if((int)t[k].size() == 0) continue;
		sort(t[k].begin(), t[k].end());
		int cnt = 1;
		for(int i = 1; i < (int)t[k].size(); ++i) {
			if(t[k][i] != t[k][i - 1]) {
				A[k].PB({t[k][i - 1], cnt});
				cnt = 0;
			}
			cnt++;
		}
		A[k].PB({t[k].back(), cnt});
	}
	int ptr = 0;
	int ans = 0;
	for(auto it : A[0]) {
		while(ptr < (int)A[1].size() && A[1][ptr].ST < it.ST) {
			ptr++;
		}
		if(ptr < (int)A[1].size() && A[1][ptr].ST == it.ST) {
			ans += min(A[1][ptr].ND, it.ND);
		}
	}
	cout << ans;
		
	
}