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

using namespace std;

class car;

typedef list<car> cList;
typedef cList::iterator cListIt;
//typedef cList::const_iterator cListCIt;
typedef map<car,map<car,int> > cMapMap;
typedef map<car,map<car,int> >::iterator cMapMapIt;
typedef pair<car,int> ciP;

class car
{
public:
int X,Y,T;

	car() {};
	car(int x, int y, int t) {X = x; Y = y; T = t;}
car& operator=(const car& s) {X = s.X; Y = s.Y; T = s.T; return *this;}
int operator<(const car &s) const { if (X+Y == s.X+s.Y) return T < s.T; else return X+Y < s.X+s.Y; }
};

int main()
{
int n,N,r,w,t,ret;
cList X,Y;
cMapMap Hm;
cMapMapIt Hmi,Hmi_m;

	scanf("%d",&N);
	X.clear();
	Y.clear();
	for (n = 0; n < N; n++)
	{
		scanf("%d %d %d",&r,&w,&t);
		
		if (r == 1) X.push_back(car(w,0,t));
		else Y.push_back(car(0,w,t));
	}
	
	Hm.clear();
	for (cListIt x = X.begin(); x != X.end(); ++x)
	{
		for (cListIt y = Y.begin(); y != Y.end(); ++y)
		{
			if (x->X - x->T == y->Y - y->T) 
			{
				Hm[car(x->X,0,x->T)].insert(ciP(car(0,y->Y,y->T),0));
				Hm[car(0,y->Y,y->T)].insert(ciP(car(x->X,0,x->T),0));
//				printf("Hit! (%d,%d,%d) x (%d,%d,%d) @ (%d,%d)\n",x->X,x->Y,x->T,y->X,y->Y,y->T,x->X,y->Y);
			}
		}
	}
	
//	printf("%d %d\n",Hm.size(),0);
//	for (Hmi = Hm.begin(); Hmi != Hm.end(); ++Hmi) printf("(%d,%d,%d) x %d\n",Hmi->first.X,Hmi->first.Y,Hmi->first.T,Hmi->second.size());
	
	ret = 0;
	while (!Hm.empty())
	{
		Hmi_m = Hm.begin();
		for (Hmi = Hm.begin(); Hmi != Hm.end(); ++Hmi)
		{
			if (Hmi_m->second.size() < Hmi->second.size()) Hmi_m = Hmi;
		}
		for (map<car,int>::iterator Hmi_d = Hm[Hmi_m->first].begin(); Hmi_d != Hm[Hmi_m->first].end(); ++Hmi_d)
		{
//			printf("Deleting rev dependencies...(%d,%d)\n",Hmi_d->first.X,Hmi_d->first.Y);
			Hm[Hmi_d->first].erase(Hmi_m->first);
			if (Hm[Hmi_d->first].empty()) 
			{
				Hm.erase(Hmi_d->first);
//				printf("Deleting dependencies...(%d,%d)\n",Hmi_d->first.X,Hmi_d->first.Y);
			}
		}
//		printf("Deleting...(%d,%d)\n",Hmi_m->first.X,Hmi_m->first.Y);
		Hm.erase(Hmi_m);
		ret++;
	}

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

	return 0;
}