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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
int n,m,a,b,c;
const bool pr = false;
bool total_random = false;
static unsigned int g_seed;
inline int fastrand() { 
  g_seed = (214013*g_seed+2531011); 
  return (g_seed>>16)&0x7FFF; 
} 
inline int fastrand_z(const int &x) {
	return fastrand() % x;
}
int INCR;
#define pr 0
struct wp {
	int a,b,c;
	#if pr
	int id;
	#endif
	wp(const int &_a, const int &_b, const int &_c) : a(_a), b(_b), c(_c) {};
	inline bool operator<(const wp &o) const {
		return a == o.a ? (b == o.b ? (c < o.c) : (b<o.b)) : (a < o.a);
	}
	wp(){};
};
vector<wp> w, t;
set<int> S;
bool alg() {
	int pv = 0;
	for (const auto & x : S) {
		c = 0;
	#if pr
		if(pr)printf("Okres %d - %d\n", pv, x);
	#endif
		for (auto i = t.begin(); i != t.end(); i++) {
			INCR++;
			if (i->a >= x) 
				continue;
			const int it = max(i->c - i->b + x, 0);
			c += it;
			i->c -= it;
			i->a = pv + it;
	#if pr
			if (it)
				if(pr)printf("[%d] koniecznie %d  (%d-%d)\n", i->id, it, pv, x);
	#endif
		}
		const int want_c = m * (x - pv);
		if (c > want_c) {
			return false;
		}
		while (c < want_c) {
			int mn=1000006, mnc=0;
			int mn2=mn;
			for (auto &i : t) {
				INCR++;
				if (i.a < pv)
					i.a = pv;
				if (i.a >= x || !i.c) 
					continue;
				const int it = i.b - i.a - i.c;
	#if pr
				if(pr)printf("      considering [%d] as min (%d)\n", i.id, it);
	#endif
				if (total_random) {
					if (fastrand()%3 == 0) {
						mn2 = mn;
						mn = it;
						mnc = 1;
					} else if (it == mn)
						mnc++;
				} else if (it < mn) {
					mn2 = mn;
					mn = it;
					mnc = 1;
				} else if (it == mn)
					mnc++;
			}
	#if pr
			if(pr)printf("   min: %d {%d}\n", mn, mnc);
	#endif
			if (mnc == 0)
				break;

			bool aa = false; // anything added
			const int fe = (min(want_c-c, mnc*(mn2-mn)) + mnc-1) / mnc;
			for (auto &i : t) {
				INCR++;
				if (i.a >= x) 
					continue;

				const int it = i.b - i.a - i.c;
				if (it == mn) {
					const int ta = min(min(min(want_c-c, i.c), min(mn2-it, x - i.a)), fe);
	#if pr
					if(pr)printf("[%d] Opcjonalna %d, zostalo %d (%d-%d)\n", i.id, ta, i.c-ta, i.a, i.b);
	#endif
					if (ta)
						aa = true;
					c += ta;
					i.a += ta;
					i.c -= ta;
				}
			}
			if (!aa) break;
		}
		int p = 0;
		for (auto &i : t) {
			INCR++;
			if (i.b == x) {
				if (i.c > 0) {
					return false;
				}
	#if pr
				if(pr)printf("skonczone [%d]\n", i.id);
	#endif
			} else {
				if (i.c)
					t[p++] = i;
			}
		}
		t.resize(p);
		pv = x;
	}
	return true;
}
int main() {
	unsigned int ms;
	scanf("%d%d", &n, &m);
	ms = (214013*ms+2531011); 
	ms *= n * m;
	for (int i = 0; i < n; i++) {
		scanf("%d%d%d", &a, &b, &c);
		w.emplace_back(a,b,c);
		ms *= a;
		ms ^= b;
		ms = (214013*ms+2531011+c); 
		S.insert(a);
		S.insert(b);
		#if pr
		w[i].id = i+1;
		#endif
	}
	srand (ms);
	g_seed = ms ^ rand();
	t=w;
	#if pr
	printf("----------_BEGIN_ (%d)-----------\n", INCR);
	#endif
	bool odp = alg();
	sort(w.begin(), w.end());
	t=w;
	#if pr
	printf("----------SORT (%d)-----------\n", INCR);
	#endif
	odp |= alg();
	sort(w.rbegin(), w.rend());
	#if pr
	printf("----------REV (%d)-----------\n", INCR);
	#endif
	t=w;
	odp |= alg();
	total_random = true;
	for (int i = 0; !odp && INCR < 1e6*n; i++) {
		#if pr
		printf("----------RANDOM_%d (%d)-----------\n", i, INCR);
		#endif
		random_shuffle(w.begin(), w.end(), fastrand_z);
		t=w;
		odp |= alg();
	}
	printf("%s\n", odp ? "TAK" : "NIE");
}