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
176
177
178
179
180
181
182
183
#include <bits/stdc++.h>
using namespace std;
#define PII pair<int, int>
#define PLL pair<LL, LL>
#define VI vector<int>
#define VPII vector<PII>
#define LL long long
#define LD long double
#define f first
#define s second
#define MP make_pair
#define PB push_back
#define pb pop_back
#define ALL(c) (c).begin(), (c).end()
#define SIZ(c) (int)(c).size()
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, b, e) for(int i = (b); i <= (int)(e); ++i)
#define FORD(i, b, e) for(int i = (b); i >= (int)(e); --i)

#define Sim template<class n
Sim, class s> ostream & operator << (ostream &p, pair<n, s> x)
{return p << "<" << x.f << ", " << x.s << ">";}
Sim> auto operator << (ostream &p, n y) -> 
typename enable_if<!is_same<n, string>::value, decltype(y.begin(), p)>::type 
{int o = 0; p << "{"; for(auto c: y) {if(o++) p << ", "; p << c;} return p << "}";}
void dor() {cerr << endl;}
Sim, class...s> void dor(n p, s...y) {cerr << p << " "; dor(y...);}
Sim, class s> void mini(n &p, s y) {if(p>y) p = y;}
Sim, class s> void maxi(n &p, s y) {if(p<y) p = y;}
#ifdef DEB
#define debug(...) dor(__FUNCTION__, ":", __LINE__, ": ", __VA_ARGS__)
#else
#define debug(...)
#endif 

#define I(x) #x " = ", (x), " "
#define A(a, i) #a "[" #i " = ", i, "] = ", a[i], " "
const int M = 1024 * 128;

int n, m, k, xo;
set<int> poz[M], pion[M];
int D[2][2 * M];

int get(int ind, int v)
{
	v += M;
	int r = D[ind][v];
	while (v > 1)
	{
		v /= 2;
		maxi(r, D[ind][v]);
	}
	return r;
}

void add(int ind, int p, int kk, int x)
{
	p += M;
	kk += M;
	maxi(D[ind][p], x);
	maxi(D[ind][kk], x);
	while (p / 2 != kk / 2)
	{
		if (p % 2 == 0)
		{
			maxi(D[ind][p + 1], x);
		}
		p /= 2;
		if (kk % 2 == 1)
		{
			maxi(D[ind][kk - 1], x);
		}
		kk /= 2;
	}
}

void goup(int x, int y)
{
	add(0, y, M - 1, x);
	auto it = poz[x + 1].lower_bound(y - 1);
	if (it != poz[x + 1].end())
	{
		PII p = MP(x + 1, *it);
		poz[x + 1].erase(it);
		if (get(0, p.s) < p.f)
		{
			goup(p.f, p.s);
		}
	}
	it = pion[y - 1].upper_bound(x + 1);
	if (it != pion[y - 1].begin())
	{
		it--;
		PII p = MP(*it, y - 1);
		if (get(0, p.s) < p.f)
		{
			goup(p.f, p.s);
		}
	}
}

void godown(int x, int y)
{
	add(1, 1, y, -x);
	auto it = poz[x - 1].upper_bound(y + 1);
	if (it != poz[x - 1].begin())
	{
		it--;
		PII p = MP(x - 1, *it);
		if (get(1, p.s) < -p.f)
		{
			godown(p.f, p.s);
		}
	}
	it = pion[y + 1].lower_bound(x - 1);
	if (it != pion[y + 1].end())
	{
		PII p = MP(*it, y + 1);
		if (get(1, p.s) < -p.f)
		{
			godown(p.f, p.s);
		}
	}
}

int32_t main()
{
	//ios_base::sync_with_stdio(0);
	//cin.tie(NULL);
	scanf("%d%d%d", &n, &m, &k);
	FOR(i, 0, 2 * M - 1)
	{
		D[1][i] = -n - 1;
	}
	add(0, 0, 0, M);
	add(0, m + 1, m + 1, M);
	add(1, 0, 0, M);
	add(1, m + 1, m + 1, M);
	FOR(i, 1, k)
	{
		int x, y, z;
		scanf("%d%d%d", &x, &y, &z);
		x = (x ^ xo) % n;
		y = (y ^ xo) % m;
		x++;
		y++;
		if (get(0, y) >= x || get(1, y) >= -x)
		{
			printf("NIE\n");
			continue;
		}
		bool up = 0, down = 0;
		if (get(0, y + 1) >= x - 1)
		{
			up = 1;
		}
		if (get(1, y - 1) >= -x - 1)
		{
			down = 1;
		}
		if (up && down)
		{
			printf("TAK\n");
			xo ^= z;
			continue;
		}
		if (up)
		{
			goup(x, y);
		}
		else if (down)
		{
			godown(x, y);
		}
		else
		{
			poz[x].insert(y);
			pion[y].insert(x);
		}
		printf("NIE\n");
	}
	return 0;
}