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


using namespace std;

int n, m, k;
int xHidden = 0;
int zLast = 0;

pair <int,int> readTower() {
    int r, c;
    cin >> r >> c >> zLast;

    r ^= xHidden;
    c ^= xHidden;

    return {r % n, c % m};
}

void printAnswer(bool ans) {
    cout << (ans ? "TAK\n" : "NIE\n");

    if (ans) {
        xHidden ^= zLast;
    }
}

void solve() {
    int diags = n + m - 1;
    vector <int> diagCount(diags, 0);

    for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
        diagCount[i + j]++;
    }

    vector <vector<bool>> taken(n, vector <bool> (m, false));

    while (k--) {
        int i, j;
        tie(i, j) = readTower();

        if (taken[i][j]) {
            printAnswer(false);
            continue;
        }

        if (diagCount[i + j] == 1) {
            printAnswer(true);
            continue;
        }

        printAnswer(false);

        queue <pair<int,int>> q;

        q.push({i, j});
        taken[i][j] = true;

        while (!q.empty()) {
            int i, j;
            tie(i, j) = q.front(); q.pop();

            diagCount[i + j]--;

            for (auto &e : vector <pair<int,int>> {{i - 1, j + 1}, {i + 1, j - 1}}) {
                int ei, ej;
                tie(ei, ej) = e;

                if (ei < 0 || ei >= n || ej < 0 || ej >= m || taken[ei][ej]) {
                    for (auto &f : vector <pair<int,int>> {{i, ej}, {ei, j}}) {
                        int fi, fj;
                        tie(fi, fj) = f;

                        if (fi >= 0 && fi < n && fj >= 0 && fj < m && !taken[fi][fj]) {
                            q.push(f);
                            taken[fi][fj] = true;
                        }
                    }
                }
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);

    cin >> n >> m >> k;
    solve();

    return 0;
}