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
#include <bits/stdc++.h>
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define TRAV(i, n) for(auto &i : n)
#define SZ(x) (int)(x).size()
#define PR std::pair
#define MP std::make_pair
#define X first
#define Y second
typedef long long ll;
typedef std::pair<int, int> PII;
typedef std::vector<int> VI;

struct Tree{
	VI t;
	int BOTTOM;
	void build(int sz){
		BOTTOM = 1;
		while(BOTTOM < sz) BOTTOM *= 2;
		t.resize(BOTTOM*2);
	}
	void setmax(int a, int b, int w){
		a += BOTTOM;
		b += BOTTOM+1;
		while(a < b){
			if(a&1){
				t[a] = std::max(t[a], w);
				a++;
			}
			if(b&1){
				b--;
				t[b] = std::max(t[b], w);
			}
			a >>= 1; b >>= 1;
		}
	}
	int query(int v){
		v += BOTTOM;
		int mx = 0;
		while(v >= 1){
			mx = std::max(mx, t[v]);
			v >>= 1;
		}
		return mx;
	}
};

int n, m;
Tree tp, tl;
std::set<int> sy[100005], sx[100005];

int good(int r, int c){
	int styk = 0;
	int col = m-c+1;
	if(tp.query(r-1) >= col-1) styk = styk|1;
	int row = n-r+1;
	if(tl.query(row-1) >= c-1) styk = styk|2;
	return styk;
}

//int ciag;

void placer(PII start){
	std::vector<PII> stack;
	stack.push_back(start);
	//ciag--;
	while(SZ(stack)){
	//	ciag++;
		PII pos = stack.back();
		stack.pop_back();
		int r = pos.X;
		int c = pos.Y;
		//assert(r < n && c > 1);
		int col = m-pos.Y+1;
		tp.setmax(0, r, col);
		auto poz = sy[r+1].lower_bound(c-1);
		if(poz != sy[r+1].end()){
			stack.push_back(MP(r+1, *poz));
			//assert(sx[*poz].count(r+1));
			sx[*poz].erase(r+1);
			sy[r+1].erase(poz);
		}
		auto pz = sx[c-1].upper_bound(r+1);
		if(pz != sx[c-1].begin()){
			pz--;
			stack.push_back(MP(*pz, c-1));
			//assert(sy[*pz].count(c-1));
			sy[*pz].erase(c-1);
			sx[c-1].erase(pz);
		}
		if(SZ(stack) >= 2 && stack.back() == stack[SZ(stack)-2]) stack.pop_back();
	}
}

void placel(PII start){
	std::vector<PII> stack;
	stack.push_back(start);
	//ciag--;
	while(SZ(stack)){
		//ciag++;
		PII pos = stack.back();
		stack.pop_back();
		int r = pos.X;
		int c = pos.Y;
		//assert(r > 1 && c < m);
		int row = n-pos.X+1;
		tl.setmax(0, row, c);
		auto poz = sy[r-1].upper_bound(c+1);
		if(poz != sy[r-1].begin()){
			poz--;
			stack.push_back(MP(r-1, *poz));
			//assert(sx[*poz].count(r-1));
			sx[*poz].erase(r-1);
			sy[r-1].erase(poz);
		}
		auto pz = sx[c+1].lower_bound(r-1);
		if(pz != sx[c+1].end()){
			stack.push_back(MP(*pz, c+1));
			//assert(sy[*pz].count(c+1));
			sy[*pz].erase(c+1);
			sx[c+1].erase(pz);
		}
		if(SZ(stack) >= 2 && stack.back() == stack[SZ(stack)-2]) stack.pop_back();
	}
}

int main(){
	int k;
	std::scanf("%d%d%d", &n, &m, &k);
	tp.build(n+1);
	tl.build(n+1);
	tp.setmax(0, 0, m);
	tl.setmax(0, 0, m);
	int x = 0;
	FOR(_, k){
		int r, c, z;
		std::scanf("%d%d%d", &r, &c, &z);
		r = (r^x)%n;
		c = (c^x)%m;
		r++;
		c++;
		int styk = good(r, c);
		if(styk == 3){
			std::printf("TAK\n");
			x = x^z;
		}else{
			std::printf("NIE\n");
			if(styk == 2) placel(MP(r, c));
			else if(styk == 1) placer(MP(r, c));
			else{
				sx[c].insert(r);
				sy[r].insert(c);
			}
		}
	}
	//assert(ciag <= k);
	//std::cerr << ciag << ", a k bylo " << k << "\n";
	return 0;
}