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
//Krzysztof Boryczka
#pragma GCC optimize "O3"
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;

#define FOR(i, b, e) for(int i=b; i<=e; i++)
#define FORD(i, b, e) for(int i=b; i>=e; i--)
#define SIZE(x) ((int)x.size())
#define pb push_back
#define st first
#define nd second
#define sp ' '
#define ent '\n'

const int N=25e6+5;

int n, m, akt;
int t[N], vis[N];
queue<ii> q;
vii upd;

int id(int a, int b){
	return a*m+b;
}

bool in(int a, int b){
	return 0<=a && a<n && 0<=b && b<m;
}

void dodaj(int a, int b){
	if(in(a, b) && vis[id(a, b)]<akt){
		vis[id(a, b)]=akt;
		q.push({a, b});
	}
}

bool block(int a, int b){
	akt++;
	upd.clear();
	upd.pb({id(a, b), t[id(a, b)]});
	t[id(a, b)]=0;
	dodaj(a+1, b);
	dodaj(a, b+1);
	while(!q.empty()){
		auto v=q.front();
		q.pop();
		int spos=(in(v.st-1, v.nd) && t[id(v.st-1, v.nd)]>0)+(in(v.st, v.nd-1) && t[id(v.st, v.nd-1)]>0);
		if(spos<t[id(v.st, v.nd)]){
			upd.pb({id(v.st, v.nd), t[id(v.st, v.nd)]});
			t[id(v.st, v.nd)]=spos;
			dodaj(v.st+1, v.nd);
			dodaj(v.st, v.nd+1);
		}
	}
	if(t[id(n-1, m-1)]==0){
		for(auto &x: upd) t[x.st]=x.nd;
		return 1;
	}
	return 0;
}

void solve(){
	int k, a, b, c, last=0;
	cin>>n>>m>>k;
	FOR(i, 1, n) FOR(j, 1, m) t[id(i-1, j-1)]=2-(i==1 || j==1);
	FOR(i, 1, k){
		cin>>a>>b>>c;
		a=(a^last)%n;
		b=(b^last)%m;
		if(block(a, b)){
			cout<<"TAK\n";
			last^=c;
		}
		else cout<<"NIE\n";
	}
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	// int tt; cin>>tt;
	// FOR(te, 1, tt)
	solve();
	return 0;
}