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
#include<bits/stdc++.h>
using namespace std;
 
ostream& operator<<(ostream &out, string str) {
	for(char c : str) out << c;
	return out;
}
 
template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) {
	return out << "(" << p.first << ", " << p.second << ")";
}
 
template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
	out << "{";
	for(auto it = a.begin(); it != a.end(); it = next(it))
		out << (it != a.begin() ? ", " : "") << *it;
	return out << "}";
}
 
void dump() { cerr << "\n"; }
template<class T, class... Ts> void dump(T a, Ts... x) {
	cerr << a << ", ";
	dump(x...);
}
 
#ifdef DEBUG
#  define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
#  define debug(...) false
#endif
 
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define ST first
#define ND second
 
template<class T> int size(T && a) { return a.size(); }
 
using LL = long long;
using PII = pair<int, int>;

mt19937 rng(2137);
int rd(int a, int b) {
	return uniform_int_distribution<int>(a, b)(rng);
}

struct Fenwick {
	vector<int> s;
	Fenwick(int n) : s(n) {}
	void update(int pos, int val) {
		for(; pos < size(s); pos |= pos + 1)
			s[pos] = max(s[pos], val);
	}
	int query(int pos) { pos++;
		int ret = 0;
		for(; pos > 0; pos &= pos - 1)
			ret = max(ret, s[pos - 1]);
		return ret;
	}
};

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n, m, k;
	cin >> n >> m >> k;
	// n = 1e3, m = 1e3, k = 1e6;

	vector<set<int>> pts_r(n), pts_c(m);
	Fenwick tree_r(n + 1), tree_c(m + 1);
	tree_r.update(n, m), tree_c.update(m, n);

	auto check = [&](int x, int y) {
		if(n == 1 || m == 1) return true;
		return tree_r.query(x) <= y && tree_c.query(y) <= x &&
			   tree_r.query(x + 1) >= y && tree_c.query(y + 1) >= x;
	};

	int h = 0;
	REP(i, k) {
		int r, c, z;
		cin >> r >> c >> z;
		// r = rd(0, 1 << 20), c = rd(0, 1 << 20), z = rd(0, 1 << 20);
		int x = (r ^ h) % n, y = (c ^ h) % m;

		debug(x, y);
		REP(j, n) debug(j, tree_r.query(j));
		REP(j, m) debug(j, tree_c.query(j));

		if(check(x, y)) {
			cout << "TAK\n";
			h ^= z;
		}
		else {
			cout << "NIE\n";
			REP(s, 2) {
				function<void(int, int)> dfs;
				dfs = [&](int _x, int _y) {
					if(tree_r.query(_x) < _y + 1) {
						tree_r.update(_x, _y + 1);
						int __x = _x, __y = _y;
						if(++_y < m) {
							auto it = pts_c[_y].lower_bound(_x - 1);
							if(it != pts_c[_y].end()) {
								_x = *it;	
								dfs(_x, _y);
							}
						}
						_x = __x, _y = __y;
						if(0 <= --_x) {
							auto it = pts_r[_x].upper_bound(_y + 1);
							if(it != pts_r[_x].begin()) {
								_y = *prev(it);
								dfs(_x, _y);
							}
						}
					}
				};

				if(tree_r.query(x + 1) >= y) dfs(x, y);

				swap(x, y);
				swap(n, m);
				swap(tree_r, tree_c);
				swap(pts_r, pts_c);
			}

			pts_r[x].emplace(y);
			pts_c[y].emplace(x);
		}
	}
}