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
#include <bits/stdc++.h>

using namespace std;

int pietro[2 * (int)1e5  + 100];
int n,m,k;

queue<pair<int,int> > kol1,kol2;

bool wartLewyParent(int &x, int &y, vector<vector<bool> > &a){
	if(y - 1 >= 0)
		return a[x][y - 1];
	return 1;
}
bool wartPrawyParent(int &x, int &y, vector<vector<bool> > &a){
	if(x - 1 >= 0)
		return a[x - 1][y];
	return 1;
}
bool wartLewySon(int &x, int &y, vector<vector<bool> > &a){
	if(x + 1 < n)
		return a[x + 1][y];
	return 1;
}
bool wartPrawySon(int &x, int &y, vector<vector<bool> > &a){
	if(y + 1 < m)
		return a[x][y + 1];
	return 1;
}
void addLewyParent(int &x, int &y){
	if(y - 1 >= 0)
		kol1.push({x,y - 1});
}
void addPrawyParent(int &x, int &y){
	if(x - 1 >= 0)
		kol1.push({x - 1,y});
}
void addLewySon(int &x, int &y){
	if(x + 1 < n)
		kol2.push({x + 1,y});
}
void addPrawySon(int &x, int &y){
	if(y + 1 < m)
		kol2.push({x,y + 1});
}
void goUp(vector<vector<bool> > &a){
	while(kol1.size()){
		int x,y;
		x = kol1.front().first;
		y = kol1.front().second;
		kol1.pop();
		if(a[x][y] == 1 || (x == 0 && y == 0) || (x == n - 1 && y == m - 1))
			continue;
		if((wartLewySon(x,y,a) && wartPrawySon(x,y,a)) || (wartLewyParent(x,y,a) && wartPrawyParent(x,y,a))){
			a[x][y] = 1;
			pietro[x + y]--;
			addLewyParent(x,y);
			addPrawyParent(x,y);
		}
	}
}
void goDown(vector<vector<bool> > &a){
	while(kol2.size()){
		int x = kol2.front().first;
		int y = kol2.front().second;
		kol2.pop();
		if(a[x][y] == 1 || (x == 0 && y == 0) || (x == n - 1 && y == m - 1))
			continue;
		if((wartLewyParent(x,y,a) && wartPrawyParent(x,y,a)) || (wartLewySon(x,y,a) && wartPrawySon(x,y,a))){
			a[x][y] = 1;
			pietro[x + y]--;
			addLewySon(x,y);
			addPrawySon(x,y);
		}
	}	
}
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

	cin >> n >> m >> k;
	
	vector<vector<bool> > a(n,vector<bool>(m));
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			pietro[i + j]++;
	int x = 0;
	while(k--){
		int r,c,z;
		cin >> r >> c >> z;
		r = (r ^ x) % n;
		c = (c ^ x) % m;
		if(a[r][c] == 0 && pietro[r + c] == 1){
			cout << "TAK" << '\n';
			x ^= z;
		}
		else{
			cout << "NIE" << '\n';
			if(a[r][c] == 1)
				continue;
			a[r][c] = 1;
			pietro[r + c]--;
			addLewyParent(r,c);
			addPrawyParent(r,c);
			addLewySon(r,c);
			addPrawySon(r,c);
			goUp(a);
			goDown(a);
		}
	}
	return 0;
}