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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
#include <iostream>
#include <vector>
#include <map>
#include <set>

//#define DEBUG
//#define PRINTARR


using namespace std;

int m,n,k;
int r,c,z;
int x=0;

struct StrongholdGroup {
	int minX;
	int minY;
	int maxX;
	int maxY;
	StrongholdGroup* parent;
};

struct Stronghold {
	StrongholdGroup* group;
	StrongholdGroup* getGroup() {
		while(group->parent)
			group = group->parent;
		return group;
	}
};
ostream& operator<<(ostream& os, const pair<int,int>& p) {
	return os<<"("<<p.first<<","<<p.second<<")";
}
ostream& operator<<(ostream& os, const StrongholdGroup& group) {
	return os<<"{"<<group.minX<<","<<group.minY<<","<<group.maxX<<","<<group.maxY<<"}";
}

map<int,map<int,Stronghold*> > rowMap, colMap;
map<pair<int,int>, Stronghold* > pointMap;
vector<int> blockedTop, blockedLeft; // vector since it's always 0..x/0..y
vector<int> blockedRight, blockedBottom; // vector since it's always n..x/m..y
set<StrongholdGroup*> leftBottomQueue, rightTopQueue;

const int MAX_K = 1000002;
Stronghold tab[MAX_K];
int strongholdCnt=0;

int inline wrap(int val, int base) {
	return (val ^ x) % base;
}

void blockTop(int row, int col) {
#if defined(DEBUG) //{
	cerr<<"  blockTop "<<make_pair(row,col)<<endl;
#endif //}
	if(blockedTop.size()<=row)
		blockedTop.resize(row+1, col);
	for(int i=0;i<=row;++i)
		blockedTop[i] = min(blockedTop[i], col);
}
void blockLeft(int row, int col) {
#if defined(DEBUG) //{
	cerr<<"  blockLeft "<<make_pair(row,col)<<endl;
#endif //}
	if(blockedLeft.size()<=col)
		blockedLeft.resize(col+1, row);
	for(int i=0;i<=col;++i)
		blockedLeft[i] = min(blockedLeft[i], row);
}
void blockRight(int row, int col) {
#if defined(DEBUG) //{
	cerr<<"  blockRight "<<make_pair(row,col)<<endl;
#endif //}
	int index = m-col-1;
	if(blockedRight.size()<=index)
		blockedRight.resize(index+1, row);
	for(int i=0;i<=index;++i)
		blockedRight[i] = max(blockedRight[i], row);
}
void blockBottom(int row, int col) {
#if defined(DEBUG) //{
	cerr<<"  blockBottom "<<make_pair(row,col)<<endl;
#endif //}
	int index = n-row-1;
	if(blockedBottom.size()<=index)
		blockedBottom.resize(index+1, col);
	for(int i=0;i<=index;++i)
		blockedBottom[i] = max(blockedBottom[i], col);
}

bool isBlockedTop(int x, int y) {
	if(x==0)
		return true;
	if(x>blockedTop.size())
		return false;
	return blockedTop[x-1] <= y+1;
}
bool isBlockedRight(int x, int y) {
	if(y==m-1)
		return true;
	if((m-1)-y > blockedRight.size())
		return false;
	return blockedRight[m-y-2] >= x-1;
}
bool isBlockedLeft(int x, int y) {
	if(y==0) {
		return true;
	}
	if(y>blockedLeft.size()) {
		return false;
	}
	return blockedLeft[y-1] <= x+1;
}
bool isBlockedBottom(int x, int y) {
	if(x==n-1) {
			//cerr<<"x=="<<(n-1)<<" -> true"<<endl;
		return true;
	}
	if((n-1)-x > blockedBottom.size()) {
			//cerr<<(n-x)<<">size("<<blockedBottom.size()<<") -> false"<<endl;
		return false;
	}
	//cerr << "blockedBottom["<<(n-x-2)<<"] = "<<blockedBottom[n-x-2]<<endl;
	return blockedBottom[n-x-2] >= y-1;
}

bool inline isLeftBottom(StrongholdGroup* group) {
#if defined(DEBUG) //{
	cerr<<"  isLeftBottom "<<*group<<" -> ";cerr<<(isBlockedLeft(group->maxX, group->minY) || isBlockedBottom(group->maxX, group->minY))<<endl;
#endif //}
	//return (group)->maxX == n-1 || (group)->minY == 0;
	return isBlockedLeft(group->maxX, group->minY) || isBlockedBottom(group->maxX, group->minY);
}
bool inline isRightTop(StrongholdGroup* group) {
#if defined(DEBUG) //{
	cerr<<"  isRightTop "<<*group<<" -> ";cerr<<(isBlockedRight(group->minX, group->maxY) || isBlockedTop(group->minX, group->maxY))<<endl;
#endif //}
	//return (group)->maxY == m-1 || (group)->minX == 0;
	return isBlockedRight(group->minX, group->maxY) || isBlockedTop(group->minX, group->maxY);
}
bool inline isLeftBottom(int x, int y) {
#if defined(DEBUG) //{
	cerr<<"  isLeftBottom ("<<x<<" "<<y<<") -> ";cerr<<(isBlockedLeft(x,y) || isBlockedBottom(x,y))<<endl;
#endif //}
	//return x == n-1 || y == 0;
	return isBlockedLeft(x,y) || isBlockedBottom(x,y);
}
bool inline isRightTop(int x, int y) {
#if defined(DEBUG) //{
	cerr<<"  isRightTop ("<<x<<" "<<y<<") -> ";cerr<<(isBlockedRight(x,y) || isBlockedTop(x,y))<<endl;
#endif //}
	//return x == 0 || y == m-1;
	return isBlockedRight(x,y) || isBlockedTop(x,y);
}

map<pair<int,int>, Stronghold*>::iterator FIND(const std::pair<int,int>& key) {
	auto res = pointMap.find(key);
#if defined(DEBUG) //{
		cerr<<"  find "<<key<<" -> "<<(res==pointMap.end() ? "nope" : "found")<<endl;
#endif //}
	return res;
}

bool alreadyBlocked(int x, int y) {
	return isRightTop(x+1, y-1)
	|| isLeftBottom(x-1, y+1);
}

//#define GET(row, col) ((pos=pointMap.find(make_pair(row, col))) != pointMap.end())
#define GET(row, col) ((pos=FIND(make_pair(row, col))) != pointMap.end())

bool cannotBuild(int row, int column) {
	//row = 0..<n, col = 0..<m
#if defined(DEBUG) //{
	//row = n-1-row; column = m-1-column;
	cerr<<"  simulate("<<row<<","<<column<<")"<<endl;
#ifdef PRINTARR
	for(pair<const pair<int,int>,Stronghold*>& elem : pointMap) {
		cerr << "   at pt: " << elem.first<<" - group " << *elem.second->getGroup() << endl;
	}
	cerr<<"  blocked top"<<endl;
	int last=-1;
	for(int i=0;i<blockedTop.size();++i)
		if(last!=blockedTop[i] && (last=blockedTop[i])!=-1)
		cerr<<"   "<<i<<": >"<<blockedTop[i]<<endl;
	cerr<<"  blocked left"<<endl;
	last=-1;
	for(int i=0;i<blockedLeft.size();++i)
		if(last!=blockedLeft[i] && (last=blockedLeft[i])!=-1)
		cerr<<"   "<<i<<": >"<<blockedLeft[i]<<endl;
	cerr<<"  blocked bottom"<<endl;
	last=-1;
	for(int i=0;i<blockedBottom.size();++i)
		if(last!=blockedBottom[i] && (last=blockedBottom[i])!=-1)
		cerr<<"   "<<(n-1-i)<<": <"<<blockedBottom[i]<<endl;
	cerr<<"  blocked right"<<endl;
	last=-1;
	for(int i=0;i<blockedRight.size();++i)
		if(last!=blockedRight[i] && (last=blockedRight[i])!=-1)
		cerr<<"   "<<(m-1-i)<<": <"<<blockedRight[i]<<endl;
#endif
#endif //}
	if(alreadyBlocked(row, column)) {
#if defined(DEBUG) //{
		cerr << "  already blocked - won't affect" << endl;
#endif //}
		return false;
	}
	map<pair<int,int>, Stronghold*>::iterator pos;
	//check (r-1,c), (r, c+1), (r-1, c+1)
	StrongholdGroup *upperGroup = nullptr, *lowerGroup = nullptr;
	if(GET(row-1, column) || GET(row, column+1) || GET(row-1, column+1)) {
		upperGroup = pos->second->getGroup();
#if defined(DEBUG) //{
		cerr<<"  found upper "<<*upperGroup<<endl;
#endif //}
	}
	//check (r+1,c), (r, c-1), (r+1, c-1)
	if(GET(row+1, column) || GET(row, column-1) || GET(row+1, column-1)) {
		lowerGroup = pos->second->getGroup();
#if defined(DEBUG) //{
		cerr<<"  found lower "<<*lowerGroup<<endl;
#endif //}
	}
	bool leftBottom = isLeftBottom(row, column);
	bool rightTop = isRightTop(row, column);
	// closes at top/right / closes at left/bottom / closes at middle
	if(upperGroup && leftBottom && isRightTop(upperGroup)) {
#if defined(DEBUG) //{
		cerr<<"  closes at top/right"<<endl;
#endif //}
		return true;
	}
	else if(lowerGroup && rightTop && isLeftBottom(lowerGroup)) {
#if defined(DEBUG) //{
		cerr<<"  closes at left-bottom"<<endl;
#endif //}
		return true;
	}
	else if(lowerGroup && upperGroup && isLeftBottom(lowerGroup) && isRightTop(upperGroup)) {
#if defined(DEBUG) //{
		cerr<<"  closes at middle"<<endl;
#endif //}
		return true;
	}
	else if(leftBottom && rightTop) {
#if defined(DEBUG) //{
		cerr<<"  closes by itself"<<endl;
#endif //}
		return true;
	}
	int blockTopRow=-1,blockTopCol=-1;
	int blockLeftRow=-1,blockLeftCol=-1;
	int blockBottomRow=-1,blockBottomCol=-1;
	int blockRightRow=-1,blockRightCol=-1;
	//check blocking
	if(rightTop) {
		if(isBlockedRight(row, column)) {
#if defined(DEBUG) //{
			cerr << "  check block right" << endl;
#endif //}
			map<int,map<int,Stronghold*> >::reverse_iterator iter(colMap.lower_bound(column));
			int lastRow = row;
			int lastCol = column;
			while(iter != colMap.rend() && iter->first == lastCol-1) {
#if defined(DEBUG) //{
			cerr << "  checking column " << iter->first << endl;
#endif //}
				map<int,Stronghold*>::reverse_iterator iter2(iter->second.upper_bound(lastRow+1));
				lastRow=0;
				if(iter2 == iter->second.rend())
					break;
				for(;iter2!=iter->second.rend();++iter2) {
#if defined(DEBUG) //{
					cerr << "  checking stronghold " << make_pair(iter2->first, iter->first) << endl;
#endif //}
					StrongholdGroup* grp = iter2->second->getGroup();
					lastRow = max(lastRow, grp->maxX);
					if(isLeftBottom(grp)) {
#if defined(DEBUG) //{
						cerr << "  right would block " << *grp << endl;
#endif //}
						return true;
					}
					rightTopQueue.insert(grp);
				}
				--lastCol;
				++iter;
			}
#if defined(DEBUG) //{
			cerr << "  able to block right" << make_pair(lastRow, lastCol) << endl;
#endif //}
			blockRightRow=row;
			blockRightCol=column;
		}
		if(isBlockedTop(row, column)) {
#if defined(DEBUG) //{
			cerr << "  check block top" << endl;
#endif //}
			map<int,map<int,Stronghold*> >::iterator iter = rowMap.upper_bound(row);
			int lastRow = row;
			int lastCol = column;
			while(iter != rowMap.end() && iter->first == lastRow+1) {
#if defined(DEBUG) //{
			cerr << "  checking row " << iter->first << endl;
#endif //}
				map<int,Stronghold*>::iterator iter2 = iter->second.lower_bound(lastCol-1);
				lastCol = 20000000;
				if(iter2 == iter->second.end())
					break;
				for(;iter2!=iter->second.end();++iter2) {
#if defined(DEBUG) //{
					cerr << "  checking stronghold " << make_pair(iter->first, iter2->first) << endl;
#endif //}
					StrongholdGroup* grp = iter2->second->getGroup();
					lastCol = min(lastCol, grp->minY);
					if(isLeftBottom(grp)) {
#if defined(DEBUG) //{
						cerr << "  top would block " << *grp << endl;
#endif //}
						return true;
					}
					rightTopQueue.insert(grp);
				}
				++lastRow;
				++iter;
			}
#if defined(DEBUG) //{
			cerr << "  able to block top" << make_pair(lastRow, lastCol) << endl;
#endif //}
			blockTopRow=row;
			blockTopCol=column;
		}
	}
	if(leftBottom) {
		if(isBlockedLeft(row, column)) {
#if defined(DEBUG) //{
			cerr << "  check block left" << endl;
#endif //}
			map<int,map<int,Stronghold*> >::iterator iter = colMap.upper_bound(column);
			int lastRow = row;
			int lastCol = column;
			while(iter != colMap.end() && iter->first == lastCol+1) {
#if defined(DEBUG) //{
			cerr << "  checking column " << iter->first << endl;
#endif //}
				map<int,Stronghold*>::iterator iter2 = iter->second.lower_bound(lastRow-1);
				lastRow = 20000000;
				if(iter2 == iter->second.end())
					break;
				for(;iter2!=iter->second.end();++iter2) {
#if defined(DEBUG) //{
					cerr << "  checking stronghold " << make_pair(iter2->first, iter->first) << endl;
#endif //}
					StrongholdGroup* grp = iter2->second->getGroup();
					lastRow = min(lastRow, grp->minX);
					if(isRightTop(grp)) {
#if defined(DEBUG) //{
						cerr << "  left would block " << *grp << endl;
#endif //}
						return true;
					}
					leftBottomQueue.insert(grp);
				}
				++lastCol;
				++iter;
			}
#if defined(DEBUG) //{
			cerr << "  able to block left" << make_pair(lastRow, lastCol) << endl;
#endif //}
			blockLeftRow=row;
			blockLeftCol=column;
		}
		if(isBlockedBottom(row, column)) {
#if defined(DEBUG) //{
			cerr << "  check block bottom" << endl;
#endif //}
			map<int,map<int,Stronghold*> >::reverse_iterator iter(rowMap.lower_bound(row));
			int lastRow = row;
			int lastCol = column;
			while(iter != rowMap.rend() && iter->first == lastRow-1) {
#if defined(DEBUG) //{
			cerr << "  checking row " << iter->first << endl;
#endif //}
				map<int,Stronghold*>::reverse_iterator iter2(iter->second.upper_bound(lastCol+1));
				lastCol = 0;
				if(iter2 == iter->second.rend())
					break;
				for(;iter2!=iter->second.rend();++iter2) {
					StrongholdGroup* grp = iter2->second->getGroup();
					lastCol = max(lastCol, grp->maxY);
#if defined(DEBUG) //{
					cerr << "  checking stronghold " << make_pair(iter->first, iter2->first) << ", lastCol="<<lastCol << endl;
#endif //}
					if(isRightTop(grp)) {
#if defined(DEBUG) //{
						cerr << "  bottom would block " << *grp << endl;
#endif //}
						return true;
					}
					leftBottomQueue.insert(grp);
				}
				--lastRow;
				++iter;
			}
#if defined(DEBUG) //{
			cerr << "  able to block bottom" << make_pair(lastRow, lastCol) << endl;
#endif //}
			blockBottomRow=row;
			blockBottomCol=column;
		}
	}
	//join and block - can build

	Stronghold& str = tab[strongholdCnt++];
	if(lowerGroup) {
		str.group = lowerGroup;
		pointMap[make_pair(row, column)] = &str;
		if(upperGroup && lowerGroup != upperGroup) {
			upperGroup->parent = lowerGroup;
			lowerGroup->minX = upperGroup->minX;
			lowerGroup->maxY = upperGroup->maxY;
		}
		else {
			if(lowerGroup->minX < row || lowerGroup->maxY > column) {
				str.group = new StrongholdGroup{row, lowerGroup->minY, lowerGroup->maxX, column, nullptr};
			} else {
				(lowerGroup)->minX = row;
				(lowerGroup)->maxY = column;
			}
		}
	}
	else if(upperGroup) {
		str.group = upperGroup;
		pointMap[make_pair(row, column)] = &str;
		if(upperGroup->maxX > row || upperGroup->minY < column) {
			str.group = new StrongholdGroup{upperGroup->maxX, column, row, upperGroup->minY, nullptr};
		} else {
			(upperGroup)->maxX = row;
			(upperGroup)->minY = column;
		}
	}
	else {
		str.group = new StrongholdGroup{row, column, row, column, nullptr};
		pointMap[make_pair(row, column)] = &str;
	}
	rowMap[row][column] = &str;
	colMap[column][row] = &str;
	if(blockTopCol != -1)
		blockTop(blockTopRow, blockTopCol);
	if(blockLeftCol != -1)
		blockLeft(blockLeftRow, blockLeftCol);
	if(blockBottomCol != -1)
		blockBottom(blockBottomRow, blockBottomCol);
	if(blockRightCol != -1)
		blockRight(blockRightRow, blockRightCol);

	for(StrongholdGroup* grp : leftBottomQueue) {
		blockLeft(grp->minX, grp->maxY);
		blockBottom(grp->minX, grp->maxY);
	}
	for(StrongholdGroup* grp : rightTopQueue) {
		blockRight(grp->maxX, grp->minY);
		blockTop(grp->maxX, grp->minY);
	}

	return false;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin>>n>>m>>k;
	for(int i=0;i<k;++i) {
		cin>>r>>c>>z;
		if(cannotBuild(wrap(r,n),wrap(c,m))) {
			cout<<"TAK"<<endl;
			x = x^z;
		} else {
			cout<<"NIE"<<endl;
		}
		rightTopQueue.clear();
		leftBottomQueue.clear();
	}
#ifdef PRINTARR
	bool empty=false,newEmpty;
	string row;
	for(int r=0;r<n;++r) {
		row="";
		newEmpty=true;
		for(int c=0;c<m;++c)
			if(pointMap.find(make_pair(r,c)) != pointMap.end()) {
				row+="X";
				newEmpty=false;
			}
			else
				row+=" ";
		if(!empty && newEmpty)
			cerr << "[cut row(s)]"<<endl;
		else if(!newEmpty)
			cerr << row << endl;
		empty=newEmpty;
	}
#endif // PRINTARR
	return 0;
}